Transform array of objects into just one array

0

I have a array with multiple objects :

array(3) {
  [0]=>
  object(stdClass)#5750 (2) {
    ["value"]=>
    string(16) "[email protected]"
    ["key"]=>
    string(18) "email_client"
  }
  [1]=>
  object(stdClass)#5254 (2) {
    ["value"]=>
    string(20) "gold"
    ["key"]=>
    string(19) "package"
  }
  [2]=>
  object(stdClass)#6074 (2) {
    ["value"]=>
    string(5) "senh4"
    ["key"]=>
    string(18) "password"
  }
}

And I need you to stay:

array = {
   email_client => [email protected],
   package => gold,
   password => senha
}

I tried with foreach but it does not access the objects. With array_walk_recursive the answer is the same. Can someone help me?

    
asked by anonymous 04.11.2016 / 19:30

2 answers

2

If the array is always the same depth you can do:

$array = array();
foreach ($arr as $obj){

$array[$obj->key] = $array[$obj->value];

}
    
04.11.2016 / 19:39
0

Why not use the objects themselves?

foreach($arr as $obj) {
    echo $obj->key . ': ' . $obj->value; 
}

You can also cast cast to array at the time of access, if you want to use array syntax too much (I do not see much sense in that):

foreach($arr as $obj) {
    echo (array)$obj['key'] . ': ' . (array)$obj['value']; 
}
    
04.11.2016 / 19:35