/    Sign up×
Community /Pin to ProfileBookmark

Adding new array to multi-dimensional array

Hi,

If I have a multidimensional array that has been declared like this:

[code=php]
$myarray = array (
array (‘var_a’=>’$value_a’, ‘var_b’=>’$value_b’ ),
array (‘var_c’=>’$value_c’, ‘var_d’=>’$value_d’ ),
array (‘var_e’=>’$value_e’, ‘var_f’=>’$value_f’ )
);
[/code]

, but later on I want to add another array like this to $myarray

[code=php]
array (‘var_g’=>’$value_g’, ‘var_h’=>’$value_h’)
[/code]

How do I do that?

Thanks in advance.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@TJ111Nov 28.2007 — Something like this should work:
[code=php]
$myarray[count($myarray)] = array ('var_g'=>'$value_g', 'var_h'=>'$value_h');
[/code]

Since array indexes start at 0, but array counts start at 1, it'll always place it in its own index.
Copy linkTweet thisAlerts:
@jlietzauthorNov 28.2007 — Thanks! That did work, but isn't there a PHP construct for this? It would seem strange if there wasn't...
Copy linkTweet thisAlerts:
@TJ111Nov 28.2007 — There are two other ways I can think of to do this, that one is just easiest for me to read later down the road if I need to come back to it.
[code=php]
//shorthand
$myarray[] = array ('var_g'=>'$value_g', 'var_h'=>'$value_h');

//array function
array_push($myarray, array ('var_g'=>'$value_g', 'var_h'=>'$value_h'));
[/code]
Copy linkTweet thisAlerts:
@jlietzauthorNov 28.2007 — array_push()!!! Of course :o

Thanks.
×

Success!

Help @jlietz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...