I am having a problem with iterating data. My for
loop is a bit strange. I am matching property->value and sending those to DB. But before that I am having a Undefined offset: 84
I think this error pops up likely when data is null. Though data is not null because I debugged it. The problem start with when I write for
and switch case
loop.
$getTR = $path->query("table[@class='bgc_line']/tr/td");
foreach($getTR as $tr){
if ($tr->length == 2) {
$route = $path>query("//table[@class='bgc_line']/tr/td[1]");
foreach ($route as $td1) {
$property[] = trim($td1->nodeValue);
}
$route = $path->query("//table[@class='bgc_line']/tr/td[2]");
foreach ($route as $td2) {
$value[] = trim($td2->nodeValue);
}
}
}
for ($a=0; $a < count($property); $a++) {
switch ($property[$a]) {
case '物件名':
$database['building_name'] = $value[$a];
break;
case '販売価格':
$database['price'] = $value[$a];
break;
case '専有面積':
$database['extend'] = $value[$a];
break;
case '所在地':
$database['address'] = $value[$a];
break;
case '総戸数':
$database['total_house'] = $value[$a];
break;
case '間取り':
$database['rooms'] = $value[$a];
break;
case '竣工時期':
$database['cons_finish'] = $value[$a];
break;
case '管理会社':
$database['company_name'] = $value[$a];
break;
case '入居時期':
$database['entry'] = $value[$a];
break;
case 'バルコニー面積':
$database['balcony'] = $value[$a];
break;
default:
break;
}
}
What am I doing wrong with the for
and switch case
and if you need to see head of code, here I share an image.
Your $value
array is shorter than the $property
array, so when you get to a property that has no corresponding value, you get an error when trying to access $value[$a]
.
Use the minimum of count($property)
and count($value)
as the limit of the loop, so you don't go past the end of $value
.
$limit = min(count($property), count($value));
for ($a=0; $a < $limit; $a++) {
switch ($property[$a]) {
case '物件名':
$database['building_name'] = $value[$a];
break;
case '販売価格':
$database['price'] = $value[$a];
break;
case '専有面積':
$database['extend'] = $value[$a];
break;
case '所在地':
$database['address'] = $value[$a];
break;
case '総戸数':
$database['total_house'] = $value[$a];
break;
case '間取り':
$database['rooms'] = $value[$a];
break;
case '竣工時期':
$database['cons_finish'] = $value[$a];
break;
case '管理会社':
$database['company_name'] = $value[$a];
break;
case '入居時期':
$database['entry'] = $value[$a];
break;
case 'バルコニー面積':
$database['balcony'] = $value[$a];
break;
default:
break;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.