I have this:
foreach ($books as $book)
{
$catname = get_category_name($book['category']);
$book['category'] = $catname;
}
print_r($books);
Right now, $book['category']
represents an integer value, which references the ID of a row in a category table of the database (e.g. ID 1 = Fiction). As you can guess, get_category_name($id)
takes an ID and finds the correct row, and returns the name of the category. The function works correctly, and if i print_r($catname)
in the foreach
it prints the names of the categories, like it should.
I have an associative array called $books
that gets filled from every row in the book table in the database. What I want to do, is take the category integer value of each book element and use get_category_name($id)
to replace that integer value with the actual category name.
The problem I am having is that I cannot replace the integer value that already exists at $book['category']
with the actual category name which resides in $catname
. When I use print_r($books)
to see if the changes were made in the foreach
, they confirm the changes do not get made.
How can I fix this?