Convert stdClass object to associative array in php
Asked Answered
B

6

12

I need to convert this array

Array ( 
[0] => stdClass Object 
     ( [title] => primo ) 
[1] => stdClass Object 
     ( [title] => secondo )) 

to

Array ( 
[primo] => primo
[secondo] => secondo ) 

Tried different options, including typecast, still not found the correct solution

Banbury answered 23/12, 2015 at 4:36 Comment(3)
Possible duplicate of Convert PHP object to associative arrayLandlady
If I use json function I obtain this array Array ( [0] => Array ( [title] => primo ) [1] => Array ( [title] => secondo ) )Banbury
it's an array of objects merged into an associative array, not just an object to an array, might not be a duplicateWillena
L
23

Use json_encode() and json_decode()

$arr = json_decode(json_encode($yourObject), TRUE);

json_decode() 's second parameter is set to TRUE.

Function definition:

mixed json_decode ( string $json [, bool $assoc = false [, int $depth > = 512 [, int $options = 0 ]]] )

That will convert your object into an associative array.

Liverwurst answered 23/12, 2015 at 4:39 Comment(1)
Simple solutions are why I love you Stackoverflow!Goliath
B
2

Finally I did it this:

$options = array('' => '<select>');
$results = $query->execute()->fetchAll();
foreach($results as $id => $node) {
  $value = $node->title;
  $options[$value] = $value;
}

Thanks for all your answer

Banbury answered 23/12, 2015 at 6:4 Comment(0)
D
1

Check this code please, I haven't debugged it...

$array = array_values($array);
$new_array = array();
foreach($array as $row){
   $new_array[$row['title']] = $row['title'];
}
Diageotropism answered 23/12, 2015 at 4:39 Comment(0)
W
1
$final_array = array();

foreach ($items as $item)
{
    $final_array = array_merge($final_array, json_decode(json_encode($item), true);
}

Where $items is the name of your array. Should go through your array of objects, convert that object to an associative array, and merge it into the $final_array

Willena answered 23/12, 2015 at 4:41 Comment(0)
S
1

Simply use array_walk like as

$result = array();
array_walk($arr,function($v)use(&$result){ 
      $result[$v->title] = $v->title;
});
print_r($result);
Saltation answered 23/12, 2015 at 5:55 Comment(0)
C
-1

To blindly answer the title of the thread, you can achieve object conversion to an associative array by simply casting it:

$array = (array) $object;

However, in the discussed example, rudimentary operations can help generate the desired data structure without using any built-in function:

$array = [];
foreach ($arrayOfObjects as $object) {
    $title = $object->title ?? null;
    if (!is_null($title)) {
        $array[$title] = $title;
    }
}
Clearwing answered 23/11, 2019 at 0:18 Comment(2)
Are you sure about this? I don't think an array of objects will get casted to an array where the keys are set to some arbitrary element of the objectZicarelli
I was not trying to answer the specific case evoked by the question author as there is no built in function for that. There are infinite data structures and we can't create predefined methods to satisfy all. I just wanted to share a hint that can be used by the community if someone looks up the title of the thread.Clearwing

© 2022 - 2024 — McMap. All rights reserved.