数组合并保持相同的顺序?

时间:2013-10-10 21:26:19

标签: php arrays

我有一个数组,它是一个数组数组,按日期排序,如下:

Array
(
    [1379167200] => Array
        (
            [110] => Introduction to Banking | Saturday, September 14, 2013 - 10:00am
        )

    [1380376800] => Array
        (
            [71] => Saving, Investing, Debt | Saturday, September 28, 2013 - 10:00am
        )

    [1381588200] => Array
        (
            [72] => Setting Personal Goals | Saturday, October 12, 2013 - 10:30am
        )

    [1382796000] => Array
        (
            [74] => Type of Account: What's Right for You? | Saturday, October 26, 2013 - 10:00am
        )

    [1384009200] => Array
        (
            [81] => Creating an Account: Learning to Budget | Saturday, November 09, 2013 - 10:00am
        )
)

我想保持排序(即按日期排序),但最好只在单个数组中包含最内部的数组项。像这样:

Array
(
        [110] => Introduction to Banking | Saturday, September 14, 2013 - 10:00am

        [71] => Saving, Investing, Debt | Saturday, September 28, 2013 - 10:00am

        [72] => Setting Personal Goals | Saturday, October 12, 2013 - 10:30am

        [74] => Type of Account: What's Right for You? | Saturday, October 26, 2013 - 10:00am

        [81] => Creating an Account: Learning to Budget | Saturday, November 09, 2013 - 10:00am
)

这怎么可能?我需要将整数转换为字符串或其他东西吗?

我绝对需要按日期维护排序,但我还需要保持整数键和值之间的关系,两者都被使用。

1 个答案:

答案 0 :(得分:1)

遍历数组,创建新数组:

$newArray = Array();
foreach($array as $row) { // $row is subarray
   $value = current($row); // first value in subarray "Itroduction to ..."
   $key = key($row); // first key in subarray 101, 71, 72
   $newArray[$key] = $value;
}

演示;

文档: