如何使用php从一个assosiative数组中读取值?

时间:2017-11-30 15:17:12

标签: php arrays

这是我的样本关联数组:

Array (
    [0] => Array (
        [Name] => ALCOIN
        [BasePlugin] => HTTP
        [Version] => 1
        [Description] => Plugin for ALCO_IN operations
        [ImagePath] => ./resources
        [xip] => http://www.example.org/XIP
        [xsi] => http://www.w3.org/2001/XMLSchema-instance
        [schemaLocation] => http://www.example.org/XIP XIP.xsd
    )
    [1] => Array (
        [xip:Action] => Array (
            [@attributes] => Array (
                [Name] => OfferActivationByOfferID
                [Version] => 1.0
                [ImagePath] => ./resources
            )
        )
    )
)

在该数组中,我需要获取BasePlugin值和名称attribute值。

2 个答案:

答案 0 :(得分:4)

看来你可能需要这个:

$basePlugin = $yourArray[0]['BasePlugin'];
$attributes = $yourArray[1]['xip:Action']['@attributes'];

答案 1 :(得分:0)

假设您被分配到$yourVar,如:

$yourVar = Array (
    [0] => Array (
        [Name] => ALCOIN
        [BasePlugin] => HTTP
        [Version] => 1
        [Description] => Plugin for ALCO_IN operations
        [ImagePath] => ./resources
        [xip] => http://www.example.org/XIP
        [xsi] => http://www.w3.org/2001/XMLSchema-instance
        [schemaLocation] => http://www.example.org/XIP XIP.xsd
    )
    [1] => Array (
        [xip:Action] => Array (
            [@attributes] => Array (
                [Name] => OfferActivationByOfferID
                [Version] => 1.0
                [ImagePath] => ./resources
            )
        )
    )
)

你会使用:

echo $yourVar[0]["BasePlugin"];
echo $yourVar[1]["xip:Action"]["@attributes"]["Name"];
echo $yourVar[1]["xip:Action"]["@attributes"]["Version"];
echo $yourVar[1]["xip:Action"]["@attributes"]["ImagePath"];