问题
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