简体   繁体   English

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

[英]Append multiple array values into single value

I have two PHP arrays, and would like to append the value of the first array to the second array to create a new array which reiterates the original order but combines both values. 我有两个PHP数组,并想将第一个数组的值附加到第二个数组以创建一个新数组,该数组重申原始顺序,但将两个值组合在一起。 I have tried the PHP array_merge but this just appends the new array but not merge into single values. 我已经尝试了PHP array_merge,但这只是追加了新数组,而没有合并为单个值。

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

Second array; 第二个数组;

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

Completed array; 完成的数组;

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)
        )
)

Just use a foreach loop. 只需使用一个foreach循环。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM