/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Function to compare the contents of two arrays

I would like to compare the contents of two sub arrays and use that
as a map to create new array out a second arrays values. I need to
find the highest value in the ‘m’ of the first sub array and assign it to
the highest value of ‘t’ in the second sub array. Either of the sub arrays
could have an odd number of values, so there might be four $k=>$v pairs
in one array and three in the other.

Input looks something like this

[code=php]
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[m] => 4.5 // assigned as second
[….] => [….]
)

[1] => Array
(
[m] => 3 // assigned as third
[….] => [….]
)

[2] => Array
(
[m] => 6 // assigned as first
[….] => [….]
)

)

[1] => Array
(
[0] => Array
(
[t] => 12 // assigned as third
[….] => [….]
)

[1] => Array
(
[t] => 19 // assigned as first
[….] => [….]
)

[2] => Array
(
[t] => 13 // assigned as second
[….] => [….]
)

)

)

[1] => Array
(
//next array
)

)
[/code]

This would be the map array

[code=php]

Array
(
[0] => Array
(
[0] => [2], // or $t[0][0][0] = $t[0][1][2]
[1] => [0], // or $t[0][0][1] = $t[0][1][0]
[2] => [1], // or $t[0][0][2] = $t[0][1][1]
)

[1] => Array
(
//next array
)

)
[/code]

The actual array to be rearranged would be this one

[code=php]
Array
(
[0] => Array
(
[0] => Array
(
[0] => ‘foo’,
[1] => ‘bar’,
[2] => ‘baz’,
)

[1] => Array
(
[0] => ‘red’,
[1] => ‘blue’,
[2] => ‘green’,
)
)

[1] => Array
(
//next array
)

)
[/code]

So the REAL final output would be this, and if possible to return the highest valued
key value pair in both decimal and word as a third array

[code=php]
Array
(
[0] => Array
(
[0] => Array
(
[‘foo’] => [‘green’],
[‘bar’] => [‘red’],
[‘baz’] => [blue],
)

[1] => Array
(
[0] => [‘bar:6’], //highest key-name, highest key value in [0][0]
[1] => [‘red:19’],//highest key-name, highest key value in [0][1]
)

[1] => Array
(
//next array
)
)
[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 29.2011 — Not the whole solution, but here's a piece you can use to sort each sub-array, based on the array key of interest. (Note: change usort() to u[b]a[/b]sort() if you need to keep the association of the first-dimension array indexes within the sub-arrays.)
[code=php]
<?php
/**
* Sort a 2-D array by specified array key
* @return array The sorted array
* @param array $array 2-D array to be sorted
* @param string $key 2nd dimension index/key to use for sorting
* @param bool $desc True (default) to sort descending, False for ascending
*/
function sortArray($array, $key, $desc=true)
{
$sort = $desc ? ' * -1' : ''; // invert comparison result if descending
usort(
$array,
create_function(
'$a,$b',
"return(strnatcasecmp($a['$key'], $b['$key'])$sort);"
)
);
return $array;
}

// TEST:
$data = array(
array('m' => 3, 'value' => 'three'),
array('m' => 1, 'value' => 'one'),
array('m' => 4, 'value' => 'four'),
array('m' => 2, 'value' => 'two')
);
$result = sortArray($data, 'm');
echo "<pre>Descending:".print_r($result,1)."</pre>";
$result = sortArray($data, 'm', false);
echo "<pre>Ascending:".print_r($result,1)."</pre>";
[/code]
Copy linkTweet thisAlerts:
@ehimeauthorAug 29.2011 — Thanks Nog.
×

Success!

Help @ehime spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...