PHP Search multidimensional array for value & get related elements value
In PHP I am looking to extract a value from an Array by searching with another value. I have the uri value and I require the playcount value that corresponds with the uri.
What is the best approach to getting this done? What I have now:
$decoded = json_decode($response, true);
$trackids = 'spotify:track:'. $trackid .'';
$key = array_search($trackids, array_column($decoded, 'playcount'));
$result = $decoded[$key]['playcount'];
echo "Result: ";
echo $result;
I think it is incomplete and not sure how to proceed from there as it doesn't work..
The array:
Array
(
[success] => 1
[data] => Array
(
[uri] => spotify:album:3T4tUhGYeRNVUGevb0wThu
[name] => ?� (Deluxe)
[cover] => Array
(
[uri] => https://i.scdn.co/image/ab67616d00001e02ba5db46f4b838ef6027e6f96
)
[year] => 2017
[track_count] => 16
[discs] => Array
(
[0] => Array
(
[number] => 1
[name] =>
[tracks] => Array
(
[0] => Array
(
[uri] => spotify:track:7oolFzHipTMg2nL7shhdz2
[playcount] => 181540969
[name] => Eraser
[popularity] => 63
[number] => 1
[duration] => 227426
[explicit] =>
[playable] => 1
[artists] => Array
(
[0] => Array
(
[name] => Ed Sheeran
[uri] => spotify:artist:6eUKZXaKkcviH0Ku9w2n3V
[image] => Array
(
[uri] => https://i.scdn.co/image/ab6761610000517412a2ef08d00dd7451a6dbed6
)
)
)
)
[1] => Array
(
[uri] => spotify:track:6PCUP3dWmTjcTtXY02oFdT
[playcount] => 966197832
[name] => Castle on the Hill
[popularity] => 79
[number] => 2
[duration] => 261153
[explicit] =>
[playable] => 1
[artists] => Array
(
[0] => Array
(
[name] => Ed Sheeran
[uri] => spotify:artist:6eUKZXaKkcviH0Ku9w2n3V
[image] => Array
(
[uri] => https://i.scdn.co/image/ab6761610000517412a2ef08d00dd7451a6dbed6
)
)
)
)
Answer
Solution:
$key = array_search($trackids, array_column($decoded, 'playcount'));
in this line, you make two mistake. First, there is no column like playcount in $decode array. Second, you are searching with uri
key, not playcount
key. There is one more thing, discs key and track inside discs key, both are multidimensional array. So if you want to fetch the exact value, This query will be,
$decoded = array_map(function($x) {
return array_column($x, 'url');
}, array_column($decoded['data']['discs'], 'tracks'));
$decoded = call_user_func_array('array_merge', $decoded);
$key = array_search($trackids, $decoded);
Answer
Solution:
You're on the right track, but you need to dig down deeper into the array structure to search for the tracks within each disc.
$decoded = json_decode($response, true);
$trackid = '7oolFzHipTMg2nL7shhdz2';
$trackidString = 'spotify:track:' . $trackid;
$playcount = null;
// Loop through the discs looking for the track
foreach ($decoded['data']['discs'] as $currDisc)
{
// If we find the track, get the playcount and break out of the loop
$key = array_search($trackidString, array_column($currDisc['tracks'], 'uri'));
if($key !== false)
{
$playcount = $currDisc['tracks'][$key]['playcount'];
break;
}
}
assert($playcount == 181540969, 'We should find the expected playcount value');
echo 'Result: ' . $playcount . PHP_EOL;
Source