Skip to content
Advertisement

PHP array stringify

In a lyrics application I’m coding, I’m using an array to print an artists table. The artists array looks like this:

JavaScript

Before printing it, I do some modification to the array. I have a folder for each artist that contains the lyrics files. I add the folder names to the $artists array for later use:

JavaScript

Later, I add the album and track count for each artist to the array:

JavaScript

The end result of the array looks like this:

JavaScript

Now, my problem is that this process happens every time I visit the page. The 2nd, 3rd, and the 4th columns are created again and again. I think this is redundant.

I want to save the end result of the array and use it on the page. If this was JavaScript I’d use JSON.stringify(), but in PHP I don’t know how to get the end result of the array. print_r() doesn’t do the job, because it prints it like this:

JavaScript

I want it like this:

JavaScript

Is there a way to print the array the JSON.stringify() way?

Advertisement

Answer

Is this what you want?

echo json_encode($artists)

PHP: json_encode

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement