I have the following object:
'new_value' =>
'name' => 'Teste',
'key' => 'TESTE',
'icon' => 'empty',
And the following array:
array(
0 => 'name',
1 => 'icon',
2 => 'key',
)
However I would like to arrange the properties in the object based in the value of the array, so the result would be:
'new_value' =>
'name' => 'Test',
'icon' => 'empty',
'key' => 'TEST',
How can I achieve this? I couldn't find anything on google related to this, only how to sort an array of objects.
My Answer:
array(
'name' => 'John',
'age' => 30,
'city' => 'New York'
);
I also have an array that specifies the order in which I want the object properties to be displayed:
$order = array('city', 'name', 'age');
To order the object properties based on the values in the $order array, you can create a new object with the properties in the desired order. Here's how you can do it:
php
$new_order = new stdClass();
foreach ($order as $key) {
$new_order->$key = $new_value[$key];
}
print_r($new_order);
This code will create a new object $new_order with the properties in the order specified by the $order array. The output will be:
stdClass Object
(
[city] => New York
[name] => John
[age] => 30
)
Rate this post
3 of 5 based on 8981 votesComments