PHP - 动态生成的数组元素()?

时间:2012-03-11 02:19:03

标签: php arrays

1。我的阵列

我有一个返回数组数组的函数:

function show_array() {
   $myArray = array(
        array(
            'foo' => 'bar',
            'bar' => 'foo',
            'aaa' => 'bbb'),
        array(
            'foo' => 'bar2',
            'bar' => 'foo',
            'aaa' => 'bbb'),
        array(
            'foo' => 'bar3',
            'bar' => 'foo',
            'aaa' => 'bbb'),
        array(
            'foo' => 'bar4',
            'bar' => 'foo',
            'aaa' => 'bbb'),
        //I want to add additional elements here using foreach

   );
   return $myArray;
}

2。要动态添加的元素

如上面的评论中所述,我想基于foreach循环向$ myArray添加一些额外的元素,这是一个简单的函数,不返回任何内容,但显示了我想要插入的内容:

$addToMyArray = array('one','two','three');

foreach($addToMyArray as $newElement) {
    array(
        'foo' => $newElement,
        'bar' => 'foo',
        'aaa' => 'bbb',
    );
}

3。期望的结果

所以最后show_array()应该返回:

        array(
            array(
                'foo' => 'bar',
                'bar' => 'foo',
                'aaa' => 'bbb'),
            array(
                'foo' => 'bar2',
                'bar' => 'foo',
                'aaa' => 'bbb'),
            array(
                'foo' => 'bar3',
                'bar' => 'foo',
                'aaa' => 'bbb'),
            array(
                'foo' => 'bar4',
                'bar' => 'foo',
                'aaa' => 'bbb'),
            //added stuff
            array(
                'foo' => 'one,
                'bar' => 'foo',
                'aaa' => 'bbb'),
            array(
                'foo' => 'two,
                'bar' => 'foo',
                'aaa' => 'bbb'),
            array(
                'foo' => 'three,
                'bar' => 'foo',
                'aaa' => 'bbb'),

       );

我试图将新选项作为$ myArray []返回,对它们执行array_push然后执行array_merge,但似乎没有任何工作,我也无法在$ myArray数组中放置任何循环(很明显)。但是show_array()永远不会返回生成的元素。

应该如何做?

2 个答案:

答案 0 :(得分:3)

你只需要在循环内追加$myArray

$addToMyArray = array('one','two','three');
foreach($addToMyArray as $newElement) {
    $myArray[] = array(
        'foo' => $newElement,
        'bar' => 'foo',
        'aaa' => 'bbb',
    );
}

答案 1 :(得分:2)

您的意思是否与此相似?

function show_array() {
   $myArray = array(
        array(
            'foo' => 'bar',
            'bar' => 'foo',
            'aaa' => 'bbb'),
        array(
            'foo' => 'bar2',
            'bar' => 'foo',
            'aaa' => 'bbb'),
        array(
            'foo' => 'bar3',
            'bar' => 'foo',
            'aaa' => 'bbb'),
        array(
            'foo' => 'bar4',
            'bar' => 'foo',
            'aaa' => 'bbb'),
    //I want to add additional elements here using foreach
   );

    $addToMyArray = array('one','two','three');

    foreach($addToMyArray as $newElement) {
        $myArray[] = array(
            'foo' => $newElement,
            'bar' => 'foo',
            'aaa' => 'bbb',
        );
    }

   return $myArray;
}

或类似的东西:

function compileArray($values) {
    myArray = array();
    foreach($values as $newElement) {
        $myArray[] = array(
            'foo' => $newElement,
            'bar' => 'foo',
            'aaa' => 'bbb',
        );
    }

   return $myArray;
}

$result = array_merge(show_array(),compileArray(array('one','two','three')));

或者,进一步增加灵活性:

function createElementArray($value) {
    return array(
        'foo'=>$value,
        'bar'=>'foo',
        'aaa'=>'bbb'
    );
}

$result = array_merge(
    show_array(),
    array_map(createElementArray, array('one','two','three'))
);

为了使封装在函数中的单个数组的代码可以轻松互换。