How to iterate php array

Asked

Viewed 71 times

0

I have the following block feeding an array

$insert = array();
for($i = 0; $i < 2; $i++){
   $insert[] = array(
                 "os"       => $cdOs,
                 "name"     => $iten->name
                 );
}

The array returns like this:

array (size=1)  0 =>     array (size=3)      'os' => string '84822' (length=5)      'name' => string 'Desert.jpg' (length=10)

I’m trying to recover the data like this:

foreach ( $values as $valor => $chave) {
  echo "Codigo da os insert: ".$chave->os." \n";   
}

But it shows nothing

  • It is an array not an object, so it needs brackets ['chave'] to light and not ->

1 answer

4


You must use square brackets. When you access a position of an array you use "[]", when you access an object you use "->".

foreach ( $values as $valor => $chave) {
  echo "Codigo da os insert: " . $chave['os'] . " \n";   
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.