如何附加到三维数组

时间:2014-09-15 18:46:05

标签: php arrays multidimensional-array

我在php中有这个数组:

    $field_data = array(
    'add_image' => array(
        array(
            'image_caption' => $caption,
            'upload' => $attachname,
        ),
    ),
  );

我需要在数组中添加一些东西,使它看起来像这样:

$field_data = array(
'add_image' => array(
    array(
        'image_caption' => $caption,
        'upload' => $attachname,
    ),
   array(
        'image_caption' => $caption,
        'upload' => $attachname,
    ),
),

);

我尝试了数组推送,但我无法让它正常工作。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

你真的不需要一个功能,就这样做:

$field_data['add_image'][] = $to_append_array;

答案 1 :(得分:0)

尝试这样的事情:

$newEntry = array(
    'image_caption' => $caption,
    'upload' => $attachname,
);

$field_data['add_image'][] = $newEntry;

答案 2 :(得分:0)

$field_data = array(
    'add_image' => array(
        1 = > array(  //you may not realize,but this array has key 1 and value array()
            'image_caption' => $caption,
            'upload' => $attachname
        ),
    ),
);

添加另一个add_image

$field_data['add_image'][] = array('image_caption' => $caption2, 'upload' => $attachname2);

添加image_title

$field_data['add_image'][1]['image_title'] = "Picture of House";

在完成这两项操作之后,您最终得到:

$field_data = array(
    'add_image' => array(
        1 = > array(
            'image_caption' => $caption,
            'upload' => $attachname,
            'image_title' => "Picture of House"
        ),
        2 = > array(
            'image_caption' => $caption2,
            'upload' => $attachname2
        ),
    ),
);