我在匹配nodeValue
和$property
的{{1}}时遇到了一些麻烦。它实际上必须顺利进行,但我在某个地方犯了错误。我想不通。
$value
数据包含在foreach ($links as $getContent){
$getContentData = pageContent($getContent);
$getRouteData = new \DOMXPath($getContentData);
$sendtoDatabase = [
'region' => '関西',
'photo' => $images,
'building_name' => '',
'price' => '',
'old_price' => '',
'extend' => '',
'address' => '',
'total_house' => '',
'rooms' => '',
'cons_finish' => '',
'entry' => '',
'balcony' => '',
'company_name' => '',
'date_update' => '毎週月曜日更新',
];
$tableth = $getRouteData->query("//table[@class='outline']/tr/th");
foreach ($tableth as $getth){
$property[] = trim($getth->nodeValue);
}
$tabletd = $getRouteData->query("//table[@class='outline']/tr/td");
foreach ($tabletd as $gettd){
$value[] = trim($gettd->nodeValue);
}
switch ($property) {
case '物件名':
$sendtoDatabase['building_name'] = $value;
break;
case '販売価格':
$sendtoDatabase['price'] = $value;
break;
case '専有床面積':
$sendtoDatabase['extend'] = $value;
break;
case '所在地':
$sendtoDatabase['address'] = $value;
break;
case '総戸数':
$sendtoDatabase['total_house'] = $value;
break;
case '間取り':
$sendtoDatabase['rooms'] = $value;
break;
case '竣工日':
$sendtoDatabase['cons_finish'] = $value;
break;
case '管理形態':
$sendtoDatabase['company_name'] = $value;
break;
case '入居開始日':
$sendtoDatabase['entry'] = $value;
break;
case 'バルコニー面積':
$sendtoDatabase['balcony'] = $value;
break;
default:
break;
}
}
和$property
中。没问题,我检查了一下。只是无法将其传递给$value
进行匹配。知道为什么会这样。
答案 0 :(得分:0)
我建议您再创建一个空变量$new_variable = [];
(这会将变量$ new_variable定义为数组。您也可以像这样使它
$new_variable = array();
)
在您第一次foreach
循环之前(在此处发布代码的开头)。
在您的switch
之后,像这样填充$new_variable
(请举例:
switch ($property) {
case '物件名':
$sendtoDatabase['building_name'] = $value;
break;
case '販売価格':
$sendtoDatabase['price'] = $value;
break;
case '専有床面積':
$sendtoDatabase['extend'] = $value;
break;
case '所在地':
$sendtoDatabase['address'] = $value;
break;
case '総戸数':
$sendtoDatabase['total_house'] = $value;
break;
case '間取り':
$sendtoDatabase['rooms'] = $value;
break;
case '竣工日':
$sendtoDatabase['cons_finish'] = $value;
break;
case '管理形態':
$sendtoDatabase['company_name'] = $value;
break;
case '入居開始日':
$sendtoDatabase['entry'] = $value;
break;
case 'バルコニー面積':
$sendtoDatabase['balcony'] = $value;
break;
default:
break;
}
//right here BEFORE you close the foreach loop fill $new_variable
//with the new value like this
$new_variable[] = $sendtoDatabase;//the '[]' brackets means that each
//$sendtoDatabase will be inserted into $new_variable[0], then
//$new_variable[1], and so on untill the last foreach loop
现在foreach()
结束之后,执行一段这样的代码
foreach($new_variable as $value){
//add each $value into the database. this will take each looped foreach and
//switch you used before and add them one by one to the database
}
如果您有任何其他问题,我希望这会有所帮助:D我将尽我所能帮助您!