2

I have one array like as below

[0] = Array
    (
        [title] => Khamna,
        [booking_number] = 200003852,
        [quantity] =1,
        [unit_price] = 5.00,
        [gross_total] = 5.00,
    )

[1] = Array
    (
        [title] = Khamna
        [booking_number] = 200003857
        [quantity] = 2
        [unit_price] = 5.00
        [gross_total] = 410.00
    )

[2] = Array
    (
        [title] = Khamna
        [booking_number] = 200003857
        [quantity] = 2
        [unit_price] = 200.00
        [gross_total] = 410.00
    )

[3] = Array
    (
        [title] = Khamna
        [booking_number] = 200003858
        [quantity] = 1
        [unit_price] = 200.00
        [gross_total] = 200.00
    )

I want result output such as booking_number will be key of array and sub array will be based on "unit_price" and "quantity".. unit_price and quantity as subarray more than 1 or 2 or 3

[200003852] => Array
    (
        [title] =Khamna
        [gross_total] = 5.00
        [detail] = Array
            ( 0 => array([quantity] = 1
                [unit_price] = 5.00),
        )

    )

[200003857] = Array
    (
        [title] = Khamna
        [gross_total] = 410.00,
        [detail] = Array
            ( 0 => array([quantity] = 2
                [unit_price] = 5.00),
              1 => array([quantity] = 2
                [unit_price] = 200.00),                 
            )
    )

[200003857] = Array
    (
        [title] = Khamna
        [gross_total] = 10.00
        [detail] = Array
            ( 0 => array([quantity] = 2
                [unit_price] = 5.00),

            )
     )
1
  • 1
    Just iterate your array and create a new one that looks like the way you want. What have you tried so far? Commented Aug 31, 2016 at 11:05

3 Answers 3

2

This will help you...

$result = array();
foreach($array as $row) {
    if(!isset($result[$row['booking_number']])) {
        $result[$row['booking_number']] = array(
            'title' => $row['title'],
            'gross_total' => $row['gross_total']
        );
    } 
    $result[$row['booking_number']]['details'][] = array(
        'quantity' => $row['quantity'],
        'unit_price' => $row['unit_price']
    );
}
Sign up to request clarification or add additional context in comments.

Comments

0

This code will help you.

$res = [];
foreach($array as $v){
  $res[$v['booking_number']] = 
  [
  'title'=>$v['title'],
  'gross_total'=>$v['gross_total'],
  'detail'=>[['quantity'=>$v['quantity']],
             ['unit_price'=>$v['unit_price']]]
  ];
}
echo '<pre>'; print_r($res);

Comments

0

just run this after your orignal array

foreach ($arra as $key) {
 $arr[$key['booking_number']]=array('title'=> $key['title'],'gross_total'  
     =>$key['gross_total'],'detail'=>array('0' => 
     $key['unit_price'],'1' => $key['quantity']) );

         }

Note: $arra is your original array

It will print your array like this using echo "<pre>"; print_r($arr);

    Array
(
    [200003852] => Array
        (
            [title] => Khamna
            [gross_total] => 5
            [detail] => Array
                (
                    [0] => 5
                    [1] => 1
                )

        )

    [200003857] => Array
        (
            [title] => Khamna
            [gross_total] => 410
            [detail] => Array
                (
                    [0] => 5
                    [1] => 2
                )

        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.