• How to two array indexs in one subarray if have a same value In php

    How to two array indexs in one subarray if have a same value In php

    Silver Member Asked on September 21, 2017 in Php.
    Add Comment
  • 1 Answer(s)
    
    public function arrayFilter($rows)
    
    { $total= count($rows);
    
    $indexed = [];
    
    for($i=0; $i<$total; $i++) {
    
    $index = $this->getParentIndex($indexed, $rows[$i]['Ref No']);
    
    if ($index === null) {
    
    $indexed[] = [
    
    'data' => $rows[$i]['Ref No'],
    
    'subs' => [
    
    ['data' => $rows[$i] ],
    
    ],
    
    ];
    
    }else{
    
    $indexed[$index]['subs'][] = ['data' => $rows[$i] ];
    
    }
    
    }
    
    pr($indexed);die;
    
    }
    
    function getParentIndex($array, $parent_id) {
    
    for($i=0;$i<count($array);$i++) {
    
    if ($array[$i]['data'] == $parent_id) return $i;
    
    }
    
    return null;
    
    }
    
    

    OutPut

    
    Array
    
    (
    
    [0] => Array
    
    (
    
    [data] => WCFF016122021307
    
    [subs] => Array
    
    (
    
    [0] => Array
    
    (
    
    [data] => Array
    
    (
    
    [Consignment Number] => X19232196
    
    [Ref No] => WCFF016122021307
    
    [Consignee Name] => SriniVenkat
    
    [Consignee ADD1] => 1B Sai Sukrta Apartments
    
    [Consignee ADD2] => Anna Nagar 3rd Main Road Velachery
    
    [Consignee Phone] =>
    
    [Mobile No] => 8870674342
    
    [City Name] => CHENNAI
    
    [Pincode] => 122002
    
    [No Of Pieces] => 1
    
    [Weight] => 0.068
    
    [Shipment Type] => NONDOX
    
    [Product Type] => PRIORITY
    
    [Service Type] => PEX
    
    [Invoice Value] => 2.54
    
    [Insured By] =>
    
    [Vas Product] => NDX
    
    [COD Amount] => 0
    
    [Mode Of Collecttion] => CASH
    
    [IN Favour] =>
    
    [Content Desc] => Set of Nail Stamp Stencil
    
    [remark] => AM2016122054256
    
    [Location] => DEL
    
    )
    
    )
    
    [1] => Array
    
    (
    
    [data] => Array
    
    (
    
    [Consignment Number] => X19232197
    
    [Ref No] => WCFF016122021307
    
    [Consignee Name] => DrRAMESHWAR VYAS
    
    [Consignee ADD1] => Vishwakarma Gate Ram Temple Lane
    
    [Consignee ADD2] => Bikaner
    
    [Consignee Phone] =>
    
    [Mobile No] => 7073246783
    
    [City Name] => BIKANER
    
    [Pincode] => 334001
    
    [No Of Pieces] => 1
    
    [Weight] => 0.028
    
    [Shipment Type] => NONDOX
    
    [Product Type] => PRIORITY
    
    [Service Type] => PEX
    
    [Invoice Value] => 1.84
    
    [Insured By] =>
    
    [Vas Product] => NDX
    
    [COD Amount] => 0
    
    [Mode Of Collecttion] => CASH
    
    [IN Favour] =>
    
    [Content Desc] => Portable Pocket Medical Torch Light
    
    [remark] => AM2016122034714
    
    [Location] => DEL
    
    )
    
    )
    
    )
    
    )
    
    [1] => Array
    
    (
    
    [data] => WCFF016122021327
    
    [subs] => Array
    
    (
    
    [0] => Array
    
    (
    
    [data] => Array
    
    (
    
    [Consignment Number] => X19232198
    
    [Ref No] => WCFF016122021327
    
    [Consignee Name] => RamyaK C
    
    [Consignee ADD1] => Office Of Padiyoor Gramapanchayath
    
    [Consignee ADD2] => Irinjalakuda
    
    [Consignee Phone] =>
    
    [Mobile No] => 9745664966
    
    [City Name] => THRISSUR
    
    [Pincode] => 680688
    
    [No Of Pieces] => 1
    
    [Weight] => 0.16
    
    [Shipment Type] => NONDOX
    
    [Product Type] => PRIORITY
    
    [Service Type] => PEX
    
    [Invoice Value] => 6.6
    
    [Insured By] =>
    
    [Vas Product] => NDX
    
    [COD Amount] => 0
    
    [Mode Of Collecttion] => CASH
    
    [IN Favour] =>
    
    [Content Desc] => fishing rod
    
    [remark] => AM2016122024785
    
    [Location] => DEL
    
    )
    
    )
    
    )
    
    )
    
    [2] => Array
    
    (
    
    [data] => WCFF016122021330
    
    [subs] => Array
    
    (
    
    [0] => Array
    
    (
    
    [data] => Array
    
    (
    
    [Consignment Number] => X19232199
    
    [Ref No] => WCFF016122021330
    
    [Consignee Name] => kundnapanchal
    
    [Consignee ADD1] => 202 bhagirath coach and metal fab pvt ltd
    
    [Consignee ADD2] => sector 1 pithampu
    
    [Consignee Phone] =>
    
    [Mobile No] => 9993091577
    
    [City Name] => PITHAMPUR
    
    [Pincode] => 454775
    
    [No Of Pieces] => 1
    
    [Weight] => 0.144
    
    [Shipment Type] => NONDOX
    
    [Product Type] => PRIORITY
    
    [Service Type] => PEX
    
    [Invoice Value] => 4.94
    
    [Insured By] =>
    
    [Vas Product] => NDX
    
    [COD Amount] => 0
    
    [Mode Of Collecttion] => CASH
    
    [IN Favour] =>
    
    [Content Desc] => COSMETIC BRUSH
    
    [remark] => AM2016122072903
    
    [Location] => DEL
    
    )
    
    )
    
    )
    
    )
    
    )
    
    
    Silver Member Answered on September 21, 2017.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.