Answer the question
In order to leave comments, you need to log in
PHP. How to reorder elements in a SimpleXMLElement Object?
There is an initial SimpleXMLElement Object object of this type. I receive from a third-party service via API. The task is to change the order at the row level. The remaining levels are unchanged
now as follows [row] => Array ( [0 =>...val0...] ,[1 =>...val1...],[2 =>...val2... ] )
you need something like this [row] => Array ( [0=>...val1...] ,[1=>...val2...],[2=>...val0...] )
the number of elements, types, data will not change, only the order of values in the row array. The new order is given by another array of the same size. The new order is set manually, i.e. does not fit into any sorting algorithm.
it turned out to make an array through json_encode and change the order of elements in it
$brands_json = json_encode($brands);
$brands_array = json_decode($brands_json,TRUE);
But collecting a new SimpleXMLElement Object from an array of the same structure as the original one does not work.
I try as described here
Perhaps there is an easier way to solve my problem?
SimpleXMLElement Object
(
[row] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[brand] => ABARTH
[code] => CFiat112013
[frameexample] =>
)
[features] => SimpleXMLElement Object
(
[feature] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[example] => ZFA31200000451262
[name] => vinsearch
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => quickgroups
)
)
)
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[brand] => ALFA ROMEO
[code] => RFiat112013
[frameexample] =>
)
[features] => SimpleXMLElement Object
(
[feature] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[example] => ZAR93700003090729
[name] => vinsearch
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => quickgroups
)
)
)
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[brand] => AUDI
[code] => AU1161
[frameexample] =>
)
[features] => SimpleXMLElement Object
(
[feature] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[example] => WAUZZZ4M0HD042149
[name] => vinsearch
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => wizardsearch2
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => quickgroups
)
)
)
)
)
Answer the question
In order to leave comments, you need to log in
using array_multisort I will get an array with a new order. And so it happened after a couple of foreach.
So I realized that for objects there is no such function as array_multisort, which would also produce an object.
The question is how to collect a new object with such nesting from an array.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question