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
)
)
)
)
$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 withuri
key, notplaycount
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);
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;
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.