Forum Moderators: coopster
customers[]
cust_id => 0001
cust_name => Joe
locations[]
cust_id => 001
cust_location => Montana
How can I efficiently merge these two together to create just one array:
customers2[]
cust_id => 001
cust_name => Joe
cust_location => Montana
The only way I can think of doing this is to loop through one and do an array_search on the other. If found just create a new array with all three fields.
This seems a bit inefficient as if I have 1000 customer id's then a maximum of a million iterations could be performed.
Is there already a function to create one array from two?
<added>
I thought of that at first too, Little_G, but then I noted the comment:
I have 1000 customer id's
... and noticed that array_merge [php.net] would merely overwrite the index with the last value realized.
</added>
"merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one."
it seems array_merge_recursive would do similar
e.g.
customers[]
0001, Joe
0002, Sally
locations[]
0001, Montana
0002, Ohio
merging would produce:
0001, Joe
0002, Sally
0001, Montana
0002, Ohio
But what I'm trying to create is a new array:
0001, Joe, Montana
0002, Sally, Ohio
$customers = array(
array(
'cust_id' => '0001',
'cust_name' => 'Joe'
),
array(
'cust_id' => '0002',
'cust_name' => 'Sally'
)
);
print_r($customers);
Array
(
[0] => Array
(
[cust_id] => 0001
[cust_name] => Joe
)
[1] => Array
(
[cust_id] => 0002
[cust_name] => Sally
)
)
$customers = array(
array(
'cust_id' => '0001',
'cust_name' => 'Joe'
),
array(
'cust_id' => '0002',
'cust_name' => 'Sally'
)
);
$locations = array(
array(
'cust_id' => '0001',
'cust_location' => 'Montana'
),
array(
'cust_id' => '0002',
'cust_location' => 'Ohio'
)
);
The php net manuals seem to imply that array_intersect should do the trick but I haven't worked out how to do that yet.
What I have had to do is to store the arrays in two mysql tables and just lookup one against the other to create a new merged table.
The data was to end up in a table anyway. It's just that I now have to create three tables where one was desired. I'm a stickler for efficiency!
If there are 1000 in the customers array there will be a corresponding 1000 in the location array.
If Joe is # 0001 in customers, he is 0001 in location etc.
Can't obtain the information any other way - it's obtained from soap. Two seperate arrays are collated and what I need to do is to make just one.
$customers = array(
array('cust_id' => 0001,'cust_name' => 'Joe'),
array('cust_id' => 0002,'cust_name' => 'Bob'),
array('cust_id' => 0003,'cust_name' => 'Sally'),
array('cust_id' => 0004,'cust_name' => 'Bill'),
array('cust_id' => 0005,'cust_name' => 'Martha')
);
echo '<p>customers<br><pre>';
print_r($customers);
echo '</pre>';
$locations = array(
array('cust_id' => 001,'cust_location' => 'Montana'),
array('cust_id' => 002,'cust_location' => 'Alabama'),
array('cust_id' => 003,'cust_location' => 'Washington'),
array('cust_id' => 004,'cust_location' => 'Maine'),
array('cust_id' => 005,'cust_location' => 'Florida')
);
echo '<p>locations<br><pre>';
print_r($locations);
echo '</pre>';
$counter = 0;
$customers2 = array();
while (isset($customers[$counter])) {
$customers2[] = array($customers[$counter]['cust_id'],$customers[$counter]['cust_name'],$locations[$counter]['cust_location']);
echo '',$counter;
$counter++;
}
echo '<p>cust2<br><pre>';
print_r($customers2);
echo '</pre>';