echo array on CHtml::listData

hi all,

i’ve got problem on display string value from array value on CHtml::listdata

i want to keep data array on variable, but i try to display array with :




$models = user::model()->findAll(" sex='male' ");

    $tripsArray = CHtml::listData($models, 'name', 'contact');   

    print_r($tripsArray);

        foreach ($tripsArray as $tripsArray) {

              echo $tripsArray."<br>";

         } 



and the result just display contact and name was not displayed.

how to display name and contact ?

thanks for advices

This how U wrote:

CHtml::listData($models, ‘name’, ‘contact’);

is generating ‘name’ as value and ‘contact’ like label, that’s why only ‘contact’ is displayed.

Please check :

CHtml::listData($models, ‘name’, ‘contact’);

http://www.yiiframework.com/doc/api/1.1/CHtml#listData-detail

Seems that I misunderstood U:

print_r($tripsArray);

    foreach (&#036;tripsArray as &#036;tripsArray) {


          echo &#036;tripsArray.&quot;&lt;br&gt;&quot;;


     } 

if U want to display trough foreach:

print_r($tripsArray);

    foreach (&#036;tripsArray as &#036;key =&gt; &#036;tripArray) {


          echo &#036;key.&#036;tripArray.&quot;&lt;br&gt;&quot;;


     } 

since ‘name’ is key and $tripArray is value

1 Like

Thanks dragan.zivkovic,

that’s amazing bro :D

You are wellcome ;)