Jump to content

Filtering values in a multidimensional array


Wechr

Recommended Posts

Hi,

 

I've been using PHP for about 4 weeks now, so forgive me when this is a noob question. I've generated the following array from Last.FM:

 

Array
(
    [0] => Array
        (
            [name] => post-rock
            [url] => http://www.last.fm/tag/post-rock
        )

    [1] => Array
        (
            [name] => ambient
            [url] => http://www.last.fm/tag/ambient
        )

    [2] => Array
        (
            [name] => icelandic
            [url] => http://www.last.fm/tag/icelandic
        )

    [3] => Array
        (
            [name] => alternative
            [url] => http://www.last.fm/tag/alternative
        )

    [4] => Array
        (
            [name] => indie
            [url] => http://www.last.fm/tag/indie
        )

..............

// goes on and on like that.

 

Now I want to return the keys that have this value: "[name] => indie". I tried for a while now with for and foreach loops, if else statements, array_search and array_filter and so on. Nothing seems to do the job. Does someone know the answer I'm looking for?

Nope, no results. Problem is that it I think it never goes through the entire array that's inside the array. Always bumped into this problem myself.

First one just returns: Array

Second one returns:

The key "0" has index 'name' with the value "Array"
The key "1" has index 'name' with the value "Array"
The key "2" has index 'name' with the value "Array"
The key "3" has index 'name' with the value "Array"
The key "4" has index 'name' with the value "Array"
The key "5" has index 'name' with the value "Array"

etc.

 

 

Maybe it helps more if I post the entire code. First, I generate all the topartists from my Last.FM account, using the following code:

 

#################
## Retrieve Topartists ##
#################


$methodVars_Topartists = array( 
					'user' => 'Wechr',
					'period' => 'overall'
				);

$apiClass_Topartists = new lastfmApi();					
$userClass = $apiClass_Topartists->getPackage($auth, 'user', $config);

if ( $artists = $userClass->getTopArtists($methodVars_Topartists) ) {

}
else {
die('<b>Error '.$userClass->error['code'].' - </b><i>'.$userClass->error['desc'].'</i>');
}

 

This generates the following array:

Array
(
    [0] => Array
        (
            [name] => Sigur Rós
            [rank] => 1
            [playcount] => 2667
            [mbid] => f6f2326f-6b25-4170-b89d-e235b25508e8
            [url] => http://www.last.fm/music/Sigur+R%C3%B3s
            [streamable] => 1
            [images] => Array
                (
                    [small] => http://userserve-ak.last.fm/serve/34/382318.jpg
                    [medium] => http://userserve-ak.last.fm/serve/64/382318.jpg
                    [large] => http://userserve-ak.last.fm/serve/126/382318.jpg
                )

        )

    [1] => Array
        (
            [name] => Animal Collective
            [rank] => 2
            [playcount] => 1177
            [mbid] => 0c751690-c784-4a4f-b1e4-c1de27d47581
            [url] => http://www.last.fm/music/Animal+Collective
            [streamable] => 1
            [images] => Array
                (
                    [small] => http://userserve-ak.last.fm/serve/34/28703999.jpg
                    [medium] => http://userserve-ak.last.fm/serve/64/28703999.jpg
                    [large] => http://userserve-ak.last.fm/serve/126/28703999.jpg
                )

        )

 

Now, I use this array to generate the top tags of every one of my top artists:

 

foreach ($artists as $key) {

$methodVars_Artisttags = array(
					 'artist' => $key['name']
					 );

$apiClass_Artisttags = new lastfmApi();
$artistClass = $apiClass_Artisttags->getPackage($auth, 'artist', $config);

if ( $tags = $artistClass->getTopTags($methodVars_Artisttags) ) {
} else {
die('<b>Error '.$artistClass->error['code'].' - </b><i>'.$artistClass->error['desc'].'</i>');
}	

}

 

Which generates about 50 arrays like the one I showed earlier. Now I want to filter out all the url's (I indeed meant that, thorpe ;) ) where ['name'] has the value 'indie'. I'm just not able to select it properly, always selecting the entire array.

Small alteration of thorpe's code:

 

<?php
$urls = array();
foreach ($array as $arr) {
if ($arr['name'] == 'indie') {
	$urls[] = $arr['url'];
}
}
//see contents of the $urls array
echo '<pre>' . print_r($urls, true) . '</pre>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.