Php add to multidimensional array
I have a multidimensional array $md_array and I want to add more elements to the sub-arrays recipe_type and cuisine coming from a loop that reads data from a table. Show
In the loop, I create a new table $newdata for each row:
and then, using
Whats the syntax for adding a new element (array) to the recipe_type array with array_push? I could never get my head around multidimensional arrays and I'm a bit confused. In the previous pages, we have described arrays that are a single list of key/value pairs. However, sometimes you want to store values with more than one key. For this, we have multidimensional arrays. PHP - Multidimensional ArraysA multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people. The dimension of an array indicates the number of indices you need to select an element.
PHP - Two-dimensional ArraysA two-dimensional array is an array of arrays (a three-dimensional array is an array of arrays of arrays). First, take a look at the following table:
We can store the data from the table above in a two-dimensional array, like this: $cars = array ( Now the two-dimensional $cars array contains four arrays, and it has two indices: row and column. To get access to the elements of the $cars array we must point to the two indices (row and column): Exampleecho $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].". Try it Yourself » We
can also put a Examplefor ($row = 0; $row < 4; $row++) { Row number $row ";echo "
for ($col = 0; $col < 3; $col++) { echo " } echo " } ?> Try it Yourself » Complete PHP Array ReferenceFor a complete reference of all array functions, go to our complete PHP Array Reference. The reference contains a brief description, and examples of use, for each function! Recommended Answers
Jump to Post
Jump to Post
Jump to Post All 10 Replies
blocblue 238 Posting Pro in Training
Featured Poster It really isn't clear from the code you've posted what your starting array structure or desired finished array structure should be. At a guess, you can do the following: If that's incorrect, what exactly are you trying to achieve?
9 Years Ago
THe reason of this is I wanna try to add elements in a 2 dimensional array by assigning a key index then use array_push()
blocblue 238 Posting Pro in Training
Featured Poster Array push can add a value (of any type) to an existing array. If you want to assign values to a specific array index, you need to do it explicitly.
9 Years Ago
mr blocblue does the blank square brackets is the container of the $new_array or not
blocblue 238 Posting Pro in Training
Featured Poster Try just using:
9 Years Ago
two ways of adding values to an array adding values by "push" uses next available int multidimensional array Edited 9 Years Ago by jstfsklh211 7 Years Ago $array = array(); echo array[0] // prints $val1
jkon 535 Posting Whiz in Training Featured Poster 7 Years AgoEdited 7 Years Ago by jkon 6 Years Ago
diafol 6 Years AgoThis thread has become totally unravelled. The precise intent (what type of keys) has not been made clear. Please can we stop posting here as it's becoming a dump for who-knows-what. Thanks. Edited 6 Years Ago by diafol Reply to this topic Be a part of the DaniWeb community We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge. How do you add a multidimensional array?Creating Multidimensional Arrays
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.
Is multidimensional array PHP?A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.
How do you loop through a multidimensional array in PHP?Looping through multidimensional arrays
Just as with regular, single-dimensional arrays, you can use foreach to loop through multidimensional arrays. To do this, you need to create nested foreach loops — that is, one loop inside another: The outer loop reads each element in the top-level array.
How can we store multidimensional array in database using PHP?PHP multidimensional array is also known as array of arrays. It allows you to store tabular data in an array. PHP multidimensional array can be represented in the form of matrix which is represented by row * column.
...
PHP Multidimensional Array Example.. |