11

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

Ponzio Pilato
  • 315
  • 1
  • 5
  • 18
  • 3
    Possible duplicate of [Convert PHP object to associative array](http://stackoverflow.com/questions/4345554/convert-php-object-to-associative-array) – Chetan Ameta Dec 23 '15 at 04:40
  • If I use json function I obtain this array Array ( [0] => Array ( [title] => primo ) [1] => Array ( [title] => secondo ) ) – Ponzio Pilato Dec 23 '15 at 04:40
  • it's an array of objects merged into an associative array, not just an object to an array, might not be a duplicate – Chris Trudeau Dec 23 '15 at 04:45

6 Answers6

21

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.

Pupil
  • 23,834
  • 6
  • 44
  • 66
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

Ponzio Pilato
  • 315
  • 1
  • 5
  • 18
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'];
}
Waqas Shahid
  • 1,051
  • 1
  • 7
  • 22
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

Chris Trudeau
  • 1,427
  • 3
  • 16
  • 20
1

Simply use array_walk like as

$result = array();
array_walk($arr,function($v)use(&$result){ 
      $result[$v->title] = $v->title;
});
print_r($result);
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
-2

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;
    }
}
  • 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 object – Nico Haase Nov 23 '19 at 12:57
  • 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. – Khoubeib Bouthour Nov 24 '19 at 16:29