Page 1 of 1
two arrays, one output, side-by-side?
Posted: Wed Mar 05, 2003 9:52 am
by moniarde
I have 2 arrays. I wish to print them to the screen for the user. The first contains the titles for values. The second array contains the values selected from a db. If there is a way of making the values of the first array change into the keys of the second array, then feel free to stop reading right now, and post a reply. For this I would be ever grateful.
For those that wish to continue, here is what I am looking for. I wish to output the values of both arrays, so that I have the following:
Title1 : Value1
Title2 : Value2
etc...
Now let me throw this into the mix. There is more than one row from the db that I wish to do this to. So I have to be able to refer to the sql to repeat the lines. I already can do this to an extent , using "while($row=mysql_fetch_row($arr)", but I can't line the 2 values up together so that they are side-by-side. Here's what I want:
Row 1
Title1 : Value1
Title2 : Value2
etc...
Row 2
Title1 : Value1
Title2 : Value2
etc...
etc...
Can anybody help? Thankyou in advance.
Posted: Wed Mar 05, 2003 10:50 am
by puckeye
Well yes there's a way to use the value of one as the key of the other...
Code: Select all
<?php
foreach($FirstArray AS $value) // goes through the whole FirstArray and assigns the value to $value
{
print $value." : ".$SecondArray[$value]."<BR>";
}
?>
So here your FirstArray would be the $row in your exemple.
I guess you could do this like that:
Code: Select all
<?php
$rowcount = 1;
while($row = mysql_fetch_row($arr))
{
print "Row $rowcount<BR><BR>";
foreach($row AS $value)
{
print $value." : ".$SecondArray[$value]."<BR>";
}
$rowcount ++;
}
?>
Posted: Thu Mar 06, 2003 9:05 am
by moniarde
Thanks, puckeye. Close, but no cigar. The values of the first array are not equal to the keys of the second, so at the moment the output is only a change in row number, which is good, and the output of the values of the first array. Any suggestions?
Posted: Thu Mar 06, 2003 6:59 pm
by McGruff
I can't quite see how title/value pairs have become detached in the first place.
Just to clarify your problem a bit: are the db column names from which you get the values the same as the titles? If not, can you set it up like that?
Then you can print title/value pairs with something like:
// $result = mysql_fetch_array($query)
foreach($result as $key=>$value) {
echo $key;
echo ": " . $value . "<br>";
}
That sounds far too obvious though - post again to explain where the title array is coming from.
Posted: Thu Mar 06, 2003 8:26 pm
by moniarde
No, the db column names are not the same as the values in $titlesarr (the first array). I don't want the db column names to be viewed (I made them so I could understand them, but the public wouldn't should someone try to hack in), which is why I have made this array with the titles.
One of my original questions was asking about the possibilty of changing the $key names of the second array (which contains the db column names) en masse to the values I created in the first array ($titlesarr - the values are the titles I want).
Basically here is what I want:
$value of titlearr[key1] : $value of dbarr[key1]
$value of titlearr[key2] : $value of dbarr[key2]
etc...
Here is what I have, with puckeye's code included:
Code: Select all
<?php
$titlesarr[] = "First Name";
$titlesarr[] = "Date of Birth";
$titlesarr[] = "Gender";
$rowcount = 1;
while($row = mysql_fetch_row($dbarr)) {
print "<BR>Row $rowcount<BR><BR>";
foreach($titlesarr AS $value) {
print $value." : ".$dbarr[$value]."<BR>";
}
$rowcount ++;
}
?>
but that doesn't work.
I hope that's clear

. If not, just ask and I'll see wat I can do. Thanx all.
Posted: Thu Mar 06, 2003 10:21 pm
by McGruff
I'm not sure if you really need to disguise column names like that.
However, to switch keynames, try this:
Code: Select all
$search = array();
foreach($result as $key=>$value) {
$search[] = $key; // an array of $result keys
}
$temp_array = array_flip($result);
$output_array = str_replace($search, $titles_array, $temp_array);
$output_array = array_flip($output_array);
I didn't test it but you should see what I'm getting at: flip the result array, replace keynames (now values) and then flip it back again.
Won't work if you have duplicate values in the original $result row - see manual.