tt
This commit is contained in:
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user