maloney
maloney

Reputation: 1653

PHP extract values from array

I'm trying to extract values from an array within an array. The code I have so far looks like this:

$values = $request->request->get('form');
$statusArray = array();
foreach ($values->status as $state) {
    array_push($statusArray, $state);
}

The result of doing a var_dump on the $values field is this:

array (size=2)   
   'status' => 
      array (size=2)
        0 => string 'New' (length=9)  
        1 => string 'Old' (length=9)    
   'apply' => string '' (length=0)

When running the above I get an error basically saying 'status' isn't an object. Can anyone tell me how I can extract the values of the array within 'status'??

Upvotes: 1

Views: 173

Answers (1)

Ende Neu
Ende Neu

Reputation: 15773

-> it's the notation to access object values, for arrays you have to use ['key']:

foreach ($values['status'] as $state) {
  array_push($statusArray, $state);
}

Object example:

class Foo {
  $bar = 'Bar';
}

$foo = new Foo();
echo $foo->bar // prints "bar"

Upvotes: 4

Related Questions