简体   繁体   English

PhP多维数组将索引连接在一起

[英]PhP Multidimensional Array concating indexes together

Let me start off by saying that I am fairly new to php. 让我先说我对php很新。 I am trying to create a multidimensional array but running into a weird result when I look at the result. 我正在尝试创建一个多维数组,但在查看结果时会遇到一个奇怪的结果。 I have a feeling that the syntax I am using is not correct but cannot verify that anywhere online. 我有一种感觉,我使用的语法不正确,但不能在任何地方验证。 So I am posing the question here. 所以我在这里提出这个问题。

Here is the gist of what I am trying to do: 以下是我要做的事情的要点:

// Given: $row["foo"] == "Hello" && $row["bar"] == 1

while($row = mysqli_fetch_array($query_result, MYSQLI_ASSOC)){
    $multiArray[$i] = $row["foo"];
    $multiArray[$i]["lorem"] = $row["bar"];
    $i++;   
}

When I go to print $multiArray[$i] I get: 1ello . 当我去打印$multiArray[$i]我得到: 1ello

As I said I believe that the error lies in my syntax of how I am assigning this multidimensional array. 正如我所说,我相信错误在于我如何分配这个多维数组的语法。 Can someone please help me find a similar method to (what I can only guess) php madness above? 有人可以帮我找一个类似的方法(我只能猜到)上面的php疯狂吗?

Thank you in advance! 先感谢您!

You can't combine data types in a same array value. 您无法在同一数组值中组合数据类型。

$multiArray[$i] only can have a value type, ( string, integer, or array), but in your code, you want an array and a string at the same time ! $multiArray[$i]只能有一个值类型,(字符串,整数或数组),但在你的代码中,你需要一个数组和一个字符串!

A possible solution is ( but depends of your logic ) : 一个可能的解决方案是(但取决于您的逻辑):

while($row = mysqli_fetch_array($query_result, MYSQLI_ASSOC)){
    $multiArray[$i]["lorem_string"] = $row["foo"];
    $multiArray[$i]["lorem"] = $row["bar"];
    $i++;   
}

Because first you assign $multiArray[$i] = $row["foo"]; 因为首先你分配$multiArray[$i] = $row["foo"]; so $multiArray[$i] will be a string with an example value Hello , then you assign the string's lorem index with $multiArray[$i]["lorem"] = $row["bar"]; 所以$multiArray[$i]将是一个带有示例值Hello的字符串,然后用$multiArray[$i]["lorem"] = $row["bar"];分配字符串的lorem索引$multiArray[$i]["lorem"] = $row["bar"]; . lorem evaulates to 0 , so the output will ve 1ello lorem评估为0 ,因此输出将为1ello

Edit : a fresh version of PHP should produce a warning too. 编辑 :PHP的新版本也应该产生警告。

You are essentially first assigning $multiArray[$i] a string "hello", then trying to assign an array to it on the next line. 你基本上首先为$ multiArray [$ i]分配一个字符串“hello”,然后尝试在下一行为它分配一个数组。

Instead you need the first assignment to be to an array, which can then contain strings or ints or more arrays or whatever 相反,您需要第一个赋值为一个数组,然后可以包含字符串或整数或更多数组或其他任何数组

Also, doing things explicitly in this case will also help you understand what is going on. 此外,在这种情况下明确地做事也将帮助您了解正在发生的事情。

For example: 例如:

$i = 0; // explicit initialization of $i
while($row = mysqli_fetch_array($query_result, MYSQLI_ASSOC)){
    $multiArray[$i] = array(); // explicit initialization of array
    $multiArray[$i]["foo"] = $row["foo"];
    $multiArray[$i]["lorem"] = $row["bar"];
    $i++;   
}

Then if you want "hello" echo $multiArray[$i]["foo"]; 那么如果你想要“你好” echo $multiArray[$i]["foo"]; and if you want 1 echo $multiArray[$i]["lorem"]; 如果你想要1 echo $multiArray[$i]["lorem"]; .

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

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