This commit is contained in:
paoloar77
2024-05-19 16:31:05 +02:00
parent ad49e1382b
commit 5488460e3d

View File

@@ -28,17 +28,18 @@ function getValueByKey($array, $key)
// Funzione per aggiornare il valore di un certo key in un array di oggetti // Funzione per aggiornare il valore di un certo key in un array di oggetti
function updateValueByKey(&$array, $key, $newValue) { function updateValueByKey(&$array, $key, $newValue) {
foreach ($array as &$item) { foreach ($array as &$item) {
if ($item->key === $key) { if (is_object($item) && property_exists($item, 'key') && $item->key === $key) {
$item->value = $newValue; $item->value = $newValue;
break; // Se trova il key, termina il loop return; // Se trova il key, termina il loop
} }
} }
// push the new value in the array // Se il key non esiste nell'array, aggiungi il nuovo key-value pair
$array[] = [ $newItem = new stdClass();
'key' => $key, $newItem->key = $key;
'value' => $newValue $newItem->value = $newValue;
];
$array[] = $newItem; // Aggiungi il nuovo elemento all'array
} }
function updateValueByKeyArr(&$array, $key, $newValue) { function updateValueByKeyArr(&$array, $key, $newValue) {