Music management app, mashing data from various APIs I am basically getting data from various APIs and using PHP to put them together - like a web mashup. I am currently using 4 foreach statements to insert the gathered data into their individual arrays. I believe that the current code is inefficient because it takes probably around 3 seconds to load the page which is displaying the PHP data. In the past I had just one big foreach loop to go through all the data at once and also print them. But that too felt inefficient to me.

So how can I make my code more efficient in term of it processing faster? I have seen a few mashup websites such as Soundeer which load around a second. Is that becuase of their code efficiency?

The code which I am using is:

$echonest_uri = simplexml_load_file("http://developer.echonest.com/api/v4/artist/search?api_key=$APIkey&amp;style=rap&amp;results=10&amp;start=$element_num&amp;bucket=id:deezer&amp;bucket=images&amp;sort=familiarity-desc&amp;format=xml");

//Swap the comments for when in UWE or not
//$echonest_xml =  new SimpleXMLElement($echonest_uri);
$echonest_xml = $echonest_uri;

$artist_name = array();
$artist_image = array();
$echonest_id = array();
$full_deezer_id = array();
$deezer_id = array();
$num_of_albums = array();

//Loop through each entries in the id_arr and make each image of the artist a link to the album page passing all the relevant information. 
foreach($echonest_xml-&gt;artists-&gt;artist as $artist){
    $artist_name[] = $artist-&gt;name;
    $artist_image[] = $artist-&gt;images-&gt;image[0]-&gt;url;
    $echonest_id[] = $artist-&gt;id;
    $full_deezer_id[] = $artist-&gt;foreign_ids-&gt;foreign_id-&gt;foreign_id;
}

foreach($full_deezer_id as $key =&gt; $value){
    preg_match('#deezer:artist:([A-Z,a-z,0-9]+)#', $value, $id);
    $deezer_id[] = (string)$id[1];
}

foreach($deezer_id as $id_index =&gt; $id){
    $deezer_xml = simplexml_load_file("http://api.deezer.com/2.0/artist/$id/albums&amp;output=xml");
    $num_of_albums[] = $deezer_xml-&gt;total;
}

//The variable which will contain the HTML code to display the artists.
$output = null;


foreach($deezer_id as $key =&gt; $value){

    $fav_count_query = "SELECT COUNT(user_index) FROM fav_artist WHERE artist_deezer_id = '$value'";
    $fav_count_resource = $mysqli-&gt;query($fav_count_query);
    $fav_count = $fav_count_resource-&gt;fetch_assoc(); 

    $output .=  &lt;&lt;&lt;HERE
                &lt;div class="artist-container"&gt;
                    &lt;a href="albums.php?echonest_id={$echonest_id[$key]}&amp;deezer_id={$deezer_id[$key]}&amp;artist_name={$artist_name[$key]}&amp;artist_image={$artist_image[$key]}&amp;num_of_albums={$num_of_albums[$key]}" class="artist-image"&gt;
                        &lt;img src="{$artist_image[$key]}" alt="{$artist_name[$key]}" title="{$artist_name[$key]}"/&gt;
                    &lt;/a&gt;

                    &lt;a href="albums.php?echonest_id={$echonest_id[$key]}&amp;deezer_id={$deezer_id[$key]}&amp;artist_name={$artist_name[$key]}&amp;artist_image={$artist_image[$key]}&amp;num_of_albums={$num_of_albums[$key]}" class="artist-name"&gt;
                        {$artist_name[$key]}
                    &lt;/a&gt;
                    &lt;a href="albums.php?echonest_id={$echonest_id[$key]}&amp;deezer_id={$deezer_id[$key]}&amp;artist_name={$artist_name[$key]}&amp;artist_image={$artist_image[$key]}" class="album-number"&gt;Albums: 
                        {$num_of_albums[$key]}
                    &lt;/a&gt;
                &lt;/div&gt;
HERE;

}
Please add the following method do your code and print the runtime of every section of your code. As indicated by my comment I guess the foreach->simplexml_load_file("...") might take a while.

//PHP &lt;5.0.0
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$start=microtime_float();
//...
echo microtime_float()-$start;

//PHP&gt;=5.0.0
$start=microtime(true);
//...
echo microtime(true)-$start;