Wechr Posted November 9, 2009 Share Posted November 9, 2009 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? Link to comment https://forums.phpfreaks.com/topic/180817-filtering-values-in-a-multidimensional-array/ Share on other sites More sharing options...
trq Posted November 9, 2009 Share Posted November 9, 2009 Assuming by keys you mean url's? $out = array(); foreach ($originalarray as $k) { if ($k['name'] == 'indie') { $out[] = $k; } } $out will now be an array of arrays having the name key set to a value of 'indie'. Link to comment https://forums.phpfreaks.com/topic/180817-filtering-values-in-a-multidimensional-array/#findComment-953926 Share on other sites More sharing options...
Garethp Posted November 9, 2009 Share Posted November 9, 2009 foreach($Array as $k=>$v) { if(isset($v['name'])) { echo "The key \"$k\" has index 'name' with the value \"$v\"<br>"; } } Link to comment https://forums.phpfreaks.com/topic/180817-filtering-values-in-a-multidimensional-array/#findComment-953927 Share on other sites More sharing options...
Wechr Posted November 9, 2009 Author Share Posted November 9, 2009 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. Link to comment https://forums.phpfreaks.com/topic/180817-filtering-values-in-a-multidimensional-array/#findComment-954098 Share on other sites More sharing options...
thebadbad Posted November 9, 2009 Share Posted November 9, 2009 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>'; ?> Link to comment https://forums.phpfreaks.com/topic/180817-filtering-values-in-a-multidimensional-array/#findComment-954114 Share on other sites More sharing options...
Wechr Posted November 9, 2009 Author Share Posted November 9, 2009 Works! Thanks ^o^ Link to comment https://forums.phpfreaks.com/topic/180817-filtering-values-in-a-multidimensional-array/#findComment-954212 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.