嗯,我对 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
)
)
您的数组片段
$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对我的回答提出评论问题:
如果我想要有选择性而不是获得大量数据转储怎么办? 如果我 echo $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
,你会发现更多情况下关联键和值会更方便。
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"
}
}