469,326 Members | 1,320 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

Multi-Dimensional Arrays Help - And Other Questions on Arrays

348 100+
For some reason, I have always had a hard time understanding arrays as they pertain to php and databases. I understand associative arrays just fine but when there are multidimensional arrays, I kinda don't.

I have gone over a few different examples but they were limited. I was able to find one piece of code that I would like to disect and ask questions about so I can gain a better understanding.

Expand|Select|Wrap|Line Numbers
  1. $characters = array
  2. (
  3.   array ( name=>"name 1"
  4.   , occupation=>"developer"
  5.   , age=>30
  6.   , specialty=>"Java"
  7.   ),
  8.   array
  9.   (
  10.     name=>"name 2"
  11.     , occupation=>"Programmer"
  12.     , age=>24
  13.     , specialty=>"C++"
  14.   ),
  15.   array
  16.   (
  17.   name=>"name 3"
  18.   , occupation=>"designer"
  19.   , age=>63
  20.   , specialty=>"Javascript"
  21.   )
  22. );
  23.  
  24. foreach ($characters as $val)
  25. {
  26.   foreach ($val as $key=>$final_val)
  27.   {
  28.     print "$key: $final_val<br>";
  29.   }
  30.   print "<br>";
  31. }
In this code, the way I am reading this is that there are 3 "rows"??? or blocks of data. Each one of these rows or blocks has several other rows inside of it. I don't have a problem with the arrays per se but more the foreach loop. If I am incorrect about these 3 arrays being rows, please feel free to correct me.

On the foreach, can someone please tell me exactly how and why it is set up the way it is? Specifically, I don't understand why the coder didn't use a key/value pair. He only uses a value, then inside the loop he uses $key=>$final_val. What I need to understand is why and when to refer to or use the key value pair and when not to. I have also seen code written inside the foreach loop like so: $key['something'] = $val;

What is that? What exactly does it do? If any one can help me to understand these, I would be forever grateful. I have pulled my hair out for the last time on drawing my data out of an array.

Thanks.
Apr 21 '09
110 5622
fjm
348 100+
lol... Wow.. I never posted so many times in a forum in my life. I think my record was 30 something and that was also on this forum. This one is the record though.

Ok, I am going to try and sleep for a little while. A sincere thanks to both of you guys! I am also putting the both of you on my forum buddy list. :)
Apr 21 '09 #101
Ciary
247 Expert 100+
ok cya later fjm and good night/morning :)
Apr 21 '09 #102
Dormilich
8,651 Expert Mod 8TB
rest well not dreaming of arrays… they have the possibility to haunt programmer's dreams :)
Apr 21 '09 #103
fjm
348 100+
Thanks a buch guys! I will give my brain a few hours off and then start all over again. :)

Talk to you guys soon.

Frank
Apr 21 '09 #104
fjm
348 100+
Daddy, look! NO HANDS! :P~

Expand|Select|Wrap|Line Numbers
  1. $colors = array
  2. (
  3.   array
  4.   (
  5.     'black' => 'sky',
  6.     'blue' => 'ocean',
  7.     'pink' => 'san francisco'
  8.   )
  9. );
  10.  
  11. echo $colors['0']['blue'];
  12.  
For any other poor soul having a hard time with arrays, look at it like this..

Expand|Select|Wrap|Line Numbers
  1.             $colors['0']['blue'];
  2. foreach--------^ key-^----^ value
Yields Ocean. ;)

Now kiddies.. when all I need is a single value, instead of using a foreach loop and typing 3 extra lines, I can just type one by using this. ;)

I still may not understand Atli's monster 7D array but I am sure well on my way. Now when I get an undefined index notice, it actually MEANS something to me.

As elementary as arrays may seem to most, I honestly struggled with them but I really feel like I have a much better handle on what is happening and what to do to troubleshoot one and so, I have achieved my goal here thanks to Dormilich, Ciary and Markus.
Apr 22 '09 #105
Dormilich
8,651 Expert Mod 8TB
some improvement on the code
Expand|Select|Wrap|Line Numbers
  1. $colors[0]['blue'];
  2.         ^--- use integers for numeric arrays
Apr 22 '09 #106
fjm
348 100+
@Dormilich
Thanks Dormilich.. Isn't a 0 an integer?
Apr 22 '09 #107
Dormilich
8,651 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1.  0  // integer
  2. '0' // string
Apr 22 '09 #108
fjm
348 100+
@Dormilich
Exactly.. That's why I'm a bit confused. I'm not using quotes which should make it an int. No?
Apr 22 '09 #109
fjm
348 100+
Nevermind.... I see it and your right. Thanks Dormi... :)
Apr 22 '09 #110
Dormilich
8,651 Expert Mod 8TB
sometimes those type changes can make all the difference in the world (I just got prodded to that yesterday)
Apr 22 '09 #111

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by OutsiderJustice | last post: by
37 posts views Thread by ajikoe | last post: by
4 posts views Thread by Frank Jona | last post: by
23 posts views Thread by Kaz Kylheku | last post: by
17 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.