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

3
  • 3
    Possible duplicate of Convert PHP object to associative array Commented Dec 23, 2015 at 4:40
  • If I use json function I obtain this array Array ( [0] => Array ( [title] => primo ) [1] => Array ( [title] => secondo ) ) Commented Dec 23, 2015 at 4:40
  • it's an array of objects merged into an associative array, not just an object to an array, might not be a duplicate Commented Dec 23, 2015 at 4:45

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Simple solutions are why I love you Stackoverflow!
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

Comments

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'];
}

Comments

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

Comments

1

Simply use array_walk like as

$result = array();
array_walk($arr,function($v)use(&$result){ 
      $result[$v->title] = $v->title;
});
print_r($result);

Comments

-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;
    }
}

2 Comments

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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.