...

View Full Version : Foreach on multidimensional array



DR.Wong
08-23-2009, 07:36 PM
Hi everyone,

Probably a simple question, however good old Google couldn't help.

I have this array :



$array['name'][1]='John';
$array['birthday'][1]='25 June';

$array['name'][2]='Mary';
$array['birthday'][2]='13 May';


I am trying to write a foreach script that will display :

name : John
birthday : 25 June
name : Mary
birthday : 13 May

Simple. In other words, I need the key(?) echoed, in this case "name" or "birthday" and then the value, and for each numerical index [1,2,3,4,etc]

I've got this code so far, but it shows the numerical index and not the string index :


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

This code currently, and incorrectly (for my needs) outputs :

0 : John
1 : Mary
0 : 25 June
1 : 13 May

Could anyone help me out?

Thanks a lot!

Phil Jackson
08-23-2009, 07:47 PM
foreach($array['name'] as $key => $name)
{
echo $name.$array['birthday'][$key];
}
?>

DR.Wong
08-23-2009, 07:51 PM
Thanks for the reply phil!

But lets assume as well that we won't know exactly what parameters there will be in the array.

For example, there could be name, birthday and address.

Phil Jackson
08-23-2009, 07:54 PM
<?php
$array['name'][1]='John';
$array['birthday'][1]='25 June';
$array['address'][1]='123 made up avenue';
$array['name'][2]='Mary';
$array['birthday'][2]='13 May';
$array['address'][2]='1 road thing';

print_r($array);
foreach($array['name'] as $key => $name)
{
echo $name.$array['birthday'][$key].$array['address'][$key];
}
?>


no worries, im here to serve (since you did have ago yourself).

Phil Jackson
08-23-2009, 07:58 PM
or



<?php
$name = array('phil', 'luke', 'john');
$age = array('12', '13', '14');
$address = array('123 thing close', '124 thing close', '125 thing close');

foreach($name as $key => $name)
{
echo $name." ".$age[$key]." ".$address[$key]."<br />\n";
}
?>

DR.Wong
08-23-2009, 08:02 PM
Thanks again for the reply.

The new code also assumes that we know exactly what parameters there are going to be in the array.

Perhaps I did not provide enough info.

I have a class that reads information out of an XML file.

The class reads the info from the XML file into a multi dimensional array.

The catch is that not all of the entries in the XML file will have the same data.

For example, the XML file contents may appear as :



<contact>
<name>John</name>
<birthday>25 June</birthday>
</contact>

<contact>
<name>Mary</name>
<birthday>14 May</birthday>
</contact>

<contact>
<name>Andrew</name>
<birthday>3 August</birthday>
<address>1 Made up avenue</address>
</contact>

<contact>
<name>George</name>
<birthday>3 March</birthday>
<address>1 Fake Street</address>
<telephone>555-5555</telephone>
</contact>


You see, the fields will be random. There is no way of knowing what is going to end up in the array.

Phil Jackson
08-23-2009, 08:04 PM
yeah but do you know what the max amount of fields would be and what POTENTIALLY could be there??

Phil Jackson
08-23-2009, 08:07 PM
surely with your script that sorts data into array you can, if no data exists, store as null?
then if set as null dont display info for that field?

EDIT:
after further reading, you XML would have to have a blank field to do that. i.e. <address></address> to store as null.

DR.Wong
08-23-2009, 08:15 PM
I guess we could make it a max of say 10 fields (for now).

But I have no idea what the fields could potentially be.

This is for an estate agency and they correspond with others and store an xml database of their associates.

Each associate has some sort of registration number that has a different name, I myself don't understand it, but the way that the database exists at the moment is something like :

<contact>
<ctv_no>34434324234</ctv_no>
</contact>

Where ctv_no sits, it could be ANYTHING else.

Also, a lot of them have multiple numbers.

So the script has to be expandable.

UPDATE :

I'm just writing the web-display side of this.. Some other program generates all of this.

Sucks I know :p

Phil Jackson
08-23-2009, 08:21 PM
also take a look at this i just wrote for ya!



$data = "
<contact>
<name>John</name>
<birthday>25 June</birthday>
</contact>

<contact>
<name>Mary</name>
<birthday>14 May</birthday>
</contact>

<contact>
<name>Andrew</name>
<birthday>3 August</birthday>
<address>1 Made up avenue</address>
</contact>

<contact>
<name>George</name>
<birthday>3 March</birthday>
<address>1 Fake Street</address>
<telephone>555-5555</telephone>
</contact>
";
if(preg_match_all("#<contact>((?!<\/contact>).)*#is", $data, $results))
{
foreach($results[0] as $foo)
{
echo $foo."<br /><br />";
}
}
?>

Phil Jackson
08-23-2009, 08:23 PM
output being:


John 25 June

Mary 14 May

Andrew 3 August
1 Made up avenue


George 3 March
1 Fake Street
555-5555


you could run many tests on $foo to get what data you need.

Phil Jackson
08-23-2009, 08:24 PM
thank me as much as you want :p

DR.Wong
08-23-2009, 08:42 PM
Haha... and he knows he's the man already!!!!!!!

THANK YOU SO MUCH!

Phil Jackson
08-23-2009, 08:48 PM
no probs my man!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum