Matching nodeValue with Switch Case

I am having some trouble to matching nodeValue which is $property and $value. it's actually must go smoothly but I am making mistake somewhere. I couldn't figure put.

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;
            }
} 

Datas are contained in $property and $value. No problem with that I checked it. Just can't pass it to switch case for a matching. Any idea why is this happening.

1 answer

  • answered 2018-11-08 08:44 Andrei Fiordean

    I would suggest that you make another empty variable $variable = []; before you foreach loop. after your switch fill $variable like this:

    $variable[] = $sendtoDatabase; // this will fill your $variable with all the values you would like to add to the database
    

    after your foreach() is finished, just make another

    foreach($variable as $value2){ add info to the DB // this will add each row to the database after the switch is done }

    I hope this helps!