Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this sample code from a lastfm script i found..:

function getArtistAlbums($artist, $size) {
    $artist = urlencode($artist);
    $xml    = "http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist={$artist}&api_key=xxxxxxxxxxxxxxxxxxxxx";
    $xml    = @file_get_contents($xml);

    if(!$xml) {
        return;  // Artist lookup failed.
    }

    $xml = new SimpleXMLElement($xml);
    $xml = $xml->topalbums;
    foreach ($xml->album as $album) {
        $album_img =  $album->image[$size];
        $album_image = convert($album_img);
        $album_name =  $album->name;

        //echo instead of returning
        echo $album_name."<br>".$album_image."<br><br>";
    }

but instead of echoing the results I want to return all $album_image to a variable and call it from another file. I tried this:

$xml = new SimpleXMLElement($xml);
$xml = $xml->topalbums;
$values = array();
foreach ($xml->album as $album) {
    $album_img =  $album->image[$size];
    $album_image = convert($album_img);
    $album_name =  $album->name;
    $values[] = $album_image;
    return $values;

    //echo instead of returning                     
    #echo $album_name."<br>".$album_image."<br><br>";
}

I know I need to build an array. I tried different things but I always get "Array" when I call the variable.

PS: i am very new with php so please bare with me, thanks in advance

The script is from here: http://techslides.com/lastfm-api-with-php/. In my other PHP script when I call the other functions, for example artist-album, I get a return with an image and then I can style it and output what I want. But I can't figure out how to output the above. When I call it inside an

echo '<div class='example'>'.getArtistAlbums(artist,2).'</div>'; 

it just echoes images.

The question is how can I somehow return variables and then use inside another variable as a did for example for artist-album. For example:

$artist_image = getArtist($artist,2); 

and then use $artist_image with echo and class etc.

share|improve this question
1  
echoing an array will show Array. If you want to inspect the content of the array, you have to use print_r or var_dump. I don't see a point in returning inside the loop though. It will terminate the function after the first iteration. – Felix Kling Jun 7 at 23:01
What does the other file look like??? Of course it would be an array, because it is an array. Isn't that what you want? – KyleK Jun 7 at 23:02
@Kylek i just updated the post with the link to original script and what i'm trying to do.. Thanks for replying :) – Sam F Jun 7 at 23:19
Using proper punctuation, grammar and formatting makes your question easier to read and understand. Regarding the edit, when you return an array, you have to iterate over that array as well (just like you did inside the function) and style each item individually. KyleK shows exactly that in his answer (only that they directly echo the value instead of adding HTML to it). – Felix Kling Jun 7 at 23:24
I don't understand what youre trying to do....from what I get, you're trying to take an output from one function an dinput to another? – KyleK Jun 7 at 23:25
show 3 more comments

2 Answers

up vote 0 down vote accepted

I would do:

$xml = new SimpleXMLElement($xml);
$xml = $xml->topalbums;
$values = array();
foreach ($xml->album as $album) {
    $album_img =  $album->image[$size];
    $album_image = convert($album_img);
    $album_name =  $album->name;
    array_push($values, $album_image);
}
return $values;

Check to make sure everything inside the array is correct:

var_dump($values);

To access the values in the other file..

foreach($values as $val){
    echo $val;
}

EDIT...I think this is what youre trying to do...

$values = getArtistAlbum(artist, 2);

foreach($values as $val){
    echo "<div class='example'>".$val."</div>";
}

Once you have executed the getArtistAlbum function, you get the array returned to you...which you can then iterate over and display accordingly.

You can also skip the step of doing two loops, and just integrate the div build into the getArtistAlbum function itself...like so....but the other way is better, since it produces more reuseable code

function getArtistAlbum($artist, $size){
    $xml = new SimpleXMLElement($xml);
    $xml = $xml->topalbums;

    foreach ($xml->album as $album) {
        $album_img =  $album->image[$size];
        $album_image = convert($album_img);
        $album_name =  $album->name;
        echo "<div class='example'>".$album_image."</div>
    }

}

EDIT 2 (based on your comments)

For one div...

$values = getArtistAlbum(artist, 2);

echo "<div class='example'>";
foreach($values as $val){
    echo $val;
}
echo "</div>";
share|improve this answer
What's wrong with $values[] = $album_image;? – Felix Kling Jun 7 at 23:02
nothing, either way works... :) – KyleK Jun 7 at 23:04
Ah, that was the only change you had when I made my comment so I was wondering what the point is ;) – Felix Kling Jun 7 at 23:06
1  
Maybe you could add $values = getArtistAlbums($artist, $size); to your answer, just to make sure the OP understands what $values is and where it comes from. – Felix Kling Jun 7 at 23:29
I really dont understand why I got downvoted, this answer is exactly what the OP is wanting... – KyleK Jun 8 at 0:07
show 4 more comments

try this

 $album_name='';

    foreach ($xml->album as $album) {
                        $album_img =  $album->image[$size];
                        $album_image = convert($album_img);
                        $album_name =  $album->name;
                        $values[] = $album_image;
                        return $values;

                        //echo instead of returning

                       $album_name.=$album_image;


                }

    echo $album_name;
share|improve this answer
$album_name.=.$album_image; and echo $album_name; won't be executed. Did you intend to remove the return statement? – Felix Kling Jun 7 at 23:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.