472,334 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,334 software developers and data experts.

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 6484
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,658 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,658 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,658 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,658 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

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

Similar topics

4
by: OutsiderJustice | last post by:
Hi All, I can not find any information if PHP support multi-thread (Posix thread) or not at all, can someone give out some information? Is it...
37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly...
6
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME ...
5
by: dkelly925 | last post by:
Is there a way to add an If Statement to the following code so if data in a field equals "x" it will launch one report and if it equals "y" it...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an...
17
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID"...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.