Andrew Novikov
Andrew Novikov

Reputation: 109

PHP Sort associative multidimensional array by another array

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")
);

Upvotes: 0

Views: 96

Answers (3)

Timmetje
Timmetje

Reputation: 7694

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
        )

)

Upvotes: 0

Progrock
Progrock

Reputation: 7485

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',
  ),
)

Upvotes: 0

arkascha
arkascha

Reputation: 42915

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

Upvotes: 2

Related Questions