17 lines
806 B
PHP
17 lines
806 B
PHP
<?php
|
|
function generateScriptData($phpArray) {
|
|
// Check if the array is associative and single-level
|
|
if (is_array($phpArray) && count($phpArray) > 0 && count(array_filter(array_keys($phpArray), 'is_string')) === count($phpArray)) {
|
|
// Generate JavaScript code to save each array element to local storage
|
|
echo "<script>";
|
|
foreach ($phpArray as $key => $value) {
|
|
$escapedKey = addslashes($key); // Escape special characters in the key
|
|
$escapedValue = addslashes($value); // Escape special characters in the value
|
|
|
|
echo "localStorage.setItem('$escapedKey', '$escapedValue');";
|
|
}
|
|
echo "</script>";
|
|
} else {
|
|
echo "<script>console.error('Invalid PHP array. Must be single-level and associative.');</script>";
|
|
}
|
|
} |