Jump to content

[SOLVED] Can an array have more than two 'fields'?


eco

Recommended Posts

It's my fist post so I'll stat by "Hi"!  I'm new to php and programing in general, hope you'll bare with me ;)

 

Here is my question:

 

Can an array store more than one value per 'record'?

 

What I mean is can something like the following be done?

 

[...]
$myArray = array(
   "John", "Doe", "1960";
   "Frank", "Doe", "1980";
   "Paul", "Allan", "1960";
);
[...]
return $myArray;

 

The reason for this is I have a Class that needs to return an array that should contain the first name, last name and birth year of several people.

 

Can this be done and if so, where is the documentation?

 

What would you advise I do when needing to return the above to another Class?

 

Thanks for your help.

-eco

Link to comment
Share on other sites

OK, but then how do I know when to go to the next 'record'?

 

I guess the array will look something like this:

 

$myArray[0]="John"
$myArray[1]="Doe"
$myArray[2]="1960"
$myArray[3]="Frank"
$myArray[4]="Doe"
$myArray[5]="1980"
...

 

So how do I do a foreach of the array so that I can assign myArray[0,3,6] to the varaible 'first name'?

foreach($myArray as $ar) {
   $fname=$ar[0];
   $lname=$ar[1];
   $birth=$ar[2];
   print "$fname, $lname, $birth\n";
}

 

That won't work.  How would you do it?

Link to comment
Share on other sites

An array of arrays.

 

eg:

 

$myArray = array(array('John', 'Doe', '1960'), array('Jane', 'Doe', '1970'));

echo $myArray[0][0]; // John

 

If you're specifically using only 3 you could do a for() like..

 

for($i = 0;$i < count($array);$i+=3)
{
     $firstName = $i;
     $lastName = ++$i;
     $year = $i+2;
}

 

Where the array would look something like yours..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.