Files
local-content-pro/assets/json/alphabetize.php
2024-12-29 01:47:56 -08:00

58 lines
1.7 KiB
PHP

<?php
// Path to your icons.json file
$filePath = 'output.json';
// Read the content of the JSON file
$jsonData = file_get_contents($filePath);
// Decode the JSON data into a PHP array
$iconsData = json_decode($jsonData, true);
// Check if the data is decoded successfully
if (json_last_error() !== JSON_ERROR_NONE) {
die('Error decoding JSON data: ' . json_last_error_msg());
}
// Check if the data contains a 'family' key (indicating it has families with SVGs)
if (isset($iconsData[0]['family'])) {
// Handle the case with families and svgs (structure 1)
foreach ($iconsData as &$family) {
if (isset($family['svgs']) && is_array($family['svgs'])) {
// Sort the 'svgs' array alphabetically by the 'name' field
usort($family['svgs'], function($a, $b) {
return strcmp($a['name'], $b['name']);
});
}
}
// Sort the families by the 'family' field (if needed)
usort($iconsData, function($a, $b) {
return strcmp($a['family'], $b['family']);
});
} else {
// Handle the case without families (structure 2)
// Sort the flat array of icons alphabetically by the 'name' field
usort($iconsData, function($a, $b) {
return strcmp($a['name'], $b['name']);
});
}
// Encode the sorted data back to JSON
$sortedJsonData = json_encode($iconsData, JSON_PRETTY_PRINT);
// Check if encoding was successful
if (json_last_error() !== JSON_ERROR_NONE) {
die('Error encoding JSON data: ' . json_last_error_msg());
}
// Write the sorted JSON data back to the file
if (file_put_contents($filePath, $sortedJsonData)) {
echo "The icons data has been sorted and saved successfully!";
} else {
echo "Error writing the sorted data to the file.";
}
?>