Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-在assoc数组中排序数组_Php_Arrays_Sorting - Fatal编程技术网

PHP-在assoc数组中排序数组

PHP-在assoc数组中排序数组,php,arrays,sorting,Php,Arrays,Sorting,我保证我已经看过很多关于PHP排序的现有SO Q,包括 我有一个PHP关联数组,以字符串作为键。每个值都是一个整数数组。我想对每个整数数组进行简单的升序排序。我相信这应该很容易,我已经找到了足够多的例子,我认为我应该做正确的事情,但它不太有效,所以有一个打字错误,或者我是个白痴或者其他什么 PHP: //Each fruit corresponds to an array (series) of integers $data = [ 'banana' => [ 1,

我保证我已经看过很多关于PHP排序的现有SO Q,包括

我有一个PHP关联数组,以字符串作为键。每个值都是一个整数数组。我想对每个整数数组进行简单的升序排序。我相信这应该很容易,我已经找到了足够多的例子,我认为我应该做正确的事情,但它不太有效,所以有一个打字错误,或者我是个白痴或者其他什么

PHP:

//Each fruit corresponds to an array (series) of integers
$data = [
    'banana' => [
        1,3,2
    ],
    'orange' => [
        5,1,3
    ]
];

echo "Before sort:\n";
var_dump($data);

//For each fruit, I want to order the numbers
foreach ($data as $key => $series)
{
    //Sort array of integers
    sort($series);

    //NB I wasn't sure about value/reference details of foreach loops, so I also tried
    //retrieving a series into a variable, sorting, and then reassigning back to the same key
}

echo "\n\nAfter sort:\n";
var_dump($data);
Before sort:
array(2) {
  'banana' =>
  array(3) {
    [0] =>
    int(1)
    [1] =>
    int(3)
    [2] =>
    int(2)
  }
  'orange' =>
  array(3) {
    [0] =>
    int(5)
    [1] =>
    int(1)
    [2] =>
    int(3)
  }
}


After sort:
array(2) {
  'banana' =>
  array(3) {
    [0] =>
    int(1)
    [1] =>
    int(3)
    [2] =>
    int(2)
  }
  'orange' =>
  array(3) {
    [0] =>
    int(5)
    [1] =>
    int(1)
    [2] =>
    int(3)
  }
}
输出:

//Each fruit corresponds to an array (series) of integers
$data = [
    'banana' => [
        1,3,2
    ],
    'orange' => [
        5,1,3
    ]
];

echo "Before sort:\n";
var_dump($data);

//For each fruit, I want to order the numbers
foreach ($data as $key => $series)
{
    //Sort array of integers
    sort($series);

    //NB I wasn't sure about value/reference details of foreach loops, so I also tried
    //retrieving a series into a variable, sorting, and then reassigning back to the same key
}

echo "\n\nAfter sort:\n";
var_dump($data);
Before sort:
array(2) {
  'banana' =>
  array(3) {
    [0] =>
    int(1)
    [1] =>
    int(3)
    [2] =>
    int(2)
  }
  'orange' =>
  array(3) {
    [0] =>
    int(5)
    [1] =>
    int(1)
    [2] =>
    int(3)
  }
}


After sort:
array(2) {
  'banana' =>
  array(3) {
    [0] =>
    int(1)
    [1] =>
    int(3)
    [2] =>
    int(2)
  }
  'orange' =>
  array(3) {
    [0] =>
    int(5)
    [1] =>
    int(1)
    [2] =>
    int(3)
  }
}

如您所见,在输出中,整数的内部数组尚未排序。我做错了什么?(PHP 5.5.9,Windows 7)

使用参考

foreach ($data as $key => &$series)
{
    //Sort array of integers
    sort($series);
    // OR
    // sort($data[$key]);
}

啊,真管用,快管用!我之所以没有这样做,是因为我发现至少有一位评论者反对它,总的来说,因为事情的表现“出乎意料”。现在找不到。@frumious嗯,不能说我听说过。。。也许在某些情况下,我也试过$temp=$data[$key];排序($temp)$日期[$key]=$temp-您是否希望它起作用?似乎没有。编辑实际上是现在,想知道我做错了什么…@frumious
sort($data[$key])