从另一个数组的元素创建数组

时间:2014-02-16 02:43:23

标签: php arrays multidimensional-array foreach

我有一个名为array3的数组;

    Array
(
    [0] => Commercial
    [1] => Infrastructure
)

我想迭代这个数组的每个元素,并根据值创建单个数组。我试过这个;

$array4 = array();
$array5= array();
foreach ($array3 as $value) {
  $array4[] = array('v' => count(search($rows, 'domaindesc', $value)));
  $array4[] = array('v' => $value);
  $array5[] = array('c' => $array4);

}

搜索功能返回来自另一个数组的加工元素的数量。我从上面的代码得到的输出是

Array
(
    [0] => Array
        (
            [c] => Array
                (
                    [0] => Array
                        (
                            [v] => 5
                        )

                    [1] => Array
                        (
                            [v] => Commercial
                        )

                )

        )

    [1] => Array
        (
            [c] => Array
                (
                    [0] => Array
                        (
                            [v] => 5
                        )

                    [1] => Array
                        (
                            [v] => Commercial
                        )

                    [2] => Array
                        (
                            [v] => 1
                        )

                    [3] => Array
                        (
                            [v] => Infrastructure
                        )

                )

        )

)

我想要的是;

Array
(
    [0] => Array
        (
            [c] => Array
                (
                    [0] => Array
                        (
                            [v] => 5
                        )

                    [1] => Array
                        (
                            [v] => Commercial
                        )

                )

        )

    [1] => Array
        (
            [c] => Array
                (                    

                    [0] => Array
                        (
                            [v] => 1
                        )

                    [1] => Array
                        (
                            [v] => Infrastructure
                        )

                )

        )

)

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

每次迭代,你都要添加到$ array4并将其放入$ array5

尝试每次迭代重新声明$ array4:

$array5 = array();
foreach ($array3 as $value) {
  $array4 = array();
  $array4[] = array('v' => count(search($rows, 'domaindesc', $value)));
  $array4[] = array('v' => $value);
  $array5[] = array('c' => $array4);

}

编辑:对于风格,我个人会这样写:

$array5 = array();
foreach ($array3 as $value) {
    $array5[] = array('c' => array(
        array('v' => count(search($rows, 'domaindesc', $value))),
        array('v' => $value),
    ));
}

请注意,数组声明中的尾随逗号是有意的,如果需要,其他开发人员可以更轻松地向声明中添加更多项。 PHP允许这样做,我利用它。

EDIT2:如果你不担心你的PHP代码不适用于5.4之前的PHP版本,我更喜欢用方括号来声明一个数组而不是array()。最终看起来更干净:

$array5 = [];
foreach ($array3 as $value) {
    $array5[] = ['c' => [
        ['v' => count(search($rows, 'domaindesc', $value))],
        ['v' => $value],
    ]];
}