简体   繁体   English

删除多维数组的索引以转换为多个一维 arrays PHP 的列表

[英]Remove indexes of multidimensional array to convert to a list of many one dimensional arrays PHP

Apologies for vague questioning, I'm fairly new.为模糊的提问道歉,我是相当新的。 I've been searching but can't seem to find the solution that fits my scenario.我一直在寻找,但似乎找不到适合我的方案的解决方案。

I am trying to store the output of a looped mysql query as a variable to be used outside the loop, exactly as it would be if I were to print_r the result within the loop.我正在尝试将循环 mysql 查询的 output 存储为要在循环外使用的变量,就像我要在循环内打印结果一样。 I am trying to save on overhead as I have inherited a function which uses this large dataset frequently and am trying to reduce the calls to the database by saving the output instead of querying database each time.我试图节省开销,因为我继承了经常使用这个大型数据集的 function,并试图通过保存 output 而不是每次都查询数据库来减少对数据库的调用。

Currently I have -目前我有 -

$data = array();
$sql = mysqli_query($con, SELECT * FROM my_table);
while($row = mysqli_fetch_assoc($sql)){
  $data[] = $row;
}
print_r($data);

which is resulting in:这导致:

Array ( 
[0] => Array ( [id] => 1 [name] => john [age] => 44  ) 
[1] => Array ( [id] => 2 [name] => paul [age] => 30  )
[2] => Array ( [id] => 3 [name] => george [age] => 25  )
)

BUT i am needing an output like this, ie without top indexes...但我需要一个像这样的 output,即没有顶级索引......

$output =

Array ( [id] => 1 [name] => john [age] => 44  ) 
Array ( [id] => 2 [name] => paul [age] => 30  )
Array ( [id] => 3 [name] => george [age] => 25  ) ;

It is late and my brain eludes me, many thanks for any pointers.已经很晚了,我的大脑躲开了我,非常感谢您的任何指点。

EDIT: Will take a mulligan on this one, turns out my problem was a formatting one where I did not escape ($) sign when assigning it as a variable whilst saving the output to file.编辑:将对此进行调度,结果发现我的问题是格式化问题,在将 output 保存到文件时将其分配为变量时我没有转义 ($) 符号。 Thanks all for your replies and setting me straight谢谢大家的回复,让我直截了当

The result and the expected result is bascially the same thing because as @Nick pointed out you need to have top level keys (0,1,2).结果和预期结果基本相同,因为正如@Nick 指出的那样,您需要拥有顶级键(0,1,2)。 (How would you else identify which id belongs to john and to paul and to george?) (否则,您将如何确定哪个 id 属于 john、paul 和 george?)

It's simply not possible to store an array like this:根本不可能存储这样的数组:

Array ( [id] => 1 [name] => john [age] => 44  ) 
Array ( [id] => 2 [name] => paul [age] => 30  )
Array ( [id] => 3 [name] => george [age] => 25  )

It must be stored like this: (with top level keys (in this case:0,1 and 2))它必须像这样存储:(使用顶级键(在本例中:0,1 和 2))

Array ( 
    [0] => Array ( [id] => 1 [name] => john [age] => 44  ) 
    [1] => Array ( [id] => 2 [name] => paul [age] => 30  )
    [2] => Array ( [id] => 3 [name] => george [age] => 25  )
)

The value of $data[0]['name'] would be john, the value of $data[1]['name'] would be paul etc. $data[0]['name']的值是 john, $data[1]['name']的值是 paul 等等。

If you refer to $data[0] it contains the array: Array ( [id] => 1 [name] => john [age] => 44 )如果您引用$data[0]它包含数组: Array ( [id] => 1 [name] => john [age] => 44 )

If you refer to $data[1] it contains the array: Array ( [id] => 2 [name] => paul [age] => 30 ) etc如果您引用$data[1]它包含数组: Array ( [id] => 2 [name] => paul [age] => 30 )

It's not possible to store the values as you want but it is of course possible to have the output as you wish:无法根据需要存储值,但当然可以根据需要使用 output:

//$data_value is an array in every row 
foreach($data as $data_value) {
    print_r($data_value);
}

which would produce the output:这将产生 output:

Array
(
    [id] => 1
    [name] => john
    [age] => 44
)
Array
(
    [id] => 2
    [name] => paul
    [age] => 30
)
Array
(
    [id] => 3
    [name] => george
    [age] => 25
)

Do this:做这个:

$data = [];
$i = 0;
while ($row = mysqli_fetch_assoc($sql)) {
    foreach( $row as $field => $value) {
        $data[$i][$field] = $value:
    }
    $i++;
}

print_r($data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM