diff --git a/app/CustomFuncPao.php b/app/CustomFuncPao.php index 933a5223..0767e20d 100644 --- a/app/CustomFuncPao.php +++ b/app/CustomFuncPao.php @@ -28,17 +28,18 @@ function getValueByKey($array, $key) // Funzione per aggiornare il valore di un certo key in un array di oggetti function updateValueByKey(&$array, $key, $newValue) { foreach ($array as &$item) { - if ($item->key === $key) { + if (is_object($item) && property_exists($item, 'key') && $item->key === $key) { $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 - $array[] = [ - 'key' => $key, - 'value' => $newValue - ]; + // Se il key non esiste nell'array, aggiungi il nuovo key-value pair + $newItem = new stdClass(); + $newItem->key = $key; + $newItem->value = $newValue; + + $array[] = $newItem; // Aggiungi il nuovo elemento all'array } function updateValueByKeyArr(&$array, $key, $newValue) {