arrays - Reference JSON object w/PHP by element value -


i'm not sure if i'm asking question correctly, here goes.

i have json file looks more or less this:

[{ "programname": "entrepreneurial skills", "description": "the certificate in entrepreneurial skills 1 provides you, small business owner/operator, essential skills , competitive strategies enterprise thrive.", "faculty": "school of business , economics", "department": "marketing, international business , 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 , management", "description": "attain broad knowledge of business environment, advanced management skills , specialized knowledge in environmental economics , sustainability.", "faculty": "school of business , economics", "department": "economics", "id": 80, "parentid": 0, "relatedids": [45,67,88], "tabgroup": 4, "credentialtype": "master", "credentialname": "master in environmental economics , 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 putting file array so:

    $programdata = json_decode($json, true); 

and subsequently stepping through , storing in $content variable displayed in html. more or less looks (simplified):

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

within foreach, reach point have foreach going through relatedprogram items. these integers meant match - , display - url/title of related id in json array. these ids not match key of item in json array. i'm having problem. how find key of $programdata -> $id 3 (for example), $url , $programname item, within foreach?

i've googled , overflowed nth degree , i'm stumped. appreciated!

i suppose post-process array ids out of items:

<?php foreach($programdata $k=>$v) {     $newprogramdata[$v["id"]] = $v; } 

now programs indexed 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 $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         )  ) 

Comments