将订单列添加到多维数组

时间:2017-05-16 17:34:31

标签: php

在PHP 7中,如果我有这个数组:

$array = [
    ["name" => "Jon", "age" => 44],
    ["name" => "Bob", "age" => 32],
    ["name" => "Jim", "age" => 103],
    ["name" => "Ted", "age" => 19]
];

处理这个数组最优雅的方法是添加一个额外的列,表明谁是最年轻的最年轻的人...

[
    ["name" => "Jon", "age" => 44, "order" => 2],
    ["name" => "Bob", "age" => 32, "order" => 3],
    ["name" => "Jim", "age" => 103, "order" => 1],
    ["name" => "Ted", "age" => 19, "order" => 4]
]

3 个答案:

答案 0 :(得分:1)

这里我们使用多个函数来获得所需的输出。 array_column用于提取列age,我们正在使用arsort($columns);然后getting array_values对列进行反向排序,然后我们正在翻转数组以获取其年龄顺序使用array_flip,最后我们使用array_maparray_merge来迭代并在数组中添加一列。

Try this code snippet here

<?php
ini_set('display_errors', 1);
$array = [
    ["name" => "Jon", "age" => 44],
    ["name" => "Bob", "age" => 32],
    ["name" => "Jim", "age" => 103],
    ["name" => "Ted", "age" => 19]
];
$columns=  array_column($array, "age");//obtaining column age
arsort($columns);//sorting column in reverse

$column=array_flip(array_values($columns));//getting order for age
$result=array_map(function($value) use(&$column){
    $value=  array_merge($value,array("order"=>$column[$value["age"]]+1));//adding age column to array by adding index with 1
    return $value;

}, $array);
print_r($result);

<强>输出:

Array
(
    [0] => Array
        (
            [name] => Jon
            [age] => 44
            [order] => 2
        )

    [1] => Array
        (
            [name] => Bob
            [age] => 32
            [order] => 3
        )

    [2] => Array
        (
            [name] => Jim
            [age] => 103
            [order] => 1
        )

    [3] => Array
        (
            [name] => Ted
            [age] => 19
            [order] => 4
        )

)

答案 1 :(得分:1)

不会撒谎,我只是想使用新的太空船运营商。这不会添加新列,而是按年龄对数组进行排序。

<?php

$array = [
    ["name" => "Jon", "age" => 44],
    ["name" => "Bob", "age" => 32],
    ["name" => "Jim", "age" => 103],
    ["name" => "Ted", "age" => 19]
];

usort($array, function ($a, $b) { 
    return ($a["age"] <=> $b["age"]) * -1; 
});

print_r($array);

答案 2 :(得分:0)

如果没有参考点来确定哪个是最老的用户,您要么进行多循环比较,要么进行排序然后插入。

Multi loop comparasin看起来像

<?php
$array = [
  ["name" => "Jon", "age" => 44],
  ["name" => "Bob", "age" => 32],
  ["name" => "Jim", "age" => 103],
  ["name" => "Ted", "age" => 19]
];

$count = count($array);

for($index = 0; $index < $count; ++$index) {
  $order = 1;
  $age = $array[$index]["age"];
  for($index2 = 0; $index2 < $count; ++$index2) {
    if($array[$index2]["age"] > $age) {
      ++$order;
    }
  }

  $array[$index]["order"] = $order;
}

echo "<pre>";
var_dump($array);
echo "</pre>";

排序然后插入将涉及array_walkuasort

来自docs

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");

function test_alter(&$item1, $key, $prefix)
{
  $item1 = "$prefix: $item1";
}

function test_print($item2, $key)
{
  echo "$key. $item2<br />\n";
}

echo "Before ...:\n";
array_walk($fruits, 'test_print');

array_walk($fruits, 'test_alter', 'fruit');
echo "... and after:\n";

array_walk($fruits, 'test_print');

docs

<?php
// Comparison function
function cmp($a, $b) {
  if ($a == $b) {
    return 0;
  }
  return ($a < $b) ? -1 : 1;
}

// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
print_r($array);

// Sort and print the resulting array
uasort($array, 'cmp');
print_r($array);

因此,对于您的示例,它将是这样的:

<?php
function walkFunc(&$item, $key) {
  $item["order"] = $key + 1;
}

// as @waterloomatt mentioned the Spaceship operator is meant for this
function sortComp($a, $b) {
  return ($a["age"] <=> $b["age"]) * -1;
}

$array = [
  ["name" => "Jon", "age" => 44],
  ["name" => "Bob", "age" => 32],
  ["name" => "Jim", "age" => 103],
  ["name" => "Ted", "age" => 19]
];

uasort($array, 'sortComp');
array_walk($array, 'walkFunc');

echo "<pre>";
var_dump($array);
echo "</pre>";