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

Apr 21 '09
110 6484
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
247 Expert 100+
ok cya later fjm and good night/morning :)
Apr 21 '09 #102
8,658 Expert Mod 8TB
rest well not dreaming of arrays… they have the possibility to haunt programmer's dreams :)
Apr 21 '09 #103
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.

Apr 21 '09 #104
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. );
  11. echo $colors['0']['blue'];
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
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
348 100+
Thanks Dormilich.. Isn't a 0 an integer?
Apr 22 '09 #107
8,658 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1.  0  // integer
  2. '0' // string
Apr 22 '09 #108
348 100+
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
348 100+
Nevermind.... I see it and your right. Thanks Dormi... :)
Apr 22 '09 #110
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

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...
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...
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...
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...
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 ...
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...
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...
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...
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008...
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"...
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...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
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...
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...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
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. ...
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...
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...
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.