PDA

View Full Version : echoing out array values?



LJackson
Oct 18th, 2011, 09:57 PM
I am having trouble echoing out array values,

here is my array

Array ( [1] => Array ( [id] => 1 [type] => start ) [2] => Array ( [id] => 22 [type] => start ) )

and im trying to echo out the values of id and type but im running into difficulties.

here is my code

foreach($appearence as $key => $value)
{
echo $key." ".$value."<br />";
}


which echos out
1 array
2 array

any ideas how i can echo out the values of id and type
1 start
22 start

many thanks
Luke

Fou-Lu
Oct 18th, 2011, 10:15 PM
Complex values can never be directly printed. You can walk it again through a foreach, target each key accordingly, extract with list and print the variables, etc.
If format doesn't matter, a simple print_r, var_dump, or var_export would show these.

LJackson
Oct 18th, 2011, 10:28 PM
thanks for your reply mate,

think i have acheived what im after by using this

<?php
foreach($appearence as $key => $value)
{
echo "<div style='float:left; width:100%; padding:10px;'>";
foreach($value as $value2 => $val)
{
echo $value2." ".$val;
}
echo "</div>";
}?>


thanks for your help!!!

Fou-Lu
Oct 18th, 2011, 10:48 PM
That will do. Alternatively if the only thing that matters is a space, you can simply implode the value instead.


Sorry my bad. You cannot do that since you are looking at key and value together. Well you could do that, but it would take extracting with array_keys and array_values first, and then doing some wizardry which I can guarantee would be more work.