1

I'm not sure if I'm even asking the question correctly, but here goes.

I have a JSON file that looks more or less like this:

[{
"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 }]

I am putting that file into an array like so:

    $programData = json_decode($json, true);

and then subsequently stepping through it and storing it in a $content variable that will be displayed in HTML. It more or less looks like this (simplified):

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

Within that foreach, I reach a point where I have another foreach going through the relatedProgram items. These are integers that are meant to match up - and display - the URL/title of the related ID in the JSON array. These IDs do not match the key of the item in the JSON array. This is where I'm having a problem. How do I find the key of the $programData -> $id 3 (for example), then get the $url and $programName from that item, from within the foreach?

I've googled and overflowed to the nth degree and I'm stumped. Any help would be very appreciated!

5
  • Possible duplicate of How do I extract data from JSON with PHP? Commented Apr 5, 2017 at 23:20
  • you mean where's the id key?, haven't you tried $value['id'] its under there, then make your if block Commented Apr 5, 2017 at 23:21
  • Well, that's faulty design. Use the ID's on the outer row and then create a function or use array_filter() to search elements of the outer list, returning on the row that has the ID you're looking for. Commented Apr 5, 2017 at 23:45
  • Very possibly faulty design - this is my first time trying this, so I was mostly winging it when building the JSON. I had looked online at a variety of sites that explained the schema, but it didn't seem apparent a 'best practice' approach to this particular setup, so I just did what I thought was right at the time. Commented Apr 6, 2017 at 15:40
  • @Ghost I meant - I want the numeric locations in the original array $programData where the $relatedIds (3, 4, 5, etc) match the $id. Basically, I want to be able to grab the data from the program where the id is 3 and I thought the easiest way would be to find the key of that element within the full array - for example, it would be [2]. Then I could simply refer to it within my second foreach, echoing that to the screen. I hope that makes it more clear Commented Apr 6, 2017 at 15:59

1 Answer 1

1

I suppose you could post-process your array to get those IDs out of the items:

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

Now your programs are indexed by their ID in $newProgramData.

Sample code:

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

Output:

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

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

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

)
Sign up to request clarification or add additional context in comments.

2 Comments

going to give this a try and see if I can make it work. Thanks!
This is exactly what I needed! Worked flawlessly, thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.