Files
apimacro/app/CustomFuncPao.php

55 lines
1.3 KiB
PHP
Raw Normal View History

2024-05-19 13:06:17 +02:00
<?php
function showarray($array)
{
echo '<pre>' . json_encode($array, JSON_PRETTY_PRINT) . '</pre>';
};
function isKeyPresent($array, $key)
{
foreach ($array as $item) {
if (isset($item->key) && $item->key === $key) {
return true;
}
}
return false;
}
function getValueByKey($array, $key)
{
foreach ($array as $item) {
if (isset($item->key) && $item->key === $key) {
return $item->value;
}
}
return null; // If key is not found
}
// Funzione per aggiornare il valore di un certo key in un array di oggetti
function updateValueByKey(&$array, $key, $newValue) {
2024-05-19 13:11:57 +02:00
foreach ($array as &$item) {
2024-05-19 16:31:05 +02:00
if (is_object($item) && property_exists($item, 'key') && $item->key === $key) {
2024-05-19 13:11:57 +02:00
$item->value = $newValue;
2024-05-19 16:31:05 +02:00
return; // Se trova il key, termina il loop
2024-05-19 13:11:57 +02:00
}
}
2024-05-19 16:27:15 +02:00
2024-05-19 16:31:05 +02:00
// 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
2024-05-19 13:11:57 +02:00
}
function updateValueByKeyArr(&$array, $key, $newValue) {
2024-05-19 13:06:17 +02:00
foreach ($array as &$item) {
if ($item['key'] === $key) {
$item['value'] = $newValue;
break; // Se trova il key, termina il loop
}
}
2024-05-19 16:27:15 +02:00
$array[$key] = $newValue;
2024-05-19 13:06:17 +02:00
}