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

标签 php json foreach iteration

我试图弄清楚如何迭代一个已解码的 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] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89e4efe6fbedc9ece4e8e0e5a7eae6e4" rel="noreferrer noopener nofollow">[email protected]</a> [searchCommunicationValue] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b363d34293f1b3a3f2d3e352f2e293e38332932282f323a357534293c" rel="noreferrer noopener nofollow">[email protected]</a> [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 然后只需使用 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 .

关于php - 如何在 PHP 中迭代数组并获取键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8440527/

相关文章:

javascript - 以数组的形式存储来自 append 表单数据的 php 变量

php - 访问数组中的类实例

php - 主键 ID 字段自动递增不允许 JSON 导入。 PHP/PDO 和 MySQL

javascript - jQuery 集合在每个循环内被破坏(删除 elem,恢复innerHTML)

php - 循环数组时返回奇怪的值

c# - 从 C# 发送一个字符串插入到 PHP 文件中,以便立即在 mysql select 查询中使用并返回结果

php - Yii2 GridView 过滤 sql

java - 如何将这些 servlet 数据创建为 json 格式我尝试过这个东西

java - 如何创建一个JSON文件,用Java从数据库中取出数据?

javascript - 在 forEach 循环后显示 HTML 时出现问题