29

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 ?

user1248047
  • 305
  • 1
  • 3
  • 6
  • No one should be using `mysql_` functions anymore. See [get array of rows with mysqli result](https://stackoverflow.com/q/1501274/2943403) – mickmackusa Aug 28 '22 at 06:26

6 Answers6

51

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;
   }
Basti
  • 3,998
  • 1
  • 18
  • 21
  • 2
    'the preferred way to add a value to an array is writing $array[] = $value; ' This is profound, thanks, reminding one self of that bit helps keep the program simple – pal4life Apr 30 '15 at 11:16
8

I think you have to go for

$arrayname[indexname] = $value;
Saiyam Patel
  • 1,161
  • 9
  • 18
7

All these answers are nice however when thinking about it....
Sometimes the most simple approach without sophistication will do the trick quicker and with no special functions.

We first set the arrays:

$arr1 = Array(
"cod" => ddd,
"denum" => ffffffffffffffff,
"descr" => ggggggg,
"cant" => 3
);
$arr2 = Array
(
"cod" => fff,
"denum" => dfgdfgdfgdfgdfg,
"descr" => dfgdfgdfgdfgdfg,
"cant" => 33
);

Then we add them to the new array :

$newArr[] = $arr1;
$newArr[] = $arr2;

Now lets see our new array with all the keys:

print_r($newArr);

There's no need for sql or special functions to build a new multi-dimensional array.... don't use a tank to get to where you can walk.

Shlomtzion
  • 674
  • 5
  • 12
  • 1
    Thanks for giving a simple answer, for other readers. (Note that this is the same technique as accepted answer shows. It only looked more complicated *there*, because there it was answered within the context of the question. The key line in accepted answer is: `$res_arr_values[] = $row;`, identical to this answer's `$newArr[] = $arr1;`) – ToolmakerSteve Feb 18 '21 at 20:19
  • Indeed. I agree with every word. :) – Shlomtzion Mar 14 '21 at 21:05
1

Use this..

$res_arr_values = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    $res_arr_values[] = $row;
}
Adrian Gunawan
  • 13,979
  • 11
  • 40
  • 41
-1

first convert your array too JSON

while($query->fetch()){
   $col[] = json_encode($row,JSON_UNESCAPED_UNICODE);
}

then vonvert back it to array

foreach($col as &$array){
   $array = json_decode($array,true);
}

good luck

milad nazari
  • 339
  • 3
  • 5
-2
$res_arr_values = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    $res_arr_values[] = $row;
}


array_push == $res_arr_values[] = $row;

example 

<?php
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);

Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
)
?>
rasvi
  • 13
  • 2
  • 7
    Would you consider adding some narrative to explain why this code works, and what makes it an answer to the question? This would be very helpful to the person asking the question, and anyone else who comes along. – Andrew Barber Mar 11 '13 at 18:46
  • @user1248047 don't wanted to learn array_push function, he/she just to know how prevent to replacing key values – SagarPPanchal Feb 04 '14 at 05:45
  • There are multiple answers written a year before this one, that show the technique. I don't see that this answer adds any value. If I'm wrong, please add an explanation of what is new about this answer. – ToolmakerSteve Feb 18 '21 at 20:25