mysql 反序列化函数_从MySQL反序列化为多维PHP数组

我有一个PHP / MySQL电子商务网站,它将订单详细信息与客户地址信息一起放入序列化数组中.

我希望能够提取订单商品字段,对其进行反序列化,然后将订单商品组合成一个可以操作的所有订购商品的主数组,以便计算特定产品的订单数量.

当我print_r未序列化的行时,数组看起来像这样.下面是两个订单数组,一个有3个产品,第二个只有一个.

数组值为ID#,SKU#,Quantity,Product Name,Price.

我希望能够将所有订单合并为一个数组,然后汇总每个唯一ID或SKU编号的总数量.

我意识到,如果数据在MySQL中是干净的,那么这种类型的东西很简单,但这就是生命.任何关于如何操纵这些阵列的想法都将得到真正的体会.

在这种情况下,最终需要4个阵列,其中629 / 01-3600组合在一起,数量值现在为2

非常感谢.

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 '

SKU Numbers

';

echo '

';

print_r($skus);

echo '

';

echo '

ID Numbers

';

echo '

';

print_r($ids);

echo '

';

希望这可以帮助.

编辑

要使此代码在实际循环期间起作用,您可以尝试这样的方法.虽然我不完全确定这是否可以开箱即用,但它应该是一个好的起点.

// 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;

}

}

标签:php,mysql,serialization,multidimensional-array

来源: https://codeday.me/bug/20190705/1383175.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值