Jump to content

Fun with Array's...


frio80

Recommended Posts

I have an array:

[letter] = An Array:

    [96-19205] = '1'

    [97-19205] = '2'

[envel] = An Array:

    [96-19205] = '3'

    [97-19205] = '4'

 

I would like the result to read:

[papers] (the name isn't very important)

    [96-19205] = '4'

    [97-19205] = '6'

 

Seems simple enough but just trying to figure out a more efficient method (using built-in array functions) rather than looping and looking for matches manually?  Any thoughts?  Thanks.

Link to comment
Share on other sites

Sorry for that lousy explanation.  Let me try again.

 

I would like to merge the arrays and add the values of the duplicates together.  The array structure is:

[<name>]

    [item#] = <quantity>

    ...

 

So I have these two arrays named letter and evel

[letter] = An Array:

    [96-19205] = '1'

    [97-19205] = '2'

    [98-12222] = '9'

    ...

[envel] = An Array:

    [96-19205] = '3'

    [97-19205] = '4'

    [92-11231] = '3'

 

I would like the resulting merged array to read:

[papers]

    [96-19205] = '4'

    [97-19205] = '6'

    [92-11231] = '3'

    [98-12222] = '9'

 

Thanks!

Link to comment
Share on other sites

<pre>
<?php

function sum_array_keys () {
	$result = array();
	$arrays = func_get_args();
	foreach ($arrays as $array) {
		foreach ($array as $k => $v) {
			$result[$k] += $v;
		}
	}
	return $result;
}

$letter = array(
	'96-19205' => 1,
	'97-19205' => 2,
	'98-12222' => 9,
);
$envel = array(
	'96-19205' => 3,
	'97-19205' => 4,
	'92-11231' => 3,
);

$result = sum_array_keys(&$letter, &$envel);
print_r($result);

?>
</pre>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.