Forum Moderators: coopster

Message Too Old, No Replies

Array help

Going round and round in circles!

         

Karma

3:44 pm on Sep 7, 2009 (gmt 0)

10+ Year Member



Hi,

Question for those PHP gurus; I'm trying to build an uneven array similar to the following:

0
(
adam, 20
bob, 24
)

1
{
anne, 28
beth, 29
claire, 22
dawn, 31
eve, 19
}

etc etc

Once built, I then want to access the array by index (E.g. 1) and output all names and ages.

I've been going round in circles with this, uneven multi-dimensional arrays hurt my brain but I get the feeling I'm over-complicating things.

What's the easiest way of doing this?

eelixduppy

4:19 pm on Sep 7, 2009 (gmt 0)



I'm not exactly sure how you have the array above formatted. Could you please give a better description, or post the results of print_r so that I can see.

Essentially you just want to use the index. So...


echo $array[0][0][0]; // should print 'adam' (if understood correctly)

This usually isn't very useful, so you are going to want to iterate through the array to get access to all of the elements of the array:


foreach($array AS $data)
{
foreach($data AS $datum)
{
echo $datum[0]; // should print every name
}
}

Hope that helps? Not sure, but play around with it and see what happens.

Karma

2:11 pm on Sep 8, 2009 (gmt 0)

10+ Year Member



I've not managed to get this working so I'll have to try to provide a better example:

0¦0¦adam¦20
1¦0¦bob¦24
2¦1¦anne¦28
3¦1¦beth¦29
4¦1¦claire¦22
5¦1¦dawn¦31
6¦1¦eve¦19

I guess this is what I'm trying to build. I need to access the array via the 2nd column index and write out each matching item, so:

Accessing using 0 would give me:

Name: adam
Age: 20
Name: bob
Age: 24

And accessing using 1 would give me:

Name: anne
Age: 28
Name: beth
Age: 29
Name: claire
Age: 22
Name: dawn
Age: 31
Name: eve
Age: 19

Make sense? :/

Tommybs

4:25 pm on Sep 8, 2009 (gmt 0)

10+ Year Member



Hi,

So in your example here you basically have a male and female array which hold arrays which are basically 'people'.

Now to be honest with you I'm a big fan of classes and oop but I don't know if you are, otherwise I would suggest using them.

In your example above would it not be easier to add an extra field to the person array that contains the sex?

e.g (please be aware this is a very quick mock up)


$person = array("ted",20,0);
$person2 = array("bill",30,0);
$person3 = array("steph", 26,1);
$person4 = array("helen", 31,1);

array_push($arr,$person);
array_push($arr,$person2);
array_push($arr,$person3);
array_push($arr,$person4);


You could then access the array in a function and pass in what the index needs to equal (in this case the sex)


function show_sex($sex,$arr){
//0 == men
//1 == women
foreach($arr as $key=>$val){
if($val[2] == $sex){
echo $val[0]." ".$val[1]." ". $val[2]."<br />";
}
}
}

echo "<h1>men</h1>";
show_sex(0,$arr);//show men
echo "<h1>women</h1>";
show_sex(1,$arr);//women

putting all the above code together


<?php
$arr = array();

$person = array("ted",20,0);
$person2 = array("bill",30,0);
$person3 = array("steph", 26,1);
$person4 = array("helen", 31,1);

array_push($arr,$person);
array_push($arr,$person2);
array_push($arr,$person3);
array_push($arr,$person4);

function show_sex($sex,$arr){
//0 == men
//1 == women
foreach($arr as $key=>$val){
if($val[2] == $sex){
echo $val[0]." ".$val[1]." ". $val[2]."<br />";
}
}
}

echo "<h1>men</h1>";
show_sex(0,$arr);//show men
echo "<h1>women</h1>";
show_sex(1,$arr);//women

?>


should give you the following results if you run it in a browser


men
ted 20 0
bill 30 0
women
steph 26 1
helen 31 1

optik

10:52 am on Sep 9, 2009 (gmt 0)

10+ Year Member



Something like this should get you started

$emp_det = array (array (sex=>"male", name=> "adam", age=> "20"),
array (sex=>"male", name=> "bob", age=> "24"),
array (sex=>"female", name=> "anne", age=> "32"),
array (sex=>"female", name=> "beth", age=> "36"),
);


foreach ($emp_det as $tempone) {
foreach ($tempone as $key=>$temptwo) {
echo "$key: $temptwo", "\n";
}
echo "\n";
}