【问题标题】:How to iterate over array and get key values in PHP如何在 PHP 中遍历数组并获取键值
【发布时间】:2012-01-16 10:51:36
【问题描述】:

我试图弄清楚如何迭代一个解码后的 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 ) ) ) )

【问题讨论】:

    标签: php json foreach iteration


    【解决方案1】:

    你可以使用 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[results][person][0] 第二个人将在 $array[results][person][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

    【讨论】:

    • 它只给了我一组值——结果和数组??
    • 感谢您的耐心@hackartist - 我是新手。我的目标是取出每个名字、姓氏、地址等以显示在记录表中。阵列中的孩子们正在扔我。我是否应该将所有名字等放入一个数组中,然后创建表。如果是这样,我如何取出所有名字并将它们放入数组中?
    • 没关系,每个人在某些时候都是新手。我上面给出的代码会将所有内容扁平化为一个数组并将所有这些内容放入变量中......但我认为这根本不是你想要的,所以很抱歉我误解了。你想要什么列?这只是一个人的记录,桌子会同时显示很多人对吗?
    • 是的——确切地说,这只是一个,但大多数查询会有很多记录。我希望显示列 - firstName、lastName、Email。如果我有这些,我应该能够弄清楚其余的。非常感谢!
    • 好的,希望上面的编辑有助于解释一切。如果这仍然不是你想要的,给我一个你想要的确切输出的例子,我会告诉你如何得到它。祝你好运。
    猜你喜欢
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多