我的代码可以正常工作,但看起来很凌乱,尤其是我需要将foreach循环重复6次,然后将每个循环的输出数组合并到一个多维数组中,下面的代码展示了如何编写循环2次。
$prodo = array();
$uk_array = array();
$de_array = array();
// set counter you want to iterate over.
$j =0;
$step = 5; // set the step value (in your case it will 50.)
$n = count($asins_array)/$step;
$associate_id = "xxxxxxxxxxx";
$access_key = "xxxxxxxxxx";
$secret_key = "xxxxxxxxxx";
for($i = 0 ;$i<$n;$i++)
{
$asin = implode(',',array_slice($asins_array,$j,$step));
$j = $j+$step; // increment counter by to step position.
echo "<pre>";
print_r($asin); // this is the collection you want. save it or use directly
echo "</pre>";
//////////////////// How to put that code from this point to the point idicated below in a function wth arguments/////
$amazon = new AmazonAPI($associate_id , $access_key, $secret_key , "amazon.co.uk");
$item = $amazon->item_lookup($asin);
$url = $amazon->build_url();
echo $url;
foreach($amazon->xml->Items->Item as $item)
{
$amz_asin = (string)$item->ASIN;
$amz_price = number_format($item->OfferSummary->LowestNewPrice->Amount/100, 2);
$amz_title = (string)$item->ItemAttributes->Title;
//$amz_desc = (string)$item->EditorialReviews->EditorialReview->Content;
$amz_feature = (string)$item->ItemAttributes->Feature;
$product_array = array ("UK" => array ("Title" => $amz_title , "Price" => $amz_price));
$uk_array [$amz_asin] = $product_array;
}
/////////////////
$amazon = new AmazonAPI($associate_id , $access_key, $secret_key , "amazon.de");
$item = $amazon->item_lookup($asin);
$url = $amazon->build_url();
echo $url;
foreach($amazon->xml->Items->Item as $item)
{
$amz_asin = (string)$item->ASIN;
$amz_price = number_format($item->OfferSummary->LowestNewPrice->Amount/100, 2);
$amz_title = (string)$item->ItemAttributes->Title;
//$amz_desc = (string)$item->EditorialReviews->EditorialReview->Content;
$amz_feature = (string)$item->ItemAttributes->Feature;
$product_array = array ("DE" => array ("Title" => $amz_title , "Price" => $amz_price));
$de_array [$amz_asin] = $product_array;
///////////////
}
}
$prodo = array_merge_recursive($de_array , $uk_array );
print_r ($prodo);
我试图这样做,但没有成功,当我仅添加以下行
$uk_array [$amz_asin] = amz_xml_array('amazon.co.uk','UK',$asin);
并打印该变量,我会正确地获得英国数组
当我用下面的行替换上面的uk行时,我得到了正确的de数组 $ de_array [$ amz_asin] = amz_xml_array('amazon.de','DE',$ asin);
但是当我添加两行并尝试在循环外使用array_merge_recursive合并它们时,我得到部分合并好的数组数组,并且部分看上去很乱,我试图通过保留uk和de变量来测试它,只打印一个他们中的一些没有合并,我得到了来自de数组的UK数组!
$product_details = array();
function amz_xml_array ($amazon_domain,$arr_country_code,$asin) {
global $product_details;
$associate_id = "xxxxxxxxxx";
$access_key = "xxxxxxxxxx";
$secret_key = "xxxxxxxxxx";
$amazon = new AmazonAPI($associate_id , $access_key, $secret_key , $amazon_domain);
$item = $amazon->item_lookup($asin);
$url = $amazon->build_url();
echo $url;
foreach($amazon->xml->Items->Item as $item)
{
$amz_asin = (string)$item->ASIN;
$amz_price = number_format($item->OfferSummary->LowestNewPrice->Amount/100, 2);
$amz_title = (string)$item->ItemAttributes->Title;
$product_array = array ($arr_country_code => array ("Title" => $amz_title , "Price" => $amz_price)); //Build array from Amazon API XML
$product_details [$amz_asin] = $product_array; //get the array outside the loop
}
return $product_details;
}
$uk_array = array();
$de_array = array();
// set counter you want to iterate over.
$j =0;
$step = 5; // set the step value (in your case it will 50.)
$n = count($asins_array)/$step;
for($i = 0 ;$i<$n;$i++) {
$asin = implode(',',array_slice($asins_array,$j,$step));
$j = $j+$step; // increment counter by to step position.
echo "<pre>";
print_r($asin); // this is the collection you want. save it or use directly
echo "</pre>";
$uk_array [$amz_asin] = amz_xml_array('amazon.co.uk','UK',$asin);
$de_array [$amz_asin] = amz_xml_array('amazon.de','DE',$asin);
}
//$prodo = array_merge_recursive($de_array , $uk_array );
echo "<pre>";
print_r ($uk_array);
echo "</pre>";
所以我想知道我是否做错了什么?!
答案 0 :(得分:0)
由于您没有重新初始化linux
数组,因此您可能会混淆细节。您应该尽量避免一般使用$product_details
,这是为什么一个例子。
而是在每个循环中创建一个新数组...
global