Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array_push into a multi-dimensional array

I have a array with a key value you on it like:

$some_array['array_key'] = "some string";

Is it possible to use array_push to add more elements to the array?

Ive tried this:

array_push($some_array['array_key'],"another string");

and I have tried other obvious way, but nothing seems to work. Is it possible to add array_push into a array with key value?

Thanks for any help you can offer,

--Bryan

like image 544
bryan sammon Avatar asked Feb 02 '11 04:02

bryan sammon


People also ask

How do you make a multidimensional array?

You can create a multidimensional array by creating a 2-D matrix first, and then extending it. For example, first define a 3-by-3 matrix as the first page in a 3-D array. Now add a second page. To do this, assign another 3-by-3 matrix to the index value 2 in the third dimension.

How do you merge multidimensional arrays?

Merging a multidimensional array is the same as that of a simple array. It takes two arrays as input and merges them. In a multidimensional array, the index will be assigned in increment order and will be appended after the parent array. Now, let's take the example of an employee who has two addresses.

How do you create a multidimensional array in Java?

data_type[][] array_name = new data_type[x][y]; For example: int[][] arr = new int[10][20];


1 Answers

If you want $some_array['array_key'] to be an array of values, you have to initialize it as an array, like this:

$some_array['array_key'] = array('some string');

Only then can you use array_push() or the [] = notation:

$some_array['array_key'][] = 'another string';
like image 53
BoltClock Avatar answered Sep 27 '22 22:09

BoltClock