在数组PHP中组合索引

时间:2018-07-03 20:38:05

标签: php

我似乎不太了解如何组合该数组中的索引。以下是数组的示例。任何帮助,资源或方向将不胜感激。

  $array_one = array(
    10 => array(0 => 2/3-AM),
    10 => array(0 => AUT-PR),
    1195 => array(0 => 1/2-AM),
    1258 => array(0 => GR-1),
    1195 => array(0 => 1/7-PM),
    );

我希望它看起来像这样:

   $array_one = array(
    10 => array(0 => 2/3-AM, AUT-PR),
    1195 => array(0 => 1/2-AM, 1/7-PM),
    1258 => array(0 => GR-1),
    );

var_dump

1 个答案:

答案 0 :(得分:1)

从屏幕快照中进行假设,我认为您的意思是输入数组为:

$input = array(
    array(10 => array(0 => '2/3-AM')),
    array(10 => array(0 => 'AUT-PR')),
    array(1195 => array(0 => '1/2-AM')),
    array(1258 => array(0 => 'GR-1')),
    array(1195 => array(0 => '1/7-PM')),
);

要将其设置为目标格式:

$output = [];
foreach ($input as $keys) {
    foreach ($keys as $key => $values) {
        foreach ($values as $value) {
            $output[$key][] = $value;
        }
    }
}
var_dump($output);

结果是:

array(3) {
    [10]=> array(2) {
        [0]=> string(6) "2/3-AM"
        [1]=> string(6) "AUT-PR"
    }
    [1195]=> array(2) {
        [0]=> string(6) "1/2-AM"
        [1]=> string(6) "1/7-PM"
    }
    [1258]=> array(1) {
        [0]=> string(4) "GR-1"
    }
}