foreach($key_doc_count as $item) {
mb_language('Japanese');
$product = $item["key"];
$product_url = 'https://search.rakuten.co.jp/search/mall/'.urlencode($product) . '/';
$source = file_get_contents($product_url);
$source = mb_convert_encoding($source, 'utf8', 'auto');
$rakuten_search_html = str_get_html($source);
$count=0;
foreach ($rakuten_search_html->find('img._verticallyaligned') as $item_image) {
if(strlen($item_image->alt > 2))
{
$ss['image_url'] = $item_image->src;
$ss['title'] = $item_image->alt;
$items_kk[] = $ss;
$count++;
if($count <5)
{
break;
}
}
}
$new_item["term"] = $item["key"];
$new_item["current_count"] = $item["doc_count"];
$new_item["results"] = $terms_kk;
$new_word_array[] = $new_item;
}
var_dump($new_word_array);
我试图在数组名称$ ss中插入产品的url和title,然后将该数组分配给$ new_term [“result”]。
但它不起作用 错误是HTTP ERROR 500
答案 0 :(得分:0)
变量$items_kk
和$new_word_array
似乎尚未在foreach循环之外初始化。尝试在循环之前将它们初始化为空数组:
$new_word_array = [];
foreach($key_doc_count as $item) {
mb_language('Japanese');
$product = $item["key"];
$product_url = 'https://search.rakuten.co.jp/search/mall/'.urlencode($product) . '/';
$source = file_get_contents($product_url);
$source = mb_convert_encoding($source, 'utf8', 'auto');
$rakuten_search_html = str_get_html($source);
$count=0;
$items_kk = [];
foreach ($rakuten_search_html->find('img._verticallyaligned') as $item_image) {
if(strlen($item_image->alt > 2))
{
$ss['image_url'] = $item_image->src;
$ss['title'] = $item_image->alt;
$items_kk[] = $ss;
$count++;
if($count <5)
{
break;
}
}
}
$new_item["term"] = $item["key"];
$new_item["current_count"] = $item["doc_count"];
$new_item["results"] = $terms_kk;
$new_word_array[] = $new_item;
}
var_dump($new_word_array);
此外,您还要设置$items_kk
并将$terms_kk
分配给新阵列。 可能是错误?