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
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.
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'];
}
$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
Simply use array_walk
like as
$result = array();
array_walk($arr,function($v)use(&$result){
$result[$v->title] = $v->title;
});
print_r($result);
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