# php - 从多个预先排序的数组创建排序的array

0 0

``````
Given :
A1 = [2, 4, 9, 16]
A2 = [-3, 4, 98, 116]
...
An = [1, 7, 17, 76, 512]

I would like :
At = [-3, 1, 2, 4, 4, 9, 16, 17, 76, 98, 116, 512]

``````

0 0

``````
A1 = [1, 2, 6]
A2 = [2, 4, 5]

A = []
I1 = 0
I2 = 0
-------------
A = [1]
I1 = 1
I2 = 0
-------------
A = [1, 2]
I1 = 2
I2 = 0
-------------
A = [1, 2, 2]
I1 = 2
I2 = 1
...

``````
0 0
``````
\$A1 = [2, 4, 9, 16];
\$A2 = [-3, 4, 98, 116];
\$An = [1, 7, 17, 76, 512];

//create arrays of ith elements
\$return = call_user_func_array('array_map', [null, \$A1, \$A2, \$An]);
//sort arrays
array_walk(\$return, 'sort');
//create new arrays
\$return = call_user_func_array('array_merge', \$return);
//remove null values lefted after first operation
\$return = array_filter(\$return, 'is_scalar');
var_dump(\$return);

``````

0 0

``````
function sortPreSortedArrays(\$arrays, \$comparisonFunction, \$order = 'asc')
{
\$sortedArray = array();

/* Sort */

while(sizeof(\$arrays)!== 0)
{
/* Find the greatest value */

\$max = true;
\$keyMax = -1;

foreach(\$arrays as \$key => \$array)
{
if(\$max === true || \$comparisonFunction(end(\$array), \$max))
{
\$max = end(\$array);
\$keyMax = \$key;
}
}

/* Take the greatest value */

array_push(\$sortedArray, array_pop(\$arrays[\$keyMax]));
if(sizeof(\$arrays[\$keyMax]) === 0) unset(\$arrays[\$keyMax]);
}

/* Return */

if(\$order === 'asc')
return array_reverse(\$sortedArray);
else
return \$sortedArray;

``````

``````
function compareLogArrayDate(\$log1, \$log2)
{
\$t1 = \$log1['date'];
\$t2 = \$log2['date'];

return (\$t1> \$t2)? true : false;
}

``````