将多个数组值附加到单个值中

时间:2016-09-21 22:52:43

标签: php arrays

我有两个PHP数组,并且想要将第一个数组的值附加到第二个数组以创建一个新数组,该数组重复原始顺序但结合了两个值。我已经尝试过PHP array_merge,但这只是附加新数组但不合并为单个值。

Array
(
    [0] => Array
        (
            [title] => Item 1
        )
    [1] => Array
        (
            [title] => Item 2
        )
    [2] => Array
        (
            [title] => Item 3
        )
    [3] => Array
        (
            [title] => Item 4
        )
)

第二个数组;

Array
(
    [0] => Array
        (
            [count] => 3
        )
    [1] => Array
        (
            [count] => 6
        )
    [2] => Array
        (
            [count] => 9
        )
    [3] => Array
        (
            [count] => 2
        )
)

已完成阵列;

Array
(
    [0] => Array
        (
            [title_count] => Item 1 (3)
        )
    [1] => Array
        (
            [title_count] => Item 2 (6)
        )
    [2] => Array
        (
            [title_count] => Item 3 (9)
        )
    [3] => Array
        (
            [title_count] => Item 4 (2)
        )
)

1 个答案:

答案 0 :(得分:1)

只需使用foreach循环。

$result = array();
foreach ($array1 as $i => $element) {
    $title = $element['title'];
    $count = $array2[$i]['count'];
    $result[] = array('title_count' => "$title ($count)");
}