ariel
ariel

Reputation: 389

foreach get one value

When I do var_dump($punkty); I got something like this:

array(1) 
{ 
[0]=> array(4) 
    { 
    ["id"]=> string(2) "28" 
    ["mapa"]=> string(97) "a:3s:3:"lat";s:17:"49.21103723075132";s:3:"lng";s:18:"22.330280542373657";s:4:"zoom";s:2:"17";}" 
    ["miasto"]=> string(5) "Cisna" 
    ["nazwa_obiektu"]=> string(44) "Cisna - noclegi u Mirosławy w Bieszczadach" 
    }
} 

When i do:

foreach ($punkty['mapa'] as $item)
        {
        echo $item;
        }

I get

Invalid argument supplied for foreach() 

How to solve it?

Upvotes: 2

Views: 4186

Answers (4)

FatalError
FatalError

Reputation: 974

mapa is at $punkty[0]['mapa'], not at $punkty['mapa'].

Upvotes: 0

Rupesh Patel
Rupesh Patel

Reputation: 3075

$punkty['mapa'] is not an array in your case but the string you can only pass arrays or objects who implements the iterator to foreach loop.

Upvotes: 0

Doc Roms
Doc Roms

Reputation: 3308

But don't forget to verify your $punkty is not empty, for don't have an other error :-) , for sample:

if (isset($punkty )){
    foreach($punkty as $item){
        echo $item[0]['mapa'];
    }
}

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324830

I think you're trying to do this:

foreach($punkty as $item) {
    echo $item['mapa'];
}

Upvotes: 5

Related Questions