Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Append to 2-Dimensional Array

Posted on 2008-10-21
4
528 Views
Last Modified: 2012-05-05
I can CREATE (and populate) a 2D array using something like this:

$fruits = array ( "fruits"  => array ( "a" => "orange",
                                       "b" => "banana",
                                       "c" => "apple"
                                       ),
                );

How the heck do I APPEND to an existing 2D array?

$fruits = array ( "fruits"  => array ( "a" => "orange",
                                       "b" => "banana",
                                       "c" => "apple"
                                       ),
                );

Open in new window

0
Comment
Question by:jdana
  • 2
4 Comments
 
LVL 82

Assisted Solution

by:hielo
hielo earned 100 total points
ID: 22772398
0
 
LVL 11

Expert Comment

by:MacAnthony
ID: 22772408
All you need to do is specify the first dimension first and it works like a normal array:

$fruits['fruits'][] = 'mango';
0
 
LVL 11

Accepted Solution

by:
MacAnthony earned 150 total points
ID: 22772419
Or:

$fruits['fruits']['d'] = 'mango';

didn't notice it was an associative array at first.
0
 

Author Closing Comment

by:jdana
ID: 31508555
It's actually straight-forward.  Awesome!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question