0

I have an array of order $order. I want to sort an associative array by this array by the article field. The point is to sort also other elements in this array such as title by that order. If there are more elements in the array than in sorting array, then leave them in their place as shown in the example below:

Input data:

$data = array(
    "title" => array("title 1", "title 2", "title 3", "title 4", "title 5", "title 6"),
    "article" => array("01", "02", "03", "04", "05", "06")
);

$order = ["03", "01", "04", "02"];

Expected result:

$data = array(
    "title" => array("title 3", "title 1", "title 4", "title 2", "title 5", "title 6"),
    "article" => array("03", "01", "04", "02", "05", "06")
);
1
  • Hi Andrew, what is your question exactly? Commented Oct 22, 2019 at 20:57

3 Answers 3

2

This is a solution, but it is as ugly as the quality of the initial data you have:

<?php
$input = [
    "title" => ["title 1", "title 2", "title 3", "title 4", "title 5", "title 6"],
    "article" => ["01", "02", "03", "04", "05", "06"]
];
$order = ["03", "01", "04", "02"];
$output = [];

// pick ordered items
foreach($order as $article) {
    $position = array_search($article, $input['article']);
    foreach (array_keys($input) as $key) {
        $output[$key][] = $input[$key][$position];
    }
}

// fill in the rest
foreach ($input['article'] as $position => $article) {
    foreach (array_keys($input) as $key) {
        if (!in_array($article, $order)) {
            $output[$key][] = $input[$key][$position];
        }
    }
}

var_dump($output);

The output obviously is:

array(2) {
  ["title"]=>
  array(6) {
    [0]=>
    string(7) "title 3"
    [1]=>
    string(7) "title 1"
    [2]=>
    string(7) "title 4"
    [3]=>
    string(7) "title 2"
    [4]=>
    string(7) "title 5"
    [5]=>
    string(7) "title 6"
  }
  ["article"]=>
  array(6) {
    [0]=>
    string(2) "03"
    [1]=>
    string(2) "01"
    [2]=>
    string(2) "04"
    [3]=>
    string(2) "02"
    [4]=>
    string(2) "05"
    [5]=>
    string(2) "06"
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Provided you have the same number of items in each, we can build a map of old positional keys to new positionals keys, and then use array_multisort to sort these into place, and sort the corresponding arrays in the same order.

<?php
$data = array(
    "title" => array("title 1", "title 2", "title 3", "title 4", "title 5", "title 6"),
    "article" => array("01", "02", "03", "04", "05", "06")
);

$new_order = ["03", "01", "04", "02"];
$old_order = $data['article'];

// Build a map of before and after key positions.
foreach($old_order as $k => $value) {
    $key = array_search($value, $new_order);
    if($key === false) // No positional change.
        $key = $k;
    $map[$k] = $key; // old position -> new position
}

array_multisort($map, $data['title'], $data['article']);
var_export($data);

Output:

array (
  'title' => 
  array (
    0 => 'title 3',
    1 => 'title 1',
    2 => 'title 4',
    3 => 'title 2',
    4 => 'title 5',
    5 => 'title 6',
  ),
  'article' => 
  array (
    0 => '03',
    1 => '01',
    2 => '04',
    3 => '02',
    4 => '05',
    5 => '06',
  ),
)

Comments

0

If you want some shorter code and less loops:

$properOrderedArray = array_replace(array_flip($data["article"]),array_flip($order));
array_map(function($title,$article,$properOrderedArray) use (&$newOrder){
    $newOrder['title'][$properOrderedArray]=$title;
    $newOrder['article'][$properOrderedArray]=$article;
},$data['title'],$data['article'],$properOrderedArray);
ksort($newOrder['title']);
ksort($newOrder['article']);
print_r($newOrder);

Output:

Array
(
    [title] => Array
        (
            [0] => title 3
            [1] => title 1
            [2] => title 4
            [3] => title 2
            [4] => title 5
            [5] => title 6
        )

    [article] => Array
        (
            [0] => 03
            [1] => 01
            [2] => 04
            [3] => 02
            [4] => 05
            [5] => 06
        )

)

Comments

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.