簡體   English   中英

難以理解PHP數組結構

[英]Difficulty understanding a PHP array structure

我正在尋找操縱從MySQL查詢生成的數組。 print_r格式返回以下內容:

數組([cols] =>數組([0] =>數組([標號] =>時間[類型] =>數字)[1] =>數組([標號] =>數量[類型] =>數字)) [rows] =>數組([0] =>數組([c] =>數組([0] =>數組([v] => 8.8)[1] =>數組([v] => 3)) )[1] =>數組([c] =>數組([0] =>數組([v] => 8.2 )[1] =>數組([v] => 4 )))[2] =>數組([c] =>數組([0] =>數組([v] => 7.3)[1] =>數組([v] => 1)))[3] =>數組([c] = >數組([0] =>數組([v] => 5.7)[1] =>數組([v] => 3)))[4] =>數組([c] =>數組([0] =>數組([v] => 4.9)[1] =>數組([v] => 2)))[5] =>數組([c] =>數組([0] =>數組([v ] => 2.9)[1] =>數組([v] => 1)))[6] =>數組([c] =>數組([0] =>數組([v] => 1.6)[ 1] =>數組([v] => 1)))))

為了更好地理解結構,我將此輸出放入數組美化器中: http//phillihp.com/toolz/php-array-beautifier/

然而,即使有這個更漂亮的版本,我也不確定如何訪問特定元素。 例如,我如何從這個數組中返回上面加粗的“8.2”,“4”對? 任何洞察這種結構將不勝感激。

這是生成此數組的代碼:

$return_structure = array(
    'cols' => array (
       // array('label' => 'name', 'type' => 'string'),
        array('label' => 'time', 'type' => 'number'),
        array('label' => 'quantity', 'type' => 'number')
    ),
    'rows' => array()
);

while($row = mysql_fetch_assoc($result)) {
        $return_structure['rows'][] = array('c' => array(
        //array('v' => $row['name']),
        array('v' => $row['time']),
        array('v' => $row['quantity']),
            ));

在此先感謝您的任何幫助!! -Daniel

快速獲得“漂亮”視圖的好方法是使用html“pre”標記在Web瀏覽器中查看它:

echo "<pre>";
print_r($array);

訪問多維關聯數組時,您可以:

1)使用循環遍歷所有內容,您可能需要嵌套循環來完成此任務。 即使使用一堆foreach循環,這也會變得相當混亂和繁瑣。

就像是:

foreach ($arrayDImension as $subArray) {
    foreach($subArray as $row -> $value) {
      //access the rows & values here
    }
}

要么

2)直接訪問它們,假設您知道索引的名稱等。您可以像這樣訪問它們:

print_r($array['rows'][3]['c'][0]['v']); // gives you 8.2

print_r($array['rows'][4]['c'][5]['v']); //gives you 4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM