This is the array. After using array_values, I found that it seems to have been reordered according to the ID impolde prompts that the array cannot be used as a parameter, the object is also used, and the json is also converted. How should I go?
renew:
This is enough, but after my new retlist 0 array_colum, the printed result [0] went to the end of the array.
If you want to convert JSON to a PHP array, the method is as follows
First of all, your data format is JSON, which needs to be converted into a PHP array first.
The second parameter ofjson_decode
isTRUE
, which means the key names are retained. Otherwise, after the JSON is converted to the PHP array, the key names of the PHP array will be reordered.Then use
array_column
to process the$a['data']
values in your data, or usearray_values
to rearrange the array values.If you want to convert PHP to JSON, but make
Data
inData
be an array[]
.As long as in the PHP array, first process
$a['Data']
according to the abovearray_column
orarray_values
, and then run it directlyNote that the second parameter of
json_encode
must not be usedJSON_FORCE_OBJECT
, otherwise Data will still become an object{}
.Update
After reading your code, I seem to know what you are going to do. Do you want to find the item in the original array whose ID value is equal to
$makeupId
, and then advance it to the first one in the array? If so, your code will be complicated to write. Through this magical functionarray_column()
, you can easily achieve your needs. The code is as followsEnd of code
Then, let me talk about why you use
array_values()
andarray_column()
to mess up the order. Because these two functions will be renumbered, and the order of their numbers is not the label order of your key names, that is to say, if you manually write the key name as0,1,2,3
, it will not cause it to be renumbered. Time is sorted according to0,1,2,3
. The order in which it is renumbered depends on the order in which your code is run. In other words, your last...[0] = $newarr is not actually at the beginning of the array, but at the end of the entire array. It's just that its key name is 0, so no matter how hard you try, this 0 corresponds to The elements will all run to the end.Update 2
There are many comments and explanations in the above code, here is the pure code:
Update 3
Let’s add some more content. Here are several feasible solutions besides
array_column
.Option 1:
This solution is based on your original code modification.
above)Add
to the second to last line of your code (
$retlist = ...Option 2:
This solution is also based on your original code modification.
Change
$retlist[0] = $newarr;
in your code toarray_unshift($retlist, $newarr);
As mentioned above,
array_column
andarray_values
will be renumbered. The order of renumbering depends on the order in which you define the corresponding values, not the numerical order of the key names, so the above modification is pushed by the array header. The entry replaces the value definition and initialization of$retlist[0]
.Option 3:
This is also modified based on your code.
Before you traverse, define
$retlist[0] = []
, and then when you traverse to an item that matches$makeupId
, directly assign it to$retlist[0]
. In this way, due to the prior definition$retlist[0]
, so even if it is renumbered, its order will still be first.Does this mean that just removing the key value is equivalent to removing one layer of the multi-dimensional array? If so, I wrote a rough example, you can follow this idea to implement it, as shown in the picture above
It is best to convert json data into a php array for easier processing. Otherwise it will be difficult to deal with.