Forum Moderators: coopster

Message Too Old, No Replies

Create one array from two

         

Frank_Rizzo

8:09 pm on Feb 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have two arrays

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?

Little_G

8:19 pm on Feb 13, 2006 (gmt 0)

10+ Year Member



Hi,

There is indeed:
http://uk2.php.net/manual/en/function.array-merge.php [uk2.php.net]

Andrew

coopster

8:22 pm on Feb 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You have two different values there under the cust_id index in this example. If there were always going to be a one-to-one relationship in both arrays perhaps array_merge_recursive() [php.net] would work for you?

<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>

Frank_Rizzo

8:37 pm on Feb 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I looked at array_merge() but was put off by this:

"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

coopster

9:11 pm on Feb 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It's hard to tell how your array is structured right now. You are correct, the recursive would only work if you had the customers in multiple arrays. I am assuming at this point the $customers array (as well as $locations) array is built something like this?
$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
)
)

Frank_Rizzo

11:01 pm on Feb 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's right.

$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.

Frank_Rizzo

11:41 am on Feb 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can't find any solution to this from using php functions.

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!

jatar_k

4:59 pm on Feb 14, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



is there a value for each row in each array?
are they the same size?

where are the arrays coming from? is there no way to select them together?

Frank_Rizzo

5:11 pm on Feb 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Every row and column has a value.

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.

jatar_k

6:30 pm on Feb 14, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



something like this then?

$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>';

Frank_Rizzo

4:31 pm on Feb 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Many thanks for that. I'll give it a try.

jatar_k

8:40 pm on Feb 16, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



coopster made me change this line

while (isset($customers[$counter])) {

I originally had

while (is_array($customers[$counter])) {

which throws a notice, he cared more than I did ;)

coopster

10:45 pm on Feb 16, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Oh shush.