473,856 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multi-Dimensional Arrays Help - And Other Questions on Arrays

348 Contributor
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 multidimensiona l 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_va l. 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 7060
fjm
348 Contributor
@Ciary
Maybe I have a better example. Are these MD arrays kind of like nested tables? If yes, then I can certanly picture it. :)
Apr 21 '09 #11
Ciary
247 Recognized Expert New Member
a table is a 2 dimentional array. and all other MD arrays are exacly as nested tables :)
Apr 21 '09 #12
Dormilich
8,658 Recognized Expert Moderator Expert
@fjm
that means: report all errors except notices and compatibility warnings

there's a whole section explaining this *nudgenudge*

@fjm
well, yes. although it gets complicated for 3-dimensionals and unimaginable for more than 4 dimensions.
Apr 21 '09 #13
fjm
348 Contributor
Wow guys... OK... So let me recap... So we have a 2D array that resembles a table. Then we have a higher level of dimensions that we can say looks like nested tables. Kind of like sayins that each cell can have an array inside of it and it just keeps going from there. Maybe an example.

Expand|Select|Wrap|Line Numbers
  1. <table>  <-- array 1
  2.   <tr>
  3.     <td>
  4.       <table>  <-- array 2
  5.         <tr>
  6.           <td>
  7.             <table>  <-- array 3
  8.               <tr>
  9.                 <td>
  10.                   Etc..
  11.                 </td>
  12.               </tr>
  13.             </table>
  14.           </td>
  15.         </tr>
  16.       </table> 
  17.     </td>
  18.   </tr>
  19. </table>
  20.  
Am I correct? 3D array?
Apr 21 '09 #14
fjm
348 Contributor
@Dormilich
OK, you win. :) Actually, yesterday I wrote some code and knew that I should have gotten an undefined index but didn't. I was wondering why and figured that it had something to do with the new changes I made.

It's really strange because with those ini values, I now get new messages that I didn't get before like the usual date() timezone error so I thought that it was a stricter ruleset. Thanks for the heads up Dormilich! If I wanted to have a strict set of rules, would I just ise E_ALL? (I'll still read the link you provided.)
Apr 21 '09 #15
Dormilich
8,658 Recognized Expert Moderator Expert
@fjm
that's the crucial point.
Apr 21 '09 #16
Dormilich
8,658 Recognized Expert Moderator Expert
@fjm
yup.

(blablablubb…20 character limit exeeded…narf)
Apr 21 '09 #17
Ciary
247 Recognized Expert New Member
take a look at this: its a visual example of a 4D array. hope this helps.


and indeed you create it by putting an array in a array. and that one in another array and that one in another and ... (you get the poin :) )
Apr 21 '09 #18
fjm
348 Contributor
@Dormilich
Scary thought because each cell or array can have an infinite number of arrays. It blows my mind to even imagine it.

Ok, so if I can just bring this into perspective and maybe have you guys guide me a bit now that I can visualize it. Let's say I have an array like this:

Expand|Select|Wrap|Line Numbers
  1. array
  2. (
  3.   array
  4.   (
  5.     'mykey0' => 'myvalue0',
  6.     'mykey1' => 'myvalue1',
  7.     'mykey2' => 'myvalue2'
  8.   ),
  9.   array
  10.   (
  11.     '0' => 'somthin',
  12.     '1' => 'somthin',
  13.     '2' => 'somthin',
  14.     array('0' =>'somethin here')
  15.   )
  16. )
  17.  
OK. I have a MD array here with a nested array. (Not sure what to call it) How would I refer to the nested array with a foreach?
Apr 21 '09 #19
fjm
348 Contributor
@Ciary
Great example Ciary, thanks. Is this a 4D array because each box has 4 columns or because each box has 4 columns? How did you determine that it was 4?

Please tell me that I wouldn't ever encounter an array like this. :)
Apr 21 '09 #20

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

Similar topics

4
14582
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 supported? If yes, where's the info? If no, is it possible to make doing multi-thread stuff? Thanks. YF
37
4916
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 which cpu shoud do every thread? Sincerely Yours, Pujo
4
4684
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 2003 Regards Frank
6
8192
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 (A.DLL+A.NETMODULE)?
6
4904
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 The other multi-list box displays courses based on a table called COURSES. This table has 2 fields CATEGORY_ID, COURSE_NAME. The CATEGORY_ID is a FK in COURSES and a PK in CATEGORIES. I want to populate the course list box based on any category(s)
5
3293
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 would open another report. Anyone know how to modify this? Private Sub cmdPreview_Click() On Error GoTo Err_Handler 'Purpose: Open the report filtered to the items selected in the list box. 'Author: Allen J Browne, 2004. http://allenbrowne.com Dim varItem As Variant 'Selected items
23
5353
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 unsolvable puzzle. So for the last 15 minutes I applied myself to this problem and come up with this off-the-wall proposal for you people. Perhaps this idea has been proposed before, I don't know. The solutions I have seen all assume that the lambda must be completely inlined within the...
17
10721
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 encoding scheme. But each time I feel confused when talking with another team -- codepage -- at the same time. I am more confused when I saw sometimes we need codepage parameter for wide character conversion, and sometimes we do not need for conversion. Here are two examples,
0
2335
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing Systems (MuCoCoS'08) Barcelona, Spain, March 4 - 7, 2008; in conjunction with CISIS'08. <http://www.par.univie.ac.at/~pllana/mucocos08> *********************************************************************** Context
1
9329
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" could not be bound. The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "PAR.PAR_Status" could not be bound. The multi-part identifier "Salary.New_Salary" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part...
0
10695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10777
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9530
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7090
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5759
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4573
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4172
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3198
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.