Welcome Guest, Not a member yet? Register   Sign In
How to echo this array using foreach?
#1

[eluser]solid9[/eluser]
Hi people

I have these array below,

Code:
Array
(
    [0] => Array
        (
            [phoid] => 55
            [swapid] => 130
            [filename] => motor.jpg
        )

    [1] => Array
        (
            [phoid] => 56
            [swapid] => 130
            [filename] => smile2.jpg
        )

)

All the data came from this array,
Code:
$this->data['photos'];

Now I'm trying to echo the filenames,

Code:
foreach($this->data['photos'][0]['filename'] as $value) {
echo $value;
}

Unfortunately it show some error
Code:
Message: Invalid argument supplied for foreach()

So what is the problem with my foreach?


#2

[eluser]solid9[/eluser]
I can do this using for()
Code:
$total_array = count($this->data['photos']);
for($i=0; $i<$total_array; $i++) {
echo  $this->data['photos'][$i]['filename'];
}

But I want to see it using foreach()

Anyone please?


#3

[eluser]CI_expert_indian[/eluser]
Code:
&lt;?php
$data= Array(
    '0' => Array
        (
            'phoid' => 55,
            'swapid' => 130,
            'filename' => 'motor.jpg'
        ),

    '1' => Array
        (
            'phoid' => 56,
            'swapid' => 130,
            'filename' => 'smile2.jpg'
        )

) ;
foreach($data as $row_data)
{  

echo $row_data[filename] ;

}
?&gt;

get idea from this !
#4

[eluser]CI_expert_indian[/eluser]
Code:
foreach($this->data['photos'] as $row_data)
{  

echo $row_data[filename] ;

}

May this will work in your case ....
#5

[eluser]Phil Sturgeon[/eluser]
['filename'] is a string. You can't foreach through a string.




Theme © iAndrew 2016 - Forum software by © MyBB