通过元素值引用带有PHP的JSON对象

时间:2017-04-05 23:17:40

标签: php arrays json

我不确定我是否正确地提出了这个问题,但现在就这样了。

我有一个看起来或多或少的JSON文件:

[{
"programName": "Entrepreneurial Skills",
"description": "The Certificate in Entrepreneurial Skills 1 provides you, as a small business owner/operator, with the essential skills and competitive strategies to help your enterprise thrive.",
"faculty": "School of Business and Economics",
"department": "Marketing, International Business and Entrepreneurship",
"id": 79,
"parentId": 0,
"relatedIds": [3, 4, 5, 16, 26, 27],
"tabGroup": 0,
"credentialType": "Certificate",
"credentialName": "Certificate in Entrepreneurial Skills",
"programCode": "",
"programOption": "",
"delivery": "distance",
"campus": "",
"length": { "credits": 15, "courses": 0, "weeks": 0, "months": 0, "years": 0, "varies": false },
"intakeDates": "",
"internationalFriendly": false,
"careers": "",
"priorityResult": false,
"url": "distance/programs/business-management/certificate-in-entrepreneurial-skills-1",
"imageUrl": "",
"tags": ""
}, {
"programName": "Environmental Economics and Management",
"description": "Attain a broad knowledge of the business environment, advanced management skills and specialized knowledge in environmental economics and sustainability.",
"faculty": "School of Business and Economics",
"department": "Economics",
"id": 80,
"parentId": 0,
"relatedIds": [45,67,88],
"tabGroup": 4,
"credentialType": "Master",
"credentialName": "Master in Environmental Economics and Management",
"programCode": "MEEM",
"programOption": "",
"delivery": "campus",
"campus": "",
"length": { "credits": 0, "courses": 0, "weeks": 0, "months": 0, "years": 2, "varies": false },
"intakeDates": "",
"internationalFriendly": true,
"careers": ["Economic sustainable management"],
"priorityResult": true,
"url": "programs/catalogue/masters-degrees-environmental-economics-and-management",
"imageUrl": "meem-msceem-banner39755.jpg",
"tags": ""
},{ etc }]

我将该文件放入如下数组:

    $programData = json_decode($json, true);

然后逐步执行并将其存储在将以HTML格式显示的$ content变量中。它或多或少看起来像这样(简化):

foreach ($programData as $key => $value) {
$content.='<h4 class="credentialName">'.$value['credentialName'].'</h4>';
$content.='<p class="lead">'.$value['description'].'</p>';
etc...
}

在那个foreach中,我达到了一个点,我有另一个foreach通过相关的程序项目。这些是整数,用于匹配 - 并显示 - JSON数组中相关ID的URL /标题。 这些ID与JSON数组中项目的键不匹配。这是我遇到问题的地方。如何找到$programData - &gt;的密钥? $id 3(例如),然后从$url中获取该项目的$programNameforeach

我用谷歌搜索并溢出到第n度,我很难过。任何帮助将非常感谢!

1 个答案:

答案 0 :(得分:1)

我想您可以对数组进行后期处理,以便从项目中获取这些ID:

<?php
foreach($programData as $k=>$v) {
    $newProgramData[$v["id"]] = $v;
}

现在,您的程序已按其$newProgramData中的ID编制索引。

示例代码:

<?php
$json = '[{"id":45,"name":"foo"},{"id":234,"name":"bar"},{"id":52,"name":"baz"}]';
$data = json_decode($json, true);
foreach ($data as $v) { $newData[$v["id"]] = $v; }
print_r($newData);

输出:

Array
(
    [45] => Array
        (
            [id] => 45
            [name] => foo
        )

    [234] => Array
        (
            [id] => 234
            [name] => bar
        )

    [52] => Array
        (
            [id] => 52
            [name] => baz
        )

)