簡體   English   中英

如何遍歷此數組?

[英]How do I loop through this array?

好吧,我對使用PHP數組還比較陌生,我擁有Follow數組,我需要遍歷該數組2280和2307是標識符。

我發現很難提出一個收集所有數據的foreach()。

Array
(
[2280] => Array
    (
        [0] => http://deals.com.au//uploads/deal_image/2706.jpg
        [1] => Yuan's Massage and Beauty
        [2] => Get Hair Free in CBD! Only $99 for SIX MONTHS worth of the latest in IPL Permanent Hair Reduction. Choose which area you want treated! Valued at $900 from Yuan's Massage and Beauty in the Heart of Melbourne's CBD. Limited vouchers available
        [3] => 99
        [4] => 900
        [5] => 801
        [6] => http://deals.com.au/1827
    )

[2307] => Array
    (
        [0] => http://deals.com.au//uploads/deal_image/2683.jpg
        [1] => Name Necklace Australia
        [2] => Style yourself like SJP! Only $29 for a STERLING Silver Name Necklace plus get FREE delivery! Valued at $75 with NameNecklace.com.au
        [3] => 29
        [4] => 75
        [5] => 46
        [6] => http://deals.com.au/Melbourne
    )
)

您的數組片段

$array = array( // foreach ($array as $k=>$subarray)
'2280' /* this is your $k */ => 
    array( /* and this is your $subarray */
        'http://deals.com.au//uploads/deal_image/2706.jpg',

現在,您獲得了所需的數據(由於數組的值是數組,因此必須使用嵌套的foreach ):

foreach ($array as $k=>$subarray) {
    foreach ($subarray as $data) {
        echo $data;//of subarray
    }
}

UPDATE

OP對我的回答的評論問題:

如果我想選擇選擇性而不是進行大量數據轉儲,該怎么辦。 如果回顯$ data,如何訪問特定行?

好吧,在大多數情況下,您應該在數組中關聯鍵和數據(在PHP中我們稱其為關聯數組)。

范例1:

$hex_colors = array('FF0000', '00FF00' '0000FF');

值不與對應的鍵關聯。 PHP將為數組元素本身分配0,1,2 ...鍵。 在這種情況下,您將使用由PHP 1鍵自動分配的綠色的十六進制值: echo $hex_colors[1]; // 00FF00 echo $hex_colors[1]; // 00FF00 ,當然您應該肯定知道。 通常,當您具有嚴格的數據結構(例如array(red, green, bluee) ,可以使用此方法,但是在大多數情況下,最好使用以下方法:

范例2:

$hex_colors = array('red'=>'FF0000', 'green'=>'00FF00' 'blue'=>'0000FF');

十六進制顏色表示與適當的鍵關聯echo $hex_colors['green']; // 00FF00 echo $hex_colors['green']; // 00FF00

如果您的數組是:

$array = array(
    '2280' => array(
        'image_url' => 'http://deals.com.au//uploads/deal_image/2706.jpg',
        'product_name' => 'Yuan\'s Massage and Beauty',
        'description' => 'Get Hair Free in CBD! Only $99 for SIX MONTHS worth of the latest in IPL Permanent Hair Reduction. Choose which area you want treated! Valued at $900 from Yuan's Massage and Beauty in the Heart of Melbourne's CBD. Limited vouchers available',
        'price' => 99,
        'weight_brutto' => 900,
        'weight_netto' => 801,
        'dealer_store' => 'http://deals.com.au/1827',
        ...

您將可以使用“人類可讀”鍵來訪問數據:

foreach ($array as $id=>$product) {
    echo '<a href="http://myshop.com/?product_id='.$id.'">Buy '.$product['name'].'</a>';
    echo '<img class="product_thumbnail" src="'.$product['image_url'].'" />';
    echo 'Description: '.$product['description'];
    echo 'The price is '.number_format($product['price']);
    ...
}

它非常適合從數據庫中讀取行,例如:

while ($data = mysql_fetch_array($query_result_link, MYSQL_ASSOC)) { 
// MYSQL_ASSOC flag is not necessary
// PHP will use this flag by default for mysql_fetch_array function
// keys of $data will be correspondent columns names returned by mysql query
    echo $data['id'];
    echo $data['title'];
    ...
}

繼續學習PHP ,當您更方便地關聯鍵和值時,會發現更多情況。

foreach ($arr1 as $key1 => $value1) {

    //$key1==2280; $value1 is the array
    foreach ($value1 as $key2 => $value2) {
        //$key2==0;$value2="http://deals.com.au//uploads/deal_image/2706.jpg"
    }

}

暫無
暫無

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

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