簡體   English   中英

如何遍歷數組並獲取PHP中的鍵值

[英]How to iterate over array and get key values in PHP

我試圖弄清楚如何遍歷數組是解碼的json字符串。 我想為名字,姓氏等創建變量,然后將它們輸出到表中的單獨行中。 這是一個示例數組。 大多數結果將具有多個記錄。 任何建議,將不勝感激:

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

您可以使用foreach然后使用變量變量

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

編輯:不是100%肯定要使用'array'和'results',但我認為問題在於該數組是其他數組的子級,您可以修改代碼以檢查值是否為數組,如下所示:

// 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語法多維數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM