2011-04-14 93 views
1

認爲我錯過了一個基本概念。我想通過遍歷幾個不同的數據數組來生成html。他們不使用數字作爲索引,因此數字循環不起作用。我無法弄清楚如何在這裏使用foreach()。索引不是數字時,如何遍歷$price$description數組遍歷非數值索引

樣品:

$traverser= 0; 
while($traverser < $number_of_records) 
{ 
    print $traverser . " - " . $price[$traverser] . "<br />"; 
    print $description[$traverser]; 
    $traverser++; 
} 

陣列結構的部分樣本:

object(phpQueryObject)#2799 (13) { ["documentID"]=> string(32) "1d62be942498df890cab4ccb78a007a2" ["document"]=> &object(DOMDocument)#3 (0) { } ["charset"]=> &string(5) "utf-8" ["documentWrapper"]=> &object(DOMDocumentWrapper)#2 (17) { ["document"]=> &object(DOMDocument)#3 (0) { } ["id"]=> string(32) "1d62be942498df890cab4ccb78a007a2" ["contentType"]=> string(9) "text/html" ["xpath"]=> &object(DOMXPath)#4 (0) { } ["uuid"]=> int(0) ["data"]=> array(0) { } ["dataNodes"]=> array(0) { } ["events"]=> array(0) { } ["eventsNodes"]=> array(0) { } ["eventsGlobal"]=> array(0) { } ["frames"]=> array(0) { } ["root"]=> &object(DOMElement)#5 (0) { } ["isDocumentFragment"]=> &bool(true) ["isXML"]=> bool(false) ["isXHTML"]=> bool(false) ["isHTML"]=> bool(true) ["charset"]=> &string(5) "utf-8" } ["xpath"]=> &object(DOMXPath)#4 (0) { } ["elements"]=> array(560) { [0]=> object(DOMElement)#2239 (0) { } [1]=> object(DOMElement)#2240 (0) { } [2]=> object(DOMElement)#2241 (0) { } [3]=> object(DOMElement)#2242 (0) { } [4]=> object(DOMElement)#2243 (0) { } [5]=> object(DOMElement)#2244 (0) { } [6]=> object(DOMElement)#2245 (0) { } [7]=> object(DOMElement)#2246 (0) { } [8]=> object(DOMElement)#2247 (0) { } 
+0

你能告訴我們你的數組是什麼樣子嗎? – 2011-04-14 17:24:28

+0

'foreach'似乎是合乎邏輯的解決方案,你的數組看起來像什麼(一個例子)? – jeroen 2011-04-14 17:25:17

+0

在數組中添加了var_dump的部分樣本。如果我在數組上執行像count()這樣的正常函數,它將返回正確數量的索引。 – JMC 2011-04-14 17:29:18

回答

2

因爲它看起來像您需要的數組鍵爲好,因爲你引用多個不同的陣列,你想要的$a as $k => $v語法foreach

foreach($description as $key => $desc) 
{ 
    print $key . " - " . $price[$key] . "<br />"; 
    print $desc; 
} 
+0

我在嘗試時遇到了這個錯誤:「可捕捉的致命錯誤:類DOMElement的對象無法轉換爲第23行/var/www/price.php中的字符串」。這是可能的,而不是第一次轉換時,處理一個完整的對象數組? – JMC 2011-04-14 17:56:35

+0

這可能意味着在嘗試「打印」它們之前,您需要轉換$ price或$ description中的值。 – Amber 2011-04-14 18:08:55

0

我不知道我得到的問題,但它真的很簡單:

<?php 
$array = array('foo', 'bar'); 
foreach ($array as $element) { 
    echo "{$element}\n"; 
} 

這應該輸出「foo」和「bar」。

1

您可以take your pic爲你想怎麼迭代其中:

<?php 

    $ary = array( // demo array 
    'apple' => 'Apple', 
    'orange' => 'Orange', 
    'grape' => 'Grape' 
); 

    // show the structure 
    var_dump($ary); echo "\r\n"; 

    // use a foreach with the key and value 
    foreach ($ary as $key => $val) 
    printf("%s => %s\r\n", $key, $val); 
    echo "\r\n"; 

    // just get the raw keys 
    $keys = array_keys($ary); 
    var_dump($keys); echo "\r\n"; 

輸出:

array(3) { 
    ["apple"]=> 
    string(5) "Apple" 
    ["orange"]=> 
    string(6) "Orange" 
    ["grape"]=> 
    string(5) "Grape" 
} 

apple => Apple 
orange => Orange 
grape => Grape 

array(3) { 
    [0]=> 
    string(5) "apple" 
    [1]=> 
    string(6) "orange" 
    [2]=> 
    string(5) "grape" 
} 

總是有array_map & array_walk