如何迭代数组并在PHP中获取键值

时间:2011-12-09 03:04:04

标签: php json foreach iteration

我试图弄清楚如何迭代一个解码的json字符串数组。我想为firstname,lastname等创建变量,然后将它们输出到表中的单独行。这是一个示例数组。大多数结果都会有多条记录。任何建议将不胜感激:

Array ( [results] => Array ( [@count] => 1 [@pageNumber] => 1 [@totalRecords] => 1 [@additionalPages] => 0 [person] => Array ( [0] => Array ( [@array] => true [@id] => 38903211 [@uri] => https://api-name-removed.com/People/38903211 [@imageURI] => [@oldID] => 38614423 [@iCode] => fwVVyUOWEg3DOjIfg== [@householdID] => 23902641 [@oldHouseholdID] => 23740508 [title] => [salutation] => [prefix] => [firstName] => Mandy [lastName] => Ford [suffix] => [middleName] => [goesByName] => [formerName] => [gender] => Female [dateOfBirth] => 1965-02-11T00:00:00 [maritalStatus] => [householdMemberType] => Array ( [@id] => 1 [@uri] => https://api-name-removed.com/People/HouseholdMemberTypes/1 [name] => Head ) [isAuthorized] => true [status] => Array ( [@id] => 26523 [@uri] => https://api-name-removed.com/People/Statuses/26523 [name] => Prospect [comment] => [date] => 2010-09-07T00:00:00 [subStatus] => Array ( [@id] => [@uri] => [name] => ) ) [occupation] => Array ( [@id] => [@uri] => [name] => [description] => ) [employer] => [school] => Array ( [@id] => [@uri] => [name] => ) [denomination] => Array ( [@id] => [@uri] => [name] => ) [formerChurch] => [barCode] => 20001881 [memberEnvelopeCode] => [defaultTagComment] => [weblink] => Array ( [userID] => [passwordHint] => [passwordAnswer] => ) [solicit] => [thank] => true [firstRecord] => 2008-11-24T14:52:23 [attributes] => [addresses] => [communications] => Array ( [communication] => Array ( [0] => Array ( [@array] => true [@id] => 40826651 [@uri] => https://api-name-removed.com/Communications/40826651 [household] => Array ( [@id] => 23902641 [@uri] => https://api-name-removed.com/Households/23902641 ) [person] => Array ( [@id] => [@uri] => ) [communicationType] => Array ( [@id] => 4 [@uri] => https://api-name-removed.com/Communications/CommunicationTypes/4 [name] => Email ) [communicationGeneralType] => Email [communicationValue] => mford@email.com [searchCommunicationValue] => mford@adventurechristian.org [listed] => true [communicationComment] => [createdDate] => 2011-11-14T17:10:13 [lastUpdatedDate] => 2008-11-24T14:52:23 ) ) ) [lastMatchDate] => [createdDate] => 2011-11-14T17:10:03 [lastUpdatedDate] => 2011-05-09T11:43:59 ) ) ) )

1 个答案:

答案 0 :(得分:5)

您可以使用foreach,然后使用variable variables

foreach($array as $keyName => $value) {
    //in here you have the key in $keyName and the value in $value
    $$keyName = $value;
}

编辑:不是100%确定您想要的'数组'和'结果',但我认为问题是数组是其他数组的子代,您可以将代码修改为检查值是否是这样的数组:

// 1. flatten out the array to contain only single values (no arrays)

do {
    $containedArrayValue = false;
    foreach($array as $key => $value) {
        if(is_array($value)) {
            $array = array_merge($array,$value);
            $containedArrayValue = true;
        }
        unset($array[$key]);
    }

} while($containedArrayValue);

// 2. run the code above to get variables for each one
foreach($array as $keyName => $value) 
    $$keyName = $value;

这是你想要发生的事吗?否则请给出你想要的结果,我会把它修复成那样......

第二次编辑:我将保留上面的代码,因为即使它不是您想要的,它可能会在以后帮助其他人。您拥有的数据结构是一个数组,其中每个值都是字符串,数字或其他数组。每个阵列都是相同的方式,因此您可以有很多层(本例中为7)。如果整个变量存储在$ array中,那么你关心的层将在$ array [results] [person]中;这是一个'人'数组的数组,所以第一个人将在$ array [结果] [人物] [0]第二个人将在$ array [结果] [人物] [1]等内部每个人你可以获得您想要的数据

$firstName = $array[results][person][0][firstName];
$lastName  = $array[results][person][0][lastName]; 
$email     = $array[results][person][0][email];

现在如果有多个人怎么办?我们可以这样生成这些变量的数组:

foreach($array[results][person] as $personNum => $personData) {
    $firstNames[] = $personData[firstName];
    $lastNames[]  = $personData[lastName];
    $emails[]     = $personData[email];
}

现在您在这三个数组中拥有所需的数据。空括号只是数组的下一个空元素的简写,所以在循环中我只是按成员构建这些数组成员。如果你想获得更复杂的东西,比如他们的密码提示你可以通过循环内部的$ personData [weblink] [passwordHint]来获得它。有关详细信息,请查看foreach syntaxmultidimensional arrays