将MySQL取消序列化为多维PHP数组
我有一个PHP/MySQL电子商务站点,它将订单详细信息与客户地址信息一起放入一个序列化数组中 我希望能够拉出order items字段,将其取消序列化,然后将order items合并到一个主数组中,该数组包含所有可操作的order items,以便计算特定产品的订单数量 当我打印未序列化的行时,数组是这样的。下面是两个订单阵列,一个包含3个产品,另一个仅包含一个产品 数组值为ID、SKU、数量、产品名称、价格 我希望能够将所有订单合并到一个数组中,然后对每个唯一ID或SKU编号的总数量求和 我意识到,如果MySQL中的数据是干净的,那么这种事情就非常简单,但这就是生活。任何关于如何操作这些阵列的想法都将不胜感激 在本例中,我们希望最终得到4个数组,将629/01-3600个数组组合在一起,使数量值现在为2 非常感谢将MySQL取消序列化为多维PHP数组,php,mysql,serialization,multidimensional-array,Php,Mysql,Serialization,Multidimensional Array,我有一个PHP/MySQL电子商务站点,它将订单详细信息与客户地址信息一起放入一个序列化数组中 我希望能够拉出order items字段,将其取消序列化,然后将order items合并到一个主数组中,该数组包含所有可操作的order items,以便计算特定产品的订单数量 当我打印未序列化的行时,数组是这样的。下面是两个订单阵列,一个包含3个产品,另一个仅包含一个产品 数组值为ID、SKU、数量、产品名称、价格 我希望能够将所有订单合并到一个数组中,然后对每个唯一ID或SKU编号的总数量求和
Array
(
[0] => Array
(
[1] => 488
[5] => 23-1000
[2] => 3
[3] => PRODUCT NAME
[4] => 2.50
)
[1] => Array
(
[1] => 423
[5] => 24-2300
[2] => 1
[3] => PRODUCT NAME
[4] => 3.50
)
[2] => Array
(
[1] => 506
[5] => 23-2800
[2] => 1
[3] => PRODUCT NAME
[4] => 2.50
)
[3] => Array
(
[1] => 629
[5] => 01-3600
[2] => 1
[3] => PRODUCT NAME
[4] => 7.50
)
)
Array
(
[0] => Array
(
[1] => 629
[5] => 01-3600
[2] => 1
[3] => PRODUCT NAME
[4] => 7.50
)
)
编辑:
我想补充一下,我最终做了什么,我在寻找什么
foreach($query->result as $row)
{
$items[] = unserialize( $row['FIELDNAME'] );
}
foreach($items as $item)
{
foreach($item as $order)
{
if( isset($output_array[$order[1]]) )
{
$output_array[$order[1]]['quantity'] += $order[2];
}
else
{
$output_array[$order[1]] = array (
'name' => $order[3],
'quantity' => $order[2],
'sku' => $order[5]
);
}
}
}
然后我使用这个排序函数对数量进行排序:这是一段代码,所以我将它分成了几块 这是我对你们两个阵列的再创造。最后一行把它们放进一行
$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,另一个用于ID
// 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;
}
}
确保一切都是你想要的
echo '<h2>SKU Numbers</h2>';
echo '<pre>';
print_r($skus);
echo '</pre>';
echo '<h2>ID Numbers</h2>';
echo '<pre>';
print_r($ids);
echo '</pre>';
事实上,虽然我知道这最终会奏效。我正在运行foreach($result as$row)循环,以便提取序列化字段并取消序列化。为了合并100行,我需要在每次循环时创建一个新变量,对吗?然后使用array\u merge合并所有一百个项目?我在代码中添加了一个更改(在底部),该更改给出了如何将项目放入$SKU和$ids变量的示例。
// 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;
}
}