如何使用PHP访问关联数组中的值 [英] How To Access Values In Associative Array Using PHP
问题描述
我有一个数组,该数组是使用Amazon SimpleDb进行选择查询的结果.
I have an array which is the result of a select query using Amazon SimpleDb.
这是我print_r($ result);时的示例数据;
Here is sample data when I print_r($result);
Array ( [0] => Array ( [Name] => 5140ede647e74
[Attributes] => Array (
[0] => Array ( [Name] => test_id [Value] => 5140ede647e74 )
[1] => Array ( [Name] => test_name [Value] => test1 )
[2] => Array ( [Name] => last_update [Value] => 1363209702 )
[3] => Array ( [Name] => created [Value] => 1363209702 ) ) ) )
如果我要提取test_id和test_name,该怎么办?我目前正在执行以下操作
If I want to extract the test_id and the test_name, how can I do it? I am currently doing the following
<?php foreach ($result as $item) {
echo $item['Attributes'][0]['Value'];
echo $item['Attributes'][1]['Value'];
} ?>
但是我想通过引用"test_id"和"test_name"来做到这一点,因为当我删除数据所在的域并重新输入数据时,每个属性的顺序都会改变,所以我不相信$ item ['Attributes'] [0] ['Value']将始终为test_id
But I want to do it by referencing "test_id" and "test_name" because when I delete the domain where the data resides and re-enter the data, the order of each attribute can change so I can't trust that $item['Attributes'][0]['Value'] will always be the test_id
谢谢!
推荐答案
foreach ($result as $item) {
foreach ($item['Attributes'] as $keyvalue) {
if ($keyvalue['Name'] == 'test_id' || $keyvalue['Name'] == 'test_name') {
echo $keyvalue['Value'];
}
}
}
这篇关于如何使用PHP访问关联数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!