user2378120
user2378120

Reputation: 31

how to append to a three dimensional array

I have this array in php:

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

I need to append something to the array to get it to look like this:

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

);

I tried array push but I was unable to get it to work properly. Any help would be appreciated.

Upvotes: 0

Views: 46

Answers (3)

J-Dizzle
J-Dizzle

Reputation: 3191

$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 another add_image

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

add a image_title

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

after both of those operations you end up with:

$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
        ),
    ),
);

Upvotes: 0

Charlotte Dunois
Charlotte Dunois

Reputation: 4680

You don't really need a function, just do it like this:

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

Upvotes: 1

Jeff Lambert
Jeff Lambert

Reputation: 24661

Try something like this:

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

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

Upvotes: 0

Related Questions