Adam2500 Posted February 22 Share Posted February 22 I need to generate an array exactly like the example, but its not working. Im using the following code: $res = prepareALL("SELECT c.id, c.name, f.id as fid, f.name as fname, f.link as flink, f.thumbnail as fthumbnail FROM video_categories c left outer join videos f on c.id=f.category", array()); $categories = array(); foreach($res as $videos){ $categories[$videos['id']]['id'] = $videos['id']; $categories[$videos['id']]['name'] = $videos['name']; $categories[$videos['id']]['videos'][] = array( 'id' => $videos['fid'], 'name' => $videos['fname'], 'link' => $videos['flink'], 'thumbnail' => $videos['fthumbnail'] ); } var_dump($categories); This is outputting array(2) { [1]=> array(3) { ["id"]=> string(1) "1" ["name"]=> string(14) "Category 1" ["videos"]=> array(3) { [0]=> array(4) { ["id"]=> string(1) "2" ["name"]=> string(17) "Song Name" ["link"]=> string(61) "https://www.youtube.com" ["thumbnail"]=> string(73) "https://website.com/thumbail.jpg" } [1]=> array(4) { ["id"]=> string(1) "1" ["name"]=> string(16) "Song Name" ["link"]=> string(61) "https://www.youtube.com" ["thumbnail"]=> string(76) "https://website.com/thumbail.jpg" } } } [2]=> array(3) { ["id"]=> string(1) "2" ["name"]=> string(10) "Category 2" ["videos"]=> array(1) { [0]=> array(4) { ["id"]=> string(1) "4" ["name"]=> string(12) "Song Name" ["link"]=> string(61) "https://www.youtube.com" ["thumbnail"]=> string(72) "https://website.com/thumbail.jpg" } } } } I need it to export exactly like this though: [ { "catName": "Category 1", "catID": "1", "videos": [ { "vidName": "Song Name", "vidThumbnail": "https://website.com/thumbnail.jpg", "vidID": "3", "vidLink": "https://www.youtube.com" }, { "vidName": "Song Name", "vidThumbnail": "https://website.com/thumbnail.jpg", "vidID": "3", "vidLink": "https://www.youtube.com" } ] }, { "catName": "category 2", "catID": "2", "videos": [ { "vidName": "Song Name", "vidThumbnail": "https://website.com/thumbnail.jpg", "vidID": "", "vidLink": "https://www.youtube.com" }, { "vidName": "Song Name", "vidThumbnail": "https://website.com/thumbnail.jpg", "vidID": "", "vidLink": "https://www.youtube.com" } ] } ] Quote Link to comment https://forums.phpfreaks.com/topic/318447-multidimensional-array/ Share on other sites More sharing options...
Barand Posted February 22 Share Posted February 22 Try $categories = []; foreach ($res as $r) { if (!isset($categories[$r['id']])) { $categories[$r['id']] = [ 'catid' => $r['id'], 'catname' => $r['name'], 'videos' => [] ]; } $categories[$r['id']]['videos'][] = [ 'vidname' => $r['fname'], 'vidthumbnail' => $r['fthumbnail'], 'vidid' => $r['fid'], 'vidlink' => $r['flink'] ]; } $result = json_encode($categories); Quote Link to comment https://forums.phpfreaks.com/topic/318447-multidimensional-array/#findComment-1616358 Share on other sites More sharing options...
Adam2500 Posted February 23 Author Share Posted February 23 (edited) Thank you for the reply, it didnt seem to work though and is giving a different format than the one needed. array(2) { [1]=> array(3) { ["catid"]=> string(1) "1" ["catname"]=> string(14) "Category 1" ["videos"]=> array(3) { [0]=> array(4) { ["vidname"]=> string(29) "Song Name" ["vidthumbnail"]=> string(73) "https://website.com/thumbnail.jpg" ["vidid"]=> string(1) "3" ["vidlink"]=> string(61) "https://www.youtube.com" } [1]=> array(4) { ["vidname"]=> string(17) "Song Name" ["vidthumbnail"]=> string(73) "https://website.com/thumbnail.jpg" ["vidid"]=> string(1) "2" ["vidlink"]=> string(61) "https://www.youtube.com" } [2]=> array(4) { ["vidname"]=> string(16) "Song Name" ["vidthumbnail"]=> string(76) "https://website.com/thumbnail.jpg" ["vidid"]=> string(1) "1" ["vidlink"]=> string(61) "https://www.youtube.com" } } } [2]=> array(3) { ["catid"]=> string(1) "2" ["catname"]=> string(10) "Category 2" ["videos"]=> array(1) { [0]=> array(4) { ["vidname"]=> string(12) "Song Name" ["vidthumbnail"]=> string(72) "https://website.com/thumbnail.jpg" ["vidid"]=> string(1) "4" ["vidlink"]=> string(61) "https://www.youtube.com" } } } } Edited February 23 by Adam2500 Quote Link to comment https://forums.phpfreaks.com/topic/318447-multidimensional-array/#findComment-1616427 Share on other sites More sharing options...
Barand Posted February 23 Share Posted February 23 It won't be a million miles out. Try changing the last line of my code to $result = json_encode(array_values($categories)); Quote Link to comment https://forums.phpfreaks.com/topic/318447-multidimensional-array/#findComment-1616439 Share on other sites More sharing options...
Adam2500 Posted February 23 Author Share Posted February 23 Many thanks Barand, its still not the correct format though. This is what its returning: array(2) { [1]=> array(3) { ["catid"]=> string(1) "1" ["catname"]=> string(14) "Category 1" ["videos"]=> array(3) { [0]=> array(4) { ["vidname"]=> string(29) "Song Name" ["vidthumbnail"]=> string(73) "https://webiste.com/thumbnail.jpg" ["vidid"]=> string(1) "3" ["vidlink"]=> string(61) "https://www.youtube.com/" } [1]=> array(4) { ["vidname"]=> string(17) "Song Name" ["vidthumbnail"]=> string(73) "https://webiste.com/thumbnail.jpg" ["vidid"]=> string(1) "2" ["vidlink"]=> string(61) "https://www.youtube.com" } [2]=> array(4) { ["vidname"]=> string(16) "Song Name" ["vidthumbnail"]=> string(76) "https://webiste.com/thumbnail.jpg" ["vidid"]=> string(1) "1" ["vidlink"]=> string(61) "https://www.youtube.com" } } } [2]=> array(3) { ["catid"]=> string(1) "2" ["catname"]=> string(10) "Category 2" ["videos"]=> array(1) { [0]=> array(4) { ["vidname"]=> string(12) "Song Name" ["vidthumbnail"]=> string(72) "https://webiste.com/thumbnail.jpg" ["vidid"]=> string(1) "4" ["vidlink"]=> string(61) "https://www.youtube.com" } } } } Quote Link to comment https://forums.phpfreaks.com/topic/318447-multidimensional-array/#findComment-1616463 Share on other sites More sharing options...
Barand Posted February 23 Share Posted February 23 OK, I give up guessing. What is wrong with it? Quote Link to comment https://forums.phpfreaks.com/topic/318447-multidimensional-array/#findComment-1616465 Share on other sites More sharing options...
gizmola Posted April 8 Share Posted April 8 Your desired format is json. You keep posting the output from a var_dump of a php array. Do you understand the difference between json and a php array, and that var_dump is just a diagnostic tool for seeing what php arrays and objects look like internally? You must json_encode() the data as Barand already instructed. Take the output you get from json_encoding and paste that into a code block for any future discussion please. Quote Link to comment https://forums.phpfreaks.com/topic/318447-multidimensional-array/#findComment-1620649 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.