How do i add associative array to array in php?
This is not working as it replaces the value of data. How can I have it add an associative value each loop though? Show
MaxiGui 5,8864 gold badges14 silver badges32 bronze badges asked Mar 21, 2011 at 23:11
You can simply do this
If your're running on php 5.4+
answered Sep 8, 2014 at 5:52
4 I think you want Or in case you want an array that maps categories to array of questions:
webvitaly 4,1317 gold badges29 silver badges47 bronze badges answered Mar 21, 2011 at 23:13
ThiefMasterThiefMaster 302k78 gold badges581 silver badges625 bronze badges 2 Before for loop:
Then in your loop:
Uwe Keim 38.7k56 gold badges173 silver badges282 bronze badges answered Mar 21, 2011 at 23:12
moemoe 28.1k3 gold badges18 silver badges16 bronze badges 6 I know this is an old question but you can use:
This will push the
lasec0203 2,2241 gold badge19 silver badges35 bronze badges answered Jun 29, 2017 at 15:54
MikeMike 1,7282 gold badges42 silver badges73 bronze badges For anyone that also need to add into 2d associative array, you can also use answer given above, and use the code like this
you can then call it (to test out the result by:
which should print $question answered Dec 19, 2017 at 7:34
maximranmaximran 4054 silver badges11 bronze badges How do I add data to an associative array?Normally add a new element in an existing associative array it will get appended at the end of that array. print_r( $arr ); ?> So, a new element can not be added directly at the beginning of an associative array but the existing array can be appended at the end of a new array where the first element is the new element.
How do you combine two associative arrays?PHP array_merge() Function. Merge two arrays into one array: $a1=array("red","green"); $a2=array("blue","yellow"); ... . Merge two associative arrays into one array: $a1=array("a"=>"red","b"=>"green"); $a2=array("c"=>"blue","b"=>"yellow"); ... . Using only one array parameter with integer keys: $a=array(3=>"red",4=>"green");. Does In_array work for associative array?in_array() function is utilized to determine if specific value exists in an array. It works fine for one dimensional numeric and associative arrays.
What is associative arrays in PHP explain it with simple PHP code?Associative Array - It refers to an array with strings as an index. Rather than storing element values in a strict linear index order, this stores them in combination with key values. Multiple indices are used to access values in a multidimensional array, which contains one or more arrays.
|