PHP迭代多维关联数组和打印项

时间:2016-12-22 20:54:37

标签: php arrays json multidimensional-array foreach

我有以下数组(从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
                )

        )

)

我有以下foreach循环,它打印infoText数组中的每个项目(4)。

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

但我希望能够只打印infoText数组中的[title],playerInfo父数组中的[state]和卷数组中的[volume]。有一种优雅的方式来做到这一点?此外,由于我使用foreach,我只获得该对的值,但我也需要密钥。这样我就可以打印出我需要的特定物品。

2 个答案:

答案 0 :(得分:1)

您可能想尝试递归功能,因此您无需跟踪所有这些键。我为这个例子缩短了你的数组,但无论尺寸如何,它都适合你。只需提供密钥即可获取$ searchterms参数的数组值。

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'));

答案 1 :(得分:0)

好像&#39; infoText&#39;是一个包含标题的数组,你可以这样做:

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

然后您还可以使用$key获取当前迭代的数组键。