php - 将数组推入数组 - 关键问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9554799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:06:46  来源:igfitidea点击:

php - push array into array - key issue

phparrayskeypush

提问by user1248047

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

我正在尝试将多个数组推送到 1 个大数组中,从而产生一个 2 lvl 数组。

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 ?

有人可以帮我吗?

回答by Basti

Don't use array_valueson your $row

不要array_values在你的$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

此外,向数组添加值的首选方法是写入$array[] = $value;,而不是使用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.

而更进一步的优化不是调用mysql_fetch_array($result, MYSQL_ASSOC)而是mysql_fetch_assoc($result)直接使用。

$res_arr_values = array();
while ($row = mysql_fetch_assoc($result))
   {
       $res_arr_values[] = $row;
   }

回答by Saiyam Patel

I think you have to go for

我想你必须去

$arrayname[indexname] = $value;

回答by Shlomtzion

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.

不需要 sql 或特殊函数来构建新的多维数组......不要使用坦克到达你可以走路的地方。

回答by Adrian Gunawan

Use this..

用这个..

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

回答by rasvi

$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
)
?>

回答by milad

first convert your array too JSON

首先将您的数组也转换为 JSON

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

then vonvert back it to array

然后 vonvert 将其返回到数组

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

good luck

祝你好运