Это немного кода, поэтому я разделил его на куски.
Это мое воссоздание ваших двух массивов.Последняя строка помещает их в одну.
$items = array(
array(
1 => 488,
5 => '23-1000',
2 => 3,
3 => 'PRODUCT NAME',
4 => 2.50
),
array(
1 => 423,
5 => '24-2300',
2 => 1,
3 => 'PRODUCT NAME',
4 => 3.50
),
array(
1 => 506,
5 => '23-2800',
2 => 1,
3 => 'PRODUCT NAME',
4 => 2.50
),
array(
1 => 629,
5 => '01-3600',
2 => 1,
3 => 'PRODUCT NAME',
4 => 7.50
)
);
$array_2 = array(
array(
1 => 629,
5 => '01-3600',
2 => 1,
3 => 'PRODUCT NAME',
4 => 7.50
)
);
// Put the two arrays together (master array)
$new_array = array_merge($items, $array_2);
Далее мы создадим два массива, которые будут использовать SKU #, а другой для идентификаторов.
// What the items will be sorted by
$skus = array();
$ids = array();
Более сложная часть
// Loop through the combined items
foreach( $new_array as $item ) {
/**
* Check if the item's SKU number
* has been added to the $skus array.
*
* If it has then add the old qty with the current item's
*
* Else, the item hasn't been added yet,
* then add the entire item to the list
*/
if( isset($skus[$item[5]]) ) {
// If it exists, then add the new qty
$skus[$item[5]][2] += $item[2];
}else {
// If it doesn't exist, then append it to the array
$skus[$item[5]] = $item;
}
// Do the same thing as above
// except for the id numbers
if( isset($ids[$item[1]]) ) {
// If it exists, then add the new qty
$ids[$item[1]][2] += $item[2];
}else {
// If it doesn't exist, then append it to the array
$ids[$item[1]] = $item;
}
}
Убедитесь, что все так, как вы хотите
<code>echo '<h2>SKU Numbers</h2>';
echo '<pre>';
print_r($skus);
echo '
';echo '
ID номера
';echo '
';
print_r($ids);
echo '
';
Надеюсь, это поможет.
EDIT
Чтобы этот код работал во время реального цикла, вы можете попробовать что-то вроде этого.Хотя я не совсем уверен, будет ли это работать из коробки , это должно быть хорошее место для начала.
// What the items will be sorted by
$skus = array();
$ids = array();
foreach( $result as $row ) {
$row = unserialize($row);
/**
* MODIFIED
*
* Check if the $row's SKU number
* has been added to the $skus array.
*
* If it has then add the old qty with the current $row's qty
*
* Else, the $row hasn't been added yet,
* then add the entire $row to the list
*/
if( isset($skus[$row[5]]) ) {
// If it exists, then add the new qty
$skus[$row[5]][2] += $row[2];
}else {
// If it doesn't exist, then append it to the array
$skus[$row[5]] = $row;
}
// Do the same thing as above
// except for the id numbers
if( isset($ids[$row[1]]) ) {
// If it exists, then add the new qty
$ids[$row[1]][2] += $row[2];
}else {
// If it doesn't exist, then append it to the array
$ids[$row[1]] = $row;
}
}