PHP iterate through multidimensional associative array and print items

Greg

I have the following Array (decoded from JSON):

Array
(
    [playerInfo] => Array
        (
            [hint] => 
            [infoText] => Array
                (
                    [multiLineMode] => 
                    [subText1] => TLC
                    [subText2] => Christmas Pop
                    [title] => Sleigh Ride
                )

            [isPlayingInLemur] => 
            [lyrics] => 
            [mainArt] => Array
                (
                    [altText] => Album Art
                    [artType] => UrlArtSource
                    [contentType] => image/jpeg
                    [url] => https://images-na.sS500_.jpg
                )

            [mediaId] => 958d47b9:16
            [miniArt] => Array
                (
                    [altText] => Album Art
                    [artType] => UrlArtSource
                    [contentType] => image/jpeg
                    [url] => https://images-n._SS48_.jpg
                )

            [miniInfoText] => Array
                (
                    [multiLineMode] => 
                    [subText1] => TLC
                    [subText2] => Prime Music
                    [title] => Sleigh Ride
                )

            [playbackSource] => 
            [playingInLemurId] => 
            [progress] => Array
                (
                    [allowScrubbing] => 1
                    [locationInfo] => 
                    [mediaLength] => 223
                    [mediaProgress] => 80
                    [showTiming] => 1
                    [visible] => 1
                )

            [provider] => Array
                (
                    [artOverlay] => PRIME_SASH
                    [fallbackMainArt] => Array
                        (
                            [altText] => 
                            [artType] => IconArtSource
                            [iconId] => music-no-art
                            [iconStyles] => 
                        )

                    [providerLogo] => Array
                        (
                            [altText] => Amazon Music
                            [artType] => IconArtSource
                            [iconId] => amazon-music
                            [iconStyles] => 
                        )

                    [providerName] => Prime Music
                )

            [queueId] => 958d47
            [state] => PAUSED
            [template] => 
            [transport] => Array
                (
                    [layoutType] => MusicTransportLayout
                    [lyrics] => HIDDEN
                    [next] => ENABLED
                    [playPause] => ENABLED
                    [previous] => ENABLED
                    [repeat] => ENABLED
                    [shuffle] => ENABLED
                )

            [volume] => Array
                (
                    [muted] => 
                    [volume] => 40
                )

        )

)

I have the following foreach loop which prints every item (4) in the infoText array.

<?php
    foreach ($player['playerInfo']['infoText'] as $music) {
        echo $music;
    }
?>

But I want to be able to print only the [title] in infoText array, [state] in the playerInfo parent array, and [volume] in the volume array. Is there an elegant way to do this? Also, since I'm using foreach, I only get the value of the pair, but I also need the key. That way I can print the specific items I need.

Tom Mulkins

You may want to try a recursion function so you don't have to keep track of all those keys. I shortened your array for this example but it should work for you regardless of dimensions. Just provide the key to get the value in an array for the $searchterms argument.

function array_recursion(array $myarray, array $searchterms)
{
        foreach ($myarray as $key => $value)
        {
                if (is_array($value)) array_recursion($value, $searchterms);
                else if (in_array($key, $searchterms)) print $key . ": " . $value . "\n";

        }
}

$myarray = Array(
        'playerInfo' => Array(
                'hint' => '',
                'infoText' => Array(
                        'multiLineMode' => '',
                        'subText1' => 'TLC',
                        'subText2' => 'Christmas Pop',
                        'title' => 'Sleigh Ride'
                ),
                'state' => 'PAUSED',
                'volume' => Array(
                        'muted' => '',
                        'volume' => 40
                        )
        )
);

array_recursion($myarray, Array('state', 'title', 'volume'));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Iterate through multidimensional associative array in PHP when every key is a string

From Dev

php iterate an associative array

From Dev

PHP Searching multidimensional associative array

From Dev

php sort associative multidimensional array

From Dev

Iterate through a List and print items

From Dev

PHP - print multidimensional array

From Dev

php - how to convert an associative array to a multidimensional array

From Dev

How to turn a PHP array into a multidimensional associative array

From Dev

print partial associative array in php

From Dev

how to iterate through an array to create a new associative array with php using laravel

From Dev

PHP json_encode multidimensional associative array

From Dev

create multidimensional associative array from CSV in PHP

From Dev

Dynamicaly generated PHP multidimensional associative array

From Dev

PHP producing a multidimensional associative array in desired format

From Dev

Adding values to a multidimensional associative array in php

From Dev

Merge or update multidimensional associative array using php

From Dev

Using pointers to iterate through multidimensional array

From Dev

How to iterate through a multidimensional JSON array?

From Dev

iterate reverse over php associative array

From Dev

Iterate loop in php associative array using Jquery

From Dev

iterate reverse over php associative array

From Dev

Iterate through an array and add new items to the array

From Dev

loop through multidimensional array in php

From Dev

Parse Through PHP Multidimensional Array

From Dev

Iterating through a multidimensional array PHP

From Dev

Iterating through multidimensional array in php

From Dev

Looping through a multidimensional array with PHP

From Dev

Loop through multidimensional array and unset matching items

From Dev

How to iterate through an associative array properly, when one of the keys is "1"?

Related Related

  1. 1

    Iterate through multidimensional associative array in PHP when every key is a string

  2. 2

    php iterate an associative array

  3. 3

    PHP Searching multidimensional associative array

  4. 4

    php sort associative multidimensional array

  5. 5

    Iterate through a List and print items

  6. 6

    PHP - print multidimensional array

  7. 7

    php - how to convert an associative array to a multidimensional array

  8. 8

    How to turn a PHP array into a multidimensional associative array

  9. 9

    print partial associative array in php

  10. 10

    how to iterate through an array to create a new associative array with php using laravel

  11. 11

    PHP json_encode multidimensional associative array

  12. 12

    create multidimensional associative array from CSV in PHP

  13. 13

    Dynamicaly generated PHP multidimensional associative array

  14. 14

    PHP producing a multidimensional associative array in desired format

  15. 15

    Adding values to a multidimensional associative array in php

  16. 16

    Merge or update multidimensional associative array using php

  17. 17

    Using pointers to iterate through multidimensional array

  18. 18

    How to iterate through a multidimensional JSON array?

  19. 19

    iterate reverse over php associative array

  20. 20

    Iterate loop in php associative array using Jquery

  21. 21

    iterate reverse over php associative array

  22. 22

    Iterate through an array and add new items to the array

  23. 23

    loop through multidimensional array in php

  24. 24

    Parse Through PHP Multidimensional Array

  25. 25

    Iterating through a multidimensional array PHP

  26. 26

    Iterating through multidimensional array in php

  27. 27

    Looping through a multidimensional array with PHP

  28. 28

    Loop through multidimensional array and unset matching items

  29. 29

    How to iterate through an associative array properly, when one of the keys is "1"?

HotTag

Archive