I'm coding a page using PHP.
I can get a list of categories via JSON (external, that I can't modify). Each category has an ID and a name. I'm retrieving the list using a foreach:
$jsonCategoriesDecode = json_decode($jsonCategories, true);
foreach ($categories as $category){
$category['id'] = $category['name'];
}
On another area of my code, I have a for loop
to get another type of information via JSON (also external). But here I have only access to the Category ID (not to the Category Name).
$jsonLatest = json_decode($latest, true);
for ($i = 1; $i < 6; $i++) {
$topic_category[$i] = $jsonLatest['topic_list']['topics'][$i]['category_id'];
echo $topic_category[$i];
}
What I want is to show the Category Name inside the for loop (instead of the Category ID).
I tried the following:
$jsonLatest = json_decode($latest, true);
for ($i = 1; $i < 6; $i++) {
$topic_category[$i] = $jsonLatest['topic_list']['topics'][$i]['category_id'];
// Translate the category ID to Name
foreach ($jsonCategoriesDecode['category_list']['categories'] as $category){
$category['id'] = $category['name'];
$topic_category[$i] = $category['id'];
echo $category['name'];
}
}
But the output is a list of names for all the categories. I just want the name of the $topic_category[$i]
I played with the switch
statement, but I haven't had any luck. I can't hardcode all the Category IDs and their corresponding Category Names because I will create more in the future, so it needs to be automated. That's why I'm trying to use the foreach loop
.
How can I translate that Category ID into the Category Name?
EDIT: The main problem is that I can't get the Category Name on on the $jsonLatest, only the ID.
I can't change the JSON contents because they are external. That's why I'm thinking that perhaps using the other JSON (where I can get both ID and Name) will help to translate that ID into the Name on the $jsonLatest.
The foreach loop, it's just for that, it doesn't do anything else on my code. I can even delete it
EDIT 2: Added source links
Json files: https://forum.illustration.tools/latest.json https://forum.illustration.tools/categories.json
Php file: pastebin.com/bmX8TAaj
The right way is a matter of opinion.
I would definately not do a foreach loop inside a for loop (or any two nestled loops of any kind for that matter) if it can be avoided. Which it can for your case.
This is not because of it being wrong, but because it increases time complexity in your code. As the arrays grow bigger, your code will execute exponentially slower.
Now, this is still an opinion, and you will have to decide for yourself. Perhaps you know that these json objects will not grow big over time, and perhaps memory is a key concern for your script. In that case, stick to another solution.
However, this is how I would solve it.
$jsonCategories = json_decode('https://forum.illustration.tools/categories.json', true);
$jsonLatest = json_decode('https://forum.illustration.tools/latest.json', true);
$categoryNames = array();
foreach ($jsonCategories['category_list']['categories'] as $category) {
$categoryNames[$category['id']] = $category['name'];
}
for ($i = 1; $i < 6; $i++) {
$topic_category[$i] = $jsonLatest['topic_list']['topics'][$i]['category_id'];
echo $categoryNames[$topic_category[$i]], PHP_EOL;
}