Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - push array into array - key issue

Tags:

arrays

php

key

push

i am trying to push multiple arrays into 1 big array, resulting in a 2 lvl array.

I got this set of arrays for example:

 Array (     [cod] => ddd     [denum] => ffffffffffffffff     [descr] => ggggggg     [cant] => 3 ) Array (     [cod] => fff     [denum] => dfgdfgdfgdfgdfg     [descr] => dfgdfgdfgdfgdfg     [cant] => 33 ) 

But, after array push, i get this array:

 Array (     [0] => Array         (             [0] => ddd             [1] => ffffffffffffffff             [2] => ggggggg             [3] => 3         )      [1] => Array         (             [0] => fff             [1] => dfgdfgdfgdfgdfg             [2] => dfgdfgdfgdfgdfg             [3] => 33         )  ) 

Basically this is what i want to do, BUT, if you notice after the push, the keys are forgotten, and converted to numeric.

This is what i want it to look like:

 Array (     [0] => Array         (             [cod] => ddd             [denum] => ffffffffffffffff             [descr] => ggggggg             [cant] => 3         )      [1] => Array         (             [cod] => fff             [denum] => dfgdfgdfgdfgdfg             [descr] => dfgdfgdfgdfgdfg             [cant] => 33         )  ) 

sample code im using:

$res_arr_values = array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC))    {        array_push($res_arr_values, array_values($row));    } 

Can someone help me with it ?

like image 403
user1248047 Avatar asked Mar 04 '12 12:03

user1248047


People also ask

Can you push an array into an array php?

Adding array into an array in PHP To add an array into an array in PHP, use the array_push() function. The array_push() function takes an array as an argument and returns the array combining with old and new values.

How do you push a key and value in an array?

Just assign $array[$key] = $value; It is automatically a push and a declaration at the same time.

How do you assign a key to an array?

To add a key/value pair to all objects in an array:Use the Array. forEach() method to iterate over the array. On each iteration, use dot notation to add a key/value pair to the current object.

What is array_push in php?

Definition and Usage. The array_push() function inserts one or more elements to the end of an array. Tip: You can add one value, or as many as you like. Note: Even if your array has string keys, your added elements will always have numeric keys (See example below).


1 Answers

Don't use array_values on your $row

$res_arr_values = array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC))    {        array_push($res_arr_values, $row);    } 

Also, the preferred way to add a value to an array is writing $array[] = $value;, not using array_push

$res_arr_values = array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC))    {        $res_arr_values[] = $row;    } 

And a further optimization is not to call mysql_fetch_array($result, MYSQL_ASSOC) but to use mysql_fetch_assoc($result) directly.

$res_arr_values = array(); while ($row = mysql_fetch_assoc($result))    {        $res_arr_values[] = $row;    } 
like image 129
Basti Avatar answered Sep 19 '22 11:09

Basti