[英]Grabbing specific values from a multi-dimensional array in PHP
I'm new to programming and I'm tackling arrays. 我是编程新手,正在处理数组。 I thought I had multi-dimensional arrays figured out but I guess I don't. 我以为我想出了多维数组,但我想没有。 Here's the code I'm working on: 这是我正在处理的代码:
$loopcounter = 0;
while ($myrow = mysql_fetch_array($results)) {
//...other stuff happens...
$allminmax[$loopcounter][] = array("$myrow[3]","$currentcoltype","$tempmin","$tempmax");
$loopcounter++;
} }
This chunk of code is supposed to create an array of four values ($myrow[3], currentcoltype, tempmin, tempmax) and insert it into another array every time the loop goes through. 这段代码应该创建一个包含四个值的数组($ myrow [3],currentcoltype,tempmin,tempmax),并在每次循环通过时将其插入另一个数组。 When I do this: 当我这样做时:
echo implode($allminmax);
I get: 我得到:
ArrayArrayArrayArrayArrayArrayArrayArrayArray
Do I need to implode each array before it goes into the master array? 在进入主阵列之前,我是否需要内爆? I really want to be able to do something like $allminmax[0][4] and get the $tempmax for the first row. 我真的希望能够执行类似$ allminmax [0] [4]的操作,并获得第一行的$ tempmax。 When I try this now nothing happens. 现在,当我尝试执行此操作时,什么也没有发生。 Thanks -- any help is appreciated! 谢谢-感谢您的帮助!
It looks like you should either use [$loopcounter]
or []
, but not both. 看来您应该使用[$loopcounter]
或[]
,但不能同时使用两者。 You also should drop the quotes. 您还应该删除引号。 They're unnecessary and in the case of "$myrow[3]"
they interfere with the variable interpolation. 它们是不必要的,在"$myrow[3]"
的情况下,它们会干扰变量插值。
$allminmax[] = array($myrow[3], $currentcoltype, $tempmin, $tempmax);
By the way, arrays are zero-indexed, so to get $tempmax
for the first row it'd be $allminmax[0][3]
not $allminmax[0][4]
. 顺便说一句,数组是零索引,因此要获得$tempmax
第一行这将会是$allminmax[0][3]
不$allminmax[0][4]
Also, a better way to display the contents of your array is with print_r
or var_dump
. 另外,显示数组内容的更好方法是使用print_r
或var_dump
。 These will display arrays within arrays while a simple echo
will not. 这些将显示数组中的数组,而简单的echo
则不会。
var_dump($allminmax);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.