如何遍历此数组?

时间:2011-08-07 04:50:47

标签: php loops foreach

我对PHP数组的使用相对较新,我需要循环遍历此数组的跟随数组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
    )
)

2 个答案:

答案 0 :(得分:4)

您的数组代码段

$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
    }
}

<强>更新

OP对我的回答发表评论:

  

如果我想要有选择性而不是获得大量数据转储怎么办?   如果我回显$ data我如何访问特定的行?

好吧,在大多数情况下,您应该将数组中的键和数据关联起来(我们在PHP中将其称为关联数组)。

示例1:

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

值与对应的密钥不相关。 PHP将为数组元素本身分配0,1,2 ...键。在这种情况下,您将使用PHP 1键echo $hex_colors[1]; // 00FF00自动分配获得绿色的十六进制值,当然您应该知道它。通常,当您拥有像array(red, green, bluee)这样的严格数据结构时,会使用此方法,但在大多数情况下,您最好使用以下方法:

示例2:

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

十六进制颜色表示与相应的键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,当关键和值关联更方便时,您会发现更多情况。

答案 1 :(得分:1)

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"
    }

}