From 5488460e3d85f24d441090bbcc609d8cce1e1afd Mon Sep 17 00:00:00 2001 From: paoloar77 Date: Sun, 19 May 2024 16:31:05 +0200 Subject: [PATCH] tt --- app/CustomFuncPao.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) 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) {