首页 / PHP / 从MySQL反序列化为多维PHP数组
从MySQL反序列化为多维PHP数组
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了从MySQL反序列化为多维PHP数组,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3915字,纯文字阅读大概需要6分钟。
内容图文
![从MySQL反序列化为多维PHP数组](/upload/InfoBanner/zyjiaocheng/903/5f21374e965a4d78aa0b64754705130f.jpg)
我有一个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]
);
}
}
}
然后,我使用此排序功能对数量进行排序:http://www.php.net/manual/en/function.sort.php#99419
解决方法:
这是一些代码,所以我把它分成了几块.
这是我对你的两个阵列的再创造.最后一行将它们合二为一.
$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>';
希望这可以帮助.
编辑
要使此代码在实际循环期间起作用,您可以尝试这样的方法.虽然我不完全确定这是否可以开箱即用,但它应该是一个好的起点.
// 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;
}
}
内容总结
以上是互联网集市为您收集整理的从MySQL反序列化为多维PHP数组全部内容,希望文章能够帮你解决从MySQL反序列化为多维PHP数组所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。