Jump to content

Multidimensional Array


Adam2500

Recommended Posts

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 by Adam2500
Link to comment
Share on other sites

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"
      }
    }
  }
}

 

Link to comment
Share on other sites

  • 1 month later...

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.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.