Zak
Zak

Reputation: 7

How to change field value in array by incoming ID

How to change field value in array by incoming ID

For example, comes $ cityID = '2'; it means that UF_DEFAULT should be changed to 1 for the city of Tver, and 0 for the city of Moscow

or comes $ cityID = '10' ; is not existing, leave everything as it is without overwriting UF_DEFAULT

$cityID = '2'; // 2 - есть | 10 - нет
$arr = array(
        '1'=>array('ID'=>'1', 'NAME'=>'Moscow', 'UF_DEFAULT'=>'1', 'UF_CITY_CORD'=>'55.76, 37.64'),
        '2'=>array('ID'=>'2', 'NAME'=>'Tver', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'),
        '3'=>array('ID'=>'3', 'NAME'=>'Nahodka', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'),
        '4'=>array('ID'=>'4', 'NAME'=>'Omsk', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'),
);
foreach($arr as $key => $arSection)
{
    $arSectionsResult[$arSection['ID']] = $arSection;
}
unset($key, $arSection);
echo "<pre>";print_r($arSectionsResult);echo "</pre>";

Thanks in any case.

Upvotes: 0

Views: 51

Answers (3)

mickmackusa
mickmackusa

Reputation: 47992

Only perform one loop. Only change values that need changing. Quit iterating asap.

I am "modifying the input array by reference" in the loop. To avoid issue downscript with $row, I unset it when the loop concludes.

Code: (Demo)

$cityID = '2';
$oldCityFound = false;
$newCityFound = false;
$arr = [
        '1' => ['ID'=>'1', 'NAME'=>'Moscow', 'UF_DEFAULT'=>'1', 'UF_CITY_CORD'=>'55.76, 37.64'],
        '2' => ['ID'=>'2', 'NAME'=>'Tver', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'],
        '3' => ['ID'=>'3', 'NAME'=>'Nahodka', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'],
        '4' => ['ID'=>'4', 'NAME'=>'Omsk', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'],
];
foreach ($arr as &$row) {
    if ($row['ID'] == $cityID) {
        if ($row['UF_DEFAULT'] == 1) {
            break; // nothing to change
        }
        $row['UF_DEFAULT'] = 1;
        $newCityFound = true;
    } elseif ($row['UF_DEFAULT'] == 1) {
        $row['UF_DEFAULT'] = 0;
        $oldCityFound = true;
    }
    if ($newCityFound && $oldCityFound) {
        break; // both jobs done ...efficiently
    }
}
unset($row); // best practice
var_export($arr);

In my answer, I am assuming the outer keys are not trusted as the ID value. If the outer keys always equal the ID value within, then you can perform two shortcuts prior to looping:

if (!isset($arr[$cityID])) {
    // cityID not found, now what?
}

And

if ($arr[$cityID]['UF_DEFAULT']  == 1) {
    // nothing to change 
}

Upvotes: 0

u_mulder
u_mulder

Reputation: 54831

$cityID = '10'; // 2 - есть | 10 - нет
$arr = array(
    '1'=>array('ID'=>'1', 'NAME'=>'Moscow', 'UF_DEFAULT'=>'1', 'UF_CITY_CORD'=>'55.76, 37.64'),
    '2'=>array('ID'=>'2', 'NAME'=>'Tver', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'),
    '3'=>array('ID'=>'3', 'NAME'=>'Nahodka', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'),
    '4'=>array('ID'=>'4', 'NAME'=>'Omsk', 'UF_DEFAULT'=>'0', 'UF_CITY_CORD'=>'55.76, 37.64'),
);

// first we find - if we should change UF_DEFAULT
$changeDefault = false;
foreach ($arr as $item) {
    // find item with same ID and UF_DEFAULT != 1
    if ($item['ID'] === $cityID && $item['UF_DEFAULT'] != 1) {
        $changeDefault = true;
        break;
    }
}

// if you have to `changeDefault` - iterate over 
// array again and change according to `cityID`
if ($changeDefault) {
    foreach ($arr as &$item) {
        $item['UF_DEFAULT'] = $item['ID'] == $cityID ? 1 : 0;
    }
}

print_r($arr);

Upvotes: 1

Kryptur
Kryptur

Reputation: 745

Even though I don't really understand the logic behind you problem, this would one way to do this.

First check if the given key exists with array_key_exists. If so, set the UF_DEFAULT to 0 for all except for the given ID.

if (array_key_exists($cityId, $arr)) {
    foreach ($arr as $k => $city) {
        $arr[$k]["UF_DEFAULT"] = 0;
    }
    $arr[$cityId]["UF_DEFAULT"] = 1;
} else {
    // Key not existent...
}

Upvotes: 0

Related Questions