Hey all,

I am having one heck of a time wrapping my mind around pushing new array elements into a multidimensional array.

I have an array setup:


$results = array	(
				array	(
					ID	=>	"0",
					site_name	=>	"site 1",
					state	=>	"AA",
					URL	=>	"http://url.com",
					numbers	=>	array (
								S1_num	=>	"12",
								S2_num	=>	"10"
							),
					Contact	=>	array	(
								First	=>	"Bob",
								Last	=>	"Smith",
								Email	=>	"bob_smith@yahoo.com"
							),
					Type	=>	"1",
					UserName	=>	"username",
					Password	=>	"password"
				)
);

and I want to be able to push in new results from a DB query. I've been reading and from what I can find array_push(); should be able to get the job done. but I am not sure if I have to push data into each array dimension at a time and then push those arrays into my main array. Any help with syntax would be greatly appreciated.

    Yes, you could use array_push(), but you could also use:

    $results[] = array("etc", "etc");

    I am not sure if I have to push data into each array dimension at a time and then push those arrays into my main array.

    It depends on how you want to construct each inner array. You could create an empty array and then populate it, or you could create variables and then create and populate the array with them. Since you are retrieving results from a database you probably already have an array (the current row of the result set) and you may be able to use that.

      thanks Laser... I ended up creating an array for "numbers" and "contact" and then pushing that into my main array with

      $results[] = array();

      . I am doing it this way because I am pulling info from multiple tables and want to be able to display and manipulate it with fewer db queries.

        I am doing it this way because I am pulling info from multiple tables and want to be able to display and manipulate it with fewer db queries.

        Incidentally, you should try using joins, subqueries, aggregate functions, etc, before doing the manipulation in PHP.

          Write a Reply...