Jump to content

Recommended Posts

Hi,

I have been going round in circles reading the docs on the different ways to use 2D arrays but getting stuck at the best method of achieving what I want.

 

This is what I want to do:

 

Display a list of user details in a random order in multiple colums.

I looked at retrieving the details from a csv file which was ok, but could not find a way to convert the info into a 2d array so I created a static array as follows:

 

$members=array(

                        "name1" => array ("Mr", "Tam", "Meechan","0123 5454","myemail@me.com","url1,"url2"),

                        "name2" => array ("Mr", "Joe", "Bloggs","0123 112233","hisemail@me.com","url1,"url2"),

                        "name3" => array ("Mr", "Bo", "Frank","0123 9900","noemail@me.com","url1,"url2"),

              );

 

 

my test code to print the details is:

 

count=count($members);

foreach ($members as $key => $val) {

    for($i=0;$i<$count;$i++) {

 

        print "<tr>";

        print "<td>$val[$i]</td>";

    }

    print "</tr>";

}

 

 

I know the output is not yet formatted e.g. I would want the first 3 values of the inner array to be displayed together on the same table row.

 

However, I want to display  as follows: $val[$i]  $val+1 [$i]

but don't know how to access the next element in the outer array.

 

Any ideas?

 

Also, if I want to shuffle the array keys name1, name2 etc, is it better to use integers for the keys rather than a string?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/183104-getting-next-element-1-in-a-2d-array/
Share on other sites

Thanks for the reply.

However, I don't think I explained myself properly.

$val[$i+1] would let me access the next element in the array for details for name1 whereas I want to access the same details for name2

 

e.g.

currently I have:

 

name1

mr

tam

meechan

01.......

 

name2

 

 

but want:

 

name1        name 2      name3

mr              mr            mr

tam            joe            bo

meechan      bloggs      frank

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.