数组中的重复ID或其他reord

时间:2017-01-29 13:32:02

标签: php mysql json multidimensional-array associative-array

我正在为移动应用程序编写PHP的Web服务,并且必须将一些数据放入get查询中以获取订单详细信息服务。 当我编码成json时,我的查询得到以下响应。

{
  "status": 101,
  "message": "Success",
  "result": [
    {
      "order_id": "38",
      "order_product_id": "49",
      "product_id": "377",
      "product_name": "Pineapple Cake 2 Kg Eggless + Free Surprise Gift",
      "option_name": "Delivery Time",
      "option_value": "4:00am to 6:00am",
      "model": "Cake B22",
      "quantity": "1"

    },
    {
      "order_id": "38",
      "order_product_id": "49",
      "product_id": "377",
      "product_name": "Pineapple Cake 2 Kg Eggless + Free Surprise Gift",
      "option_name": "Message For Cake",
      "option_value": "Second order first product",
      "model": "Cake B22",
      "quantity": "1"
    },
    {
      "order_id": "38",
      "order_product_id": "50",
      "product_id": "339",
      "product_name": "Pineapple Cake 1 Kg + Free Surprise Gift",
      "option_name": "Make it Eggless",
      "option_value": "No",
      "model": "Cake A7",
      "quantity": "1"
    },
    {
      "order_id": "38",
      "order_product_id": "50",
      "product_id": "339",
      "product_name": "Pineapple Cake 1 Kg + Free Surprise Gift",
      "option_name": "Delivery Time",
      "option_value": "4:00am to 6:00am",
      "model": "Cake A7",
      "quantity": "1"
    },
    {
      "order_id": "38",
      "order_product_id": "50",
      "product_id": "339",
      "product_name": "Pineapple Cake 1 Kg + Free Surprise Gift",
      "option_name": "Message For Cake",
      "option_value": "second order 2nd cake",
      "model": "Cake A7",
      "quantity": "1"
    }
  ]
}

我正在通过sql查询

$strSQL = "SEletc * ........";

    $objQuery = mysql_query($strSQL);
    $intNumField = mysql_num_fields($objQuery);
    $resultArray = array();
     $data_ok = false;
    while($obResult = mysql_fetch_array($objQuery))
    {
        $arrCol = array();

        $data_ok = false;
        for($i=0;$i<$intNumField;$i++)
        {
            $arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
        }
        array_push($resultArray,$arrCol);
$data_ok = true;
    }
    if($data_ok) {
     $response["status"] = 101;
       $response["message"] = "Success";
         $response["result"] = $resultArray;
     echo json_encode($response);
}
else
{
 $response["status"] = 100;
  $response["message"] = "No category exist in database";

 echo json_encode($response);
}
    mysql_close($objConnect);

我想要的是这样的

{
  "status": 101,
  "message": "Success",
  "result": [
    {
      "order_id": "38",
      "order_product_id": "49",
      "product_id": "377",
      "product_name": "Pineapple Cake 2 Kg Eggless + Free Surprise Gift",
      "result2": [
        {
          "option_name": "Delivery Time",
          "option_value": "4:00am to 6:00am",

        },
        {
          "option_name": "Message For Cake",
          "option_value": "Second order first product",

        }
      ],
      "model": "Cake B22",
      "quantity": "1"
    },
    {
      "order_id": "38",
      "order_product_id": "50",
      "product_id": "339",
      "product_name": "Pineapple Cake 1 Kg + Free Surprise Gift",
      "result2": [
        {
          "option_name": "Make it Eggless",
          "option_value": "No",

        },
        {
          "option_name": "Delivery Time",
          "option_value": "4:00am to 6:00am",

        },
        {
          "option_name": "Message For Cake",
          "option_value": "Second order first product",

        }
      ],
      "model": "Cake A7",
      "quantity": "1"
    }
  ]
}

提前谢谢你 Priyanka

1 个答案:

答案 0 :(得分:0)

我拿了你的样本JSON并编写了以下内容来获得预期的输出。希望它有所帮助。

$data = '[
    {
      "order_id": "38",
      "order_product_id": "49",
      "product_id": "377",
      "product_name": "Pineapple Cake 2 Kg Eggless + Free Surprise Gift",
      "option_name": "Delivery Time",
      "option_value": "4:00am to 6:00am",
      "model": "Cake B22",
      "quantity": "1"

    },
    {
      "order_id": "38",
      "order_product_id": "49",
      "product_id": "377",
      "product_name": "Pineapple Cake 2 Kg Eggless + Free Surprise Gift",
      "option_name": "Message For Cake",
      "option_value": "Second order first product",
      "model": "Cake B22",
      "quantity": "1"
    },
    {
      "order_id": "38",
      "order_product_id": "50",
      "product_id": "339",
      "product_name": "Pineapple Cake 1 Kg + Free Surprise Gift",
      "option_name": "Make it Eggless",
      "option_value": "No",
      "model": "Cake A7",
      "quantity": "1"
    },
    {
      "order_id": "38",
      "order_product_id": "50",
      "product_id": "339",
      "product_name": "Pineapple Cake 1 Kg + Free Surprise Gift",
      "option_name": "Delivery Time",
      "option_value": "4:00am to 6:00am",
      "model": "Cake A7",
      "quantity": "1"
    },
    {
      "order_id": "38",
      "order_product_id": "50",
      "product_id": "339",
      "product_name": "Pineapple Cake 1 Kg + Free Surprise Gift",
      "option_name": "Message For Cake",
      "option_value": "second order 2nd cake",
      "model": "Cake A7",
      "quantity": "1"
    }
  ]';

$data = json_decode($data, true);

$final = [];
foreach ($data as $_d) {

    $index = $_d['order_id'] . '.' . $_d['order_product_id'] . '.' . $_d['product_id'];
    if (isset($final[$index])) {

        $final[$index]['result2'][] = [
            'option_name' => $_d['option_name'],
            'option_value' => $_d['option_value']
        ];

    } else {

        $final[$index] = [
            'order_id' => $_d['order_id'],
            'order_product_id' => $_d['order_product_id'],
            'product_id' => $_d['product_id'],
            'product_name' => $_d['product_name'],
            'model' => $_d['model'],
            'quantity' => $_d['quantity']
        ];

        $final[$index]['result2'][] = [
            'option_name' => $_d['option_name'],
            'option_value' => $_d['option_value']
        ];
    }
}

print_r(array_values($final));

输出:

Array
(
    [0] => Array
        (
            [order_id] => 38
            [order_product_id] => 49
            [product_id] => 377
            [product_name] => Pineapple Cake 2 Kg Eggless + Free Surprise Gift
            [model] => Cake B22
            [quantity] => 1
            [result2] => Array
                (
                    [0] => Array
                        (
                            [option_name] => Delivery Time
                            [option_value] => 4:00am to 6:00am
                        )

                    [1] => Array
                        (
                            [option_name] => Message For Cake
                            [option_value] => Second order first product
                        )

                )

        )

    [1] => Array
        (
            [order_id] => 38
            [order_product_id] => 50
            [product_id] => 339
            [product_name] => Pineapple Cake 1 Kg + Free Surprise Gift
            [model] => Cake A7
            [quantity] => 1
            [result2] => Array
                (
                    [0] => Array
                        (
                            [option_name] => Make it Eggless
                            [option_value] => No
                        )

                    [1] => Array
                        (
                            [option_name] => Delivery Time
                            [option_value] => 4:00am to 6:00am
                        )

                    [2] => Array
                        (
                            [option_name] => Message For Cake
                            [option_value] => second order 2nd cake
                        )

                )

        )

)