Re: Merge two arrays into associative arrays?
| From: | Lars B. Jensen, LJ Webnologics | Date: | Tue, 22 May 2001 08:32:00 +0000 |
| Subject: | Re: Merge two arrays into associative arrays? | ||
| References: | 1 2 3 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-9519@lists.php.net to get a copy of this message | ||
With the risk of being wrong, I will try again...
I just did a test with your data,
<?php
$a = array("id","svrname","ipadd","domainname");
$b = array("ID","Server Name","IP Address","Domain Name");
$c = array();
$arraysize = sizeof($a);
for ($i=0;$i<$arraysize;$i++) {
$c[$a[$i]] = $b[$i];
}
print ("<PRE>");
print_r($c);
print ("</PRE>");
?>
which should produce an associative array with keys from $a and values from
$b. The result of this "operation" is
Array
(
[id] => ID
[svrname] => Server Name
[ipadd] => IP Address
[domainname] => Domain Name
)
. As I read your question, this is exactly what you look for, right ?
/ Lars
www.ljweb.com
----- Original Message -----
From: "Shahmat Dahlan" <shahmatd@sains.com.my>
To: "Lars B. Jensen LJ Webnologics" <ljweb@ljweb.com>
Cc: "Jonathan Hilgeman" <JHilgeman@ecx.com>; "PHP-DB (E-mail)"
<php-db@lists.php.net>
Sent: Tuesday, May 22, 2001 8:52 AM
Subject: Re: [PHP-DB] Merge two arrays into associative arrays?
> This should have worked but my data are not single characters as I had
showed
> earlier. Maybe the examples below are clearer.
>
> $a=array("id","svrname","ipadd","domainname",);
> $b=array("ID","Server Name","IP Address","Domain
> Name");
>
> Thanks
>
>
> "Lars B. Jensen, LJ Webnologics" wrote:
>
> > Have followed this group for a while, it is my turn to try send an
response.
> >
> > Would this angle with a for loop not be better than a foreach angle ?
> > <?php
> > $a = array(1,2,3,4,5);
> > $b = array("a","b","c","d","e");
> > $c = array();
> >
> > $arraysize = sizeof($a);
> > for ($i=0;$i<$arraysize;$i++) {
> > $c[$a[$i]] = $b[$i];
> > }
> > ?>
> >
> > / Lars
> > www.ljweb.com
> >
> > ----- Original Message -----
> > From: "Jonathan Hilgeman" <JHilgeman@ecx.com>
> > To: "'Shahmat Dahlan'" <shahmatd@sains.com.my>
> > Cc: "PHP-DB (E-mail)" <php-db@lists.php.net>
> > Sent: Monday, May 21, 2001 6:10 PM
> > Subject: RE: [PHP-DB] Merge two arrays into associative arrays?
> >
> > > try:
> > >
> > > $a=array(1,2,3,4,5);
> > > $b=array("a","b","c","d","e");
> > > $c = array();
> > >
> > > foreach($a as $a_key => $a_value)
> > > {
> > > $c[$a_value] = $b[$a_key];
> > > }
> > >
> > > that should give you what you want.
> > >
> > > Jonathan
> > >
> > > -----Original Message-----
> > > From: Shahmat Dahlan [mailto:shahmatd@sains.com.my]
> > > Sent: Monday, May 21, 2001 12:23 AM
> > > To: PHP-DB
> > > Subject: [PHP-DB] Merge two arrays into associative arrays?
> > >
> > >
> > > Is there anywhere possible for me to merge two arrays into associative
> > > arrays?
> > >
> > > e.g.
> > > $a=array(1,2,3,4,5);
> > > $b=array("a","b","c","d","e")
> > >
> > > into
> > >
> > > $c=array(1 => "a", 2 => "b", etc... )
> > >
> > > Thanks in advance..
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net
> For additional commands, e-mail: php-db-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>
>