nested arrays in php

  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

is it possible to make nested arrays in php? i know it is possible in javascript b/c ive done it before using that... but i need to know how to do it in PHP...
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

PHP Code: [ Select ]
$arr = array();
 
$arr[] = array('term' => 'apple',
 
   'definition' => 'fruit');
 
$arr[] = array('term' => 'cucumber',
 
   'definition' => 'vegetable');
 
$arr[] = array('term' => 'banana',
 
   'definition' => 'fruit');
  1. $arr = array();
  2.  
  3. $arr[] = array('term' => 'apple',
  4.  
  5.    'definition' => 'fruit');
  6.  
  7. $arr[] = array('term' => 'cucumber',
  8.  
  9.    'definition' => 'vegetable');
  10.  
  11. $arr[] = array('term' => 'banana',
  12.  
  13.    'definition' => 'fruit');


Think that's what you're looking for
  • CazpianXI
  • Proficient
  • Proficient
  • User avatar
  • Posts: 285

Post 3+ Months Ago

Actually, this213, the code you posted is not a nested array.

Here's how you do a nested array in PHP:

PHP Code: [ Select ]
 
$var = array(
 
array(1, 2, 3, 4, 5),
 
array(12, 13, 14, 15),
 
array("item", "another item", "yet another item")
 
);
 
 
  1.  
  2. $var = array(
  3.  
  4. array(1, 2, 3, 4, 5),
  5.  
  6. array(12, 13, 14, 15),
  7.  
  8. array("item", "another item", "yet another item")
  9.  
  10. );
  11.  
  12.  
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

bear with me, I've only been doing php for a week
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Aren't those are just two different manners of declaration? Don't both yield mutli-dimensional arrays (albeit the first is associative and the second numeric)?

If you were to rewrite the first as:

PHP Code: [ Select ]
$arr = array(
 
array('term' => 'apple', 'definition' => 'fruit'),
 
array('term' => 'cucumber', 'definition' => 'vegetable'),
 
array('term' => 'banana', 'definition' => 'fruit')
 
);
  1. $arr = array(
  2.  
  3. array('term' => 'apple', 'definition' => 'fruit'),
  4.  
  5. array('term' => 'cucumber', 'definition' => 'vegetable'),
  6.  
  7. array('term' => 'banana', 'definition' => 'fruit')
  8.  
  9. );


and the second as:

PHP Code: [ Select ]
$var = array();
 
$var[] = array(1, 2, 3, 4, 5);
 
$var[] = array(12, 13, 14, 15);
 
$var[] = array("item", "another item", "yet another item");
  1. $var = array();
  2.  
  3. $var[] = array(1, 2, 3, 4, 5);
  4.  
  5. $var[] = array(12, 13, 14, 15);
  6.  
  7. $var[] = array("item", "another item", "yet another item");


wouldn't they still result in arrays that have three elements each of which is itself an array?
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

As I stated before, I've only been coding php for about a week. If you hadn't noticed from my sig, I'm a Perl coder, so I see my example as being an array of hashes (or associative arrays), and the other as an array of arrays. Since, as far as I can tell, php handles all arrays as associative arrays (http://us3.php.net/is_array#35194}, I guess there wouldn't be much, if any, difference programatically as to how those arrays were initialized, although, I could be wrong.
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

thanks everyone...

i just have a few questions...

how do i access the information in the subarrays?

using this as an example:
PHP Code: [ Select ]
$var = array(
 
array(1, 2, 3, 4, 5),
 
array(12, 13, 14, 15),
 
array("item", "another item", "yet another item")
 
);
  1. $var = array(
  2.  
  3. array(1, 2, 3, 4, 5),
  4.  
  5. array(12, 13, 14, 15),
  6.  
  7. array("item", "another item", "yet another item")
  8.  
  9. );


how would i get the 14 out of it?
PHP Code: [ Select ]
$var[2[3]];

????????
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

funny you should ask that - I just went through an hour of trying just that!

this worked for me:
PHP Code: [ Select ]
foreach($array as $arrnum){
 
   foreach($arrnum as $key => $value){
 
      print "<br>$key is $value";
 
   }
 
}
  1. foreach($array as $arrnum){
  2.  
  3.    foreach($arrnum as $key => $value){
  4.  
  5.       print "<br>$key is $value";
  6.  
  7.    }
  8.  
  9. }

Though there may be a better way
  • CazpianXI
  • Proficient
  • Proficient
  • User avatar
  • Posts: 285

Post 3+ Months Ago

Calendae, the way you get info out of a nested array is like this:

PHP Code: [ Select ]
 
<?
 
echo $var[2][3];
 
?>
 
 
  1.  
  2. <?
  3.  
  4. echo $var[2][3];
  5.  
  6. ?>
  7.  
  8.  


Hope this helps.

~Cazpian the 11th
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

that would be for a multi dimensional array wouldnt it??? not a nested array...
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

If you want to get 14 out of it then you would use:

PHP Code: [ Select ]
<?
 
echo $var[1][2];
 
?>
  1. <?
  2.  
  3. echo $var[1][2];
  4.  
  5. ?>


The arrays are going to be indexed to zero rather than one. I think nested and multidimensional are synonymous in this usage, but I'm not positive.
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

oops, yeah, sry...

ok, im trying to do something like this, except in PHP (the following is in javascript), its for something completely different, but the organizational concept is the same...

Code: [ Select ]
            var year = new Array();
                year[0] = new gradYear();
                    year[0].gradYear = "2005";
                    year[0].student = new Array();
                        year[0].student[0] = new ident();
                        year[0].student[0].name = "Schrodinger, Cae";
                        year[0].student[0].userName = "schrodingerc";
                        year[0].student[1] = new ident();
                        year[0].student[1].name = "Delta, Gamma";
                        year[0].student[1].userName = "gammade";
                year[1] = new gradYear();
                    year[1].gradYear = "2006";
                    year[1].student = new Array();
                        year[1].student[0] = new ident();
                        year[1].student[0].name = "Blah, Bleh";
                        year[1].student[0].userName = "blahbl";
                year[2] = new gradYear();
                    year[2].gradYear = "2007";
                    year[2].student = new Array();
                        year[2].student[0] = new ident();
                        year[2].student[0].name = "Clue, No";
                        year[2].student[0].userName = "cluen";
            function gradYear(gradYear, student){
                this.gradYear = gradYear;
                this.student = student;
            }
            function ident(name, userName){
                this.name = name;
                this.userName = userName;
            }
  1.             var year = new Array();
  2.                 year[0] = new gradYear();
  3.                     year[0].gradYear = "2005";
  4.                     year[0].student = new Array();
  5.                         year[0].student[0] = new ident();
  6.                         year[0].student[0].name = "Schrodinger, Cae";
  7.                         year[0].student[0].userName = "schrodingerc";
  8.                         year[0].student[1] = new ident();
  9.                         year[0].student[1].name = "Delta, Gamma";
  10.                         year[0].student[1].userName = "gammade";
  11.                 year[1] = new gradYear();
  12.                     year[1].gradYear = "2006";
  13.                     year[1].student = new Array();
  14.                         year[1].student[0] = new ident();
  15.                         year[1].student[0].name = "Blah, Bleh";
  16.                         year[1].student[0].userName = "blahbl";
  17.                 year[2] = new gradYear();
  18.                     year[2].gradYear = "2007";
  19.                     year[2].student = new Array();
  20.                         year[2].student[0] = new ident();
  21.                         year[2].student[0].name = "Clue, No";
  22.                         year[2].student[0].userName = "cluen";
  23.             function gradYear(gradYear, student){
  24.                 this.gradYear = gradYear;
  25.                 this.student = student;
  26.             }
  27.             function ident(name, userName){
  28.                 this.name = name;
  29.                 this.userName = userName;
  30.             }
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

This is done by username rather than year. I would guess you'd have to make another entry in the array (or another array), such as array($year => user,list) though you could just as easily do

if($theyear == $users[$username][gradYear]) {
# Do Something
}

This is pretty much like Perl hash tables, btw. where
$users{$username{gradYear}} = gradYear;
I'm still trying to get used to using the same symbol for different data types too.

anyways:
PHP Code: [ Select ]
<?
 
$studentinfo = array();
 
$users = array(
 
   'schrodingerc' =>
 
      array('name' => 'Schrodinger, Cae','gradYear' => '2004'),
 
   'soonandsoforth' =>
 
      array('name' => 'Soforth, Soon','gradYear' => '2005')
 
);
 
$studentinfo = getnfo($users,'schrodingerc');
 
 
 
print "<br><br>$studentinfo[0]<br>";
 
print "Graduated In $studentinfo[1]<br>";
 
 
 
function getnfo($users,$username){
 
   $which = $users[armap][$username];
 
   $student = $users[$username][name];
 
   $gradyear = $users[$username][gradYear];
 
   $studentinfo = array($student,$gradyear);
 
   return $studentinfo;
 
}
 
?>
  1. <?
  2.  
  3. $studentinfo = array();
  4.  
  5. $users = array(
  6.  
  7.    'schrodingerc' =>
  8.  
  9.       array('name' => 'Schrodinger, Cae','gradYear' => '2004'),
  10.  
  11.    'soonandsoforth' =>
  12.  
  13.       array('name' => 'Soforth, Soon','gradYear' => '2005')
  14.  
  15. );
  16.  
  17. $studentinfo = getnfo($users,'schrodingerc');
  18.  
  19.  
  20.  
  21. print "<br><br>$studentinfo[0]<br>";
  22.  
  23. print "Graduated In $studentinfo[1]<br>";
  24.  
  25.  
  26.  
  27. function getnfo($users,$username){
  28.  
  29.    $which = $users[armap][$username];
  30.  
  31.    $student = $users[$username][name];
  32.  
  33.    $gradyear = $users[$username][gradYear];
  34.  
  35.    $studentinfo = array($student,$gradyear);
  36.  
  37.    return $studentinfo;
  38.  
  39. }
  40.  
  41. ?>
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

could i do something like this (no i havent uploaded and tried it yet, i dont want to write the interpretaion code if its not going to work... :P)

PHP Code: [ Select ]
<?php
 
   $masterMenu[0] = new menuItem();
 
      $masterMenu[0]->setItem(
 
         '<a href = "/main.php?page=e2004" class = "nav level1">Election 2004</a>',
 
         array()
 
         );
 
   $masterMenu[1] = new menuItem();
 
      $masterMenu[1]->setItem(
 
         '<a href = "/main.php?page=wow" class = "nav level1">World of Warcraft</a>',
 
         array(
 
            [0]=> new menuItem('<a href = "/main.php?page=wow&#8834;=races" class = "nav level2">Races</a>',
 
               array(
 
                  [0]=> new menuItem('<a href = "/main.php?page=wow&#8834;=races&side=alliance" class = "nav level3">The Alliance</a>',
 
                     array(
 
                        [0]=> new menuItem('<a href = "main.php?page=wow&#8834;=races&side=alliance&race=humans" class = "nav level4">Humans</a>',
 
                           array()
 
                           )
 
                        [1]=> new menuItem('<a href = "main.php?page=wow&#8834;=races&side=alliance&race=elves" class = "nav level4">Night Elves</a>',
 
                           array()
 
                        )
 
                        [2]=> new menuItem('<a href = "main.php?page=wow&#8834;=races&side=alliance&race=dwarves" class = "nav level4">Dwarves</a>',
 
                           array()
 
                        )
 
                        [3]=> new menuItem)'<a href = "main.php?page=wow&#8834;=races&side=alliance&race=gnomes" class = "nav level4">Gnomes</a>',
 
                           array()
 
                        )
 
                     )
 
                  )
 
                  [1]=> new menuItem('<a href = "/main.php?page=wow&#8834;=races&side=horde" class = "nav level3">The Horde</a>',
 
                     array(
 
                        [0]=> new menuItem('<a href = "main.php?page=wow&#8834;=races&side=horde&race=orcs" class = "nav level4">Orcs</a>',
 
                           array()
 
                        )
 
                        [1]=> new menuItem('<a href = "main.php?page=wow&#8834;=races&side=horde&race=trolls" class = "nav level4">Trolls</a>',
 
                           array()
 
                        )
 
                        [2]=> new menuItem('<a href = "main.php?page=wow&#8834;=races&side=horde&race=tauren" class = "nav level4">Tauren</a>',
 
                           array()
 
                        )
 
                        [3]=> new menuItem('<a href = "main.php?page=wow&#8834;=races&side=horde&race=undead" class = "nav level4">Undead</a>',
 
                           array()
 
                        )
 
                     )
 
                  )
 
               )
 
            )
 
            [1]=> new menuItem('<a href = "/main.php?page=wow&#8834;=classes" class = "nav level2">Classes</a>',
 
               array()
 
            )
 
            [2]=> new menuItem('<a href = "/main.php?page=wow&#8834;=skills" class = "nav level2">Skills</a>',
 
               array()
 
            )
 
            [3]=> new menuItem('<a href = "/main.php?page=wow&#8834;=items" class = "nav level2">Items</a>',
 
               array()
 
            )
 
            [4]=> new menuItem('<a href = "/main.php?page=wow&#8834;=sasics" class = "nav level2">Basics</a>',
 
               array()
 
            )
 
         )
 
      );
 
   
 
   
 
   class menuItem{
 
      function setItem($alpha, $beta){
 
         $this->menuItem = $alpha;
 
         $this->subMenu = $beta;
 
      }
 
      function getMenuItem(){
 
         return $this->menuItem;
 
      }
 
      function getSubMenu(){
 
         return $this->subMenu;
 
      }
 
   }
 
?>
  1. <?php
  2.  
  3.    $masterMenu[0] = new menuItem();
  4.  
  5.       $masterMenu[0]->setItem(
  6.  
  7.          '<a href = "/main.php?page=e2004" class = "nav level1">Election 2004</a>',
  8.  
  9.          array()
  10.  
  11.          );
  12.  
  13.    $masterMenu[1] = new menuItem();
  14.  
  15.       $masterMenu[1]->setItem(
  16.  
  17.          '<a href = "/main.php?page=wow" class = "nav level1">World of Warcraft</a>',
  18.  
  19.          array(
  20.  
  21.             [0]=> new menuItem('<a href = "/main.php?page=wow&#8834;=races" class = "nav level2">Races</a>',
  22.  
  23.                array(
  24.  
  25.                   [0]=> new menuItem('<a href = "/main.php?page=wow&#8834;=races&side=alliance" class = "nav level3">The Alliance</a>',
  26.  
  27.                      array(
  28.  
  29.                         [0]=> new menuItem('<a href = "main.php?page=wow&#8834;=races&side=alliance&race=humans" class = "nav level4">Humans</a>',
  30.  
  31.                            array()
  32.  
  33.                            )
  34.  
  35.                         [1]=> new menuItem('<a href = "main.php?page=wow&#8834;=races&side=alliance&race=elves" class = "nav level4">Night Elves</a>',
  36.  
  37.                            array()
  38.  
  39.                         )
  40.  
  41.                         [2]=> new menuItem('<a href = "main.php?page=wow&#8834;=races&side=alliance&race=dwarves" class = "nav level4">Dwarves</a>',
  42.  
  43.                            array()
  44.  
  45.                         )
  46.  
  47.                         [3]=> new menuItem)'<a href = "main.php?page=wow&#8834;=races&side=alliance&race=gnomes" class = "nav level4">Gnomes</a>',
  48.  
  49.                            array()
  50.  
  51.                         )
  52.  
  53.                      )
  54.  
  55.                   )
  56.  
  57.                   [1]=> new menuItem('<a href = "/main.php?page=wow&#8834;=races&side=horde" class = "nav level3">The Horde</a>',
  58.  
  59.                      array(
  60.  
  61.                         [0]=> new menuItem('<a href = "main.php?page=wow&#8834;=races&side=horde&race=orcs" class = "nav level4">Orcs</a>',
  62.  
  63.                            array()
  64.  
  65.                         )
  66.  
  67.                         [1]=> new menuItem('<a href = "main.php?page=wow&#8834;=races&side=horde&race=trolls" class = "nav level4">Trolls</a>',
  68.  
  69.                            array()
  70.  
  71.                         )
  72.  
  73.                         [2]=> new menuItem('<a href = "main.php?page=wow&#8834;=races&side=horde&race=tauren" class = "nav level4">Tauren</a>',
  74.  
  75.                            array()
  76.  
  77.                         )
  78.  
  79.                         [3]=> new menuItem('<a href = "main.php?page=wow&#8834;=races&side=horde&race=undead" class = "nav level4">Undead</a>',
  80.  
  81.                            array()
  82.  
  83.                         )
  84.  
  85.                      )
  86.  
  87.                   )
  88.  
  89.                )
  90.  
  91.             )
  92.  
  93.             [1]=> new menuItem('<a href = "/main.php?page=wow&#8834;=classes" class = "nav level2">Classes</a>',
  94.  
  95.                array()
  96.  
  97.             )
  98.  
  99.             [2]=> new menuItem('<a href = "/main.php?page=wow&#8834;=skills" class = "nav level2">Skills</a>',
  100.  
  101.                array()
  102.  
  103.             )
  104.  
  105.             [3]=> new menuItem('<a href = "/main.php?page=wow&#8834;=items" class = "nav level2">Items</a>',
  106.  
  107.                array()
  108.  
  109.             )
  110.  
  111.             [4]=> new menuItem('<a href = "/main.php?page=wow&#8834;=sasics" class = "nav level2">Basics</a>',
  112.  
  113.                array()
  114.  
  115.             )
  116.  
  117.          )
  118.  
  119.       );
  120.  
  121.    
  122.  
  123.    
  124.  
  125.    class menuItem{
  126.  
  127.       function setItem($alpha, $beta){
  128.  
  129.          $this->menuItem = $alpha;
  130.  
  131.          $this->subMenu = $beta;
  132.  
  133.       }
  134.  
  135.       function getMenuItem(){
  136.  
  137.          return $this->menuItem;
  138.  
  139.       }
  140.  
  141.       function getSubMenu(){
  142.  
  143.          return $this->subMenu;
  144.  
  145.       }
  146.  
  147.    }
  148.  
  149. ?>
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

anyone?
  • CazpianXI
  • Proficient
  • Proficient
  • User avatar
  • Posts: 285

Post 3+ Months Ago

I took a glance at your code. Are you asking if it'll work? It looks like it will... but I'd suggest uploading it and trying it out for yourself. If you get any errors, just post them here...
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

ok, ty, i just wanted to know if i could do it because in the past ive gone out and writtten a bunch of code, and fought with it... and then gone and asked my computer teacher what i was doing wrong, and found out that i couldnt actually do what i was trying to do... :S:p
  • Egeste
  • Born
  • Born
  • Egeste
  • Posts: 1

Post 3+ Months Ago

array(
"user_email" => array(true, 255),
"user_fn" => array(true, 45),
"user_mi" => array(false, 45),
"user_ln" => array(true, 45),
"user_fln" => array(false, 45),
"dob_year" => array(true, 4),
"dob_month" => array(true, 2),
"dob_day" => array(true, 2),
"user_street" => array(true, 255),
"user_city" => array(true, 128),
"user_poldiv" => array(true, 128),
"user_postal" => array(true, 45),
"user_country" => array(true, 128),
"user_hpn" => array(true, 45),
"user_wpn" => array(false, 45),
"user_tos" => array(true, "on")
);

Post Information

  • Total Posts in this topic: 18 posts
  • Users browsing this forum: No registered users and 88 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.