从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]
                );
        }
    }

}

然后,我使用此排序功能数量进行排序: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;
  }

}

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...