按另一个数组的值对Php数组的数据进行排序
I've two PHP Arrays. The first one contains a sort order. The second one contains the data which i need to sort. I have no idea how to solve it…
What I'm trying to get is a list, sorted by the values of the first array (order.txt). Any suggestions?
<li>Item [2]</li>
<li>Item [1]</li>
<li>Item [3]</li>
Order
Array
(
[0] => 2
[1] => 1
[2] => 3
)
Data
Array
(
[0] => Array
(
[id] => 1
[name] => 00134258.jpg
[size] => 2787
)
[1] => Array
(
[id] => 2
[name] => 80132454.jpg
[size] => 2667
)
[2] => Array
(
[id] => 3
[name] => 13134218.jpg
[size] => 2787
)
)
Here are the code which produces the arrays above:
<?php
$order = file('order.txt');
foreach ($order as $key => $value) {
$order = json_decode($value, true);
}
print_r($order);
$file = file('db.txt');
foreach ($file as $key => $value) {
$file_data[] = json_decode($value, true);
}
print_r($file_data);
?>
This are the json strings:
order.text
{"0":"2","1":"1","2":"3"}
db.txt
{"id":"1","name":"00134258.jpg","size":2787}
{"id":"2","name":"80132454.jpg","size":2667}
{"id":"3","name":"13134218.jpg","size":2787}
我有两个PHP数组。 第一个包含排序顺序。 第二个包含我需要排序的数据。 我不知道如何解决它... p>
我想要得到的是一个列表,按第一个数组(order.txt)的值排序。 有什么建议吗? p>
&lt; li&gt; Item [2]&lt; / li&gt;
&lt; li&gt; Item [1]&lt; / li&gt;
&lt; li&gt; Item [3]&lt; / li&gt;
code> pre>
订单 h3>
Array
(
[0] = &gt; 2
[1] =&gt; 1
[2] =&gt; 3
)
code> pre>
数据 h3>
Array
(
[0] =&gt; Array
(
[id] =&gt; 1
[name] =&gt; 00134258.jpg
[size] =&gt; 2787
)
[1] =&gt;数组
(
[id] =&gt; 2
[名称] =&gt; 80132454.jpg
[size] =&gt; 2667
)
[2] =&gt;数组
(
[id] =&gt; 3
[name] =&gt; 13134218.jpg
[size] =&gt; 2787
)
)
code> pre>
以下是产生上述数组的代码: p>
&lt;?php
$ order = file('order.txt');
foreach($ order as $ key =&gt; $ value){
$ order = json_decode($ value,true);
}
print_r($ order);
$ file = file('db .txt');
foreach($ file as $ key =&gt; $ value){
$ file_data [] = json_decode($ value,true);
}
print_r($ file_data);
?&gt;
code> pre>
\ n 这是json字符串: p>
order.text h3>
{“0”:“2”,“1” : “1”, “2”: “3”}
代码> PRE>
db.txt H3>
{“编码 “:” 1" , “名称”: “00134258.jpg”, “大小”:2787} \ N { “ID”: “2”, “名称”: “80132454.jpg”, “大小”:2667} \ n {“id”:“3”,“name”:“13134218.jpg”,“size”:2787}
code> pre>
div>
Make a new array where the keys are the id of the data, then loop through your order array and assign the values to an ordered array...
<?php
// loop through the file data
foreach($file_data as $v){
// assign values to new array with the data id as it's key
$identified[$v['id']] = $v;
}
// loop through the order array
foreach($order as $v){
// pull the data values from the identified array by their key
$ordered[] = $identified[$v];
}
// check it has all worked out as planned ;)
print_r($ordered);
?>
Alternatively... in line with @hakre's method, first create array ordered by index, and then use array_multisort method.
<?php
foreach($file_data as $v){
$ordered[$v['id']] = $v;
}
array_multisort($order, $ordered);
print_r($ordered);
?>
From your $order
array substract 1 from each value, then use array_multisort
:
foreach($order as &$o) $o--;
unset($o);
array_multisort($order, $data);
This works as long as the id
value is always one higher than it's offset in $data
.