Laravel list() with each() function error with deprecated function

岁酱吖の 提交于 2020-02-23 07:25:09

问题


I get a little confuse here on how to alternatively replace the each() function since it was deprecated and I'm aware of that and fixed some of the while( list() = each() ) error case in my project. However, what other option should I use for this case:

foreach($new_id as $new_ids) {
    list($key,$valueAddress) = each($address);
    list($key,$valueCity) = each($city);
    list($key,$valueState) = each($state);
    if(isset($_POST['publicOnly'])) {
        list($key,$valuePublicOnly) = each($publicOnly);
    } else {
        $valuePublicOnly = 0;
    }

    $propertyAddress = PropertyAddressManagement::find($new_ids);
        $propertyAddress->address = $valueAddress;
        $propertyAddress->city = $valueCity;
        $propertyAddress->state = $valueState;
        $propertyAddress->publicOnly = $valuePublicOnly;
    $propertyAddress->save();
}

回答1:


You're not using the keys so just get the current value and then move to the next one:

foreach($new_id as $new_ids) {
    $propertyAddress = PropertyAddressManagement::find($new_ids);
        $propertyAddress->address = current($address);
        $propertyAddress->city = current($city);
        $propertyAddress->state = current($state);
        $propertyAddress->publicOnly = isset($_POST['publicOnly']) ? current($publicOnly) : 0;
    $propertyAddress->save();

    next($address); next($city); next($state); next($publicOnly);
}

However, if the keys are the same in all of the array then I think really this should work:

foreach($new_id as $key => $new_ids) {
    $propertyAddress = PropertyAddressManagement::find($new_ids);
        $propertyAddress->address = $address[$key];
        $propertyAddress->city = $city[$key];
        $propertyAddress->state = $state[$key];
        $propertyAddress->publicOnly = isset($_POST['publicOnly']) ? $publicOnly[$key] : 0;
    $propertyAddress->save();
}


来源:https://stackoverflow.com/questions/58631553/laravel-list-with-each-function-error-with-deprecated-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!