0

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}
1

2 Answers 2

2

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);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! But this will only work as long the ids are in chronological order like 1,2,3,4. 1,5,11,44 will not work as i unterstand your code correctly, right?
I have not tested, but I think it should work with ids that are in any order. The first part, actually puts the data into order according to it's id. It achieves this, by using the id value as the key for the whole row. The second part pulls the values as needed from the now correctly indexed array.
2

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.

3 Comments

Thanks for your answer. The & before $o means this is a copy of the array, right?
No it means that if $o is changed inside the foreach loop, that the original value of it inside $order will be changed. It's a variable alias or reference, see php.net/foreach.
as soon as one item is deleted from the database, the id may not match

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.