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}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…