0

I have below code from which i am getting Below data in response..

public function getCategory(){
        $this->autoRender = False;
        $list = TableRegistry::get('Subproducts');
        $supplierId = $this->request->data['supplierId'];
        if($this->request->is('ajax')) {
            if(!empty($supplierId)) {
                $query1 = $list->find()->contain(['Products'])
                ->where(['Subproducts.supplier_id =' => $supplierId]);
                $data = $query1->hydrate(false)->toArray();
                if(!empty($data)){   
                  **Print_r($data);**                
                    **foreach ($data as $value){ 
                        print_r($value); 
                                          
                    }**                       

                    //echo json_encode(array("category"=>$newArray));
                    exit;
                }            
            } 
        }           
    }

Below Data i am getting from above code

    Array
(   
    [supplier_id] => 40
    [product] => Array
        (
            [id] => 45
            [name] => PLASTIC ITEM
            [is_deleted] => 0

        )

)
Array
(    
    [supplier_id] => 40
    [product] => Array
        (
            [id] => 50
            [name] => RAW MATERIAL
            [is_deleted] => 0

        )

)

I want to collect Id and Name of product array in $newArray, but i am not able to iterate product array and dont know how to add id and name of product in $newArray. Can anyone please help me to iterate product array value and create new array which contains only Id and Name.

I need help inside to iterate values and create new array with only id and name of product array.

**Print_r($data) is getting below data, I have added print_r($data); like below code, i mean before foreach loop..

if(!empty($data)){ 
                    print_r($data);
                    foreach ($data as $value) { }
}

   Array
(
    [0] => Array
        (
            [id] => 468
            [supplier_id] => 40
            [buy_price] => 6.23
            [sell_price] => 7.87
            [category_id] => 45
            [product_name] => test1
            [is_deleted] => 0
            [created] => Cake\I18n\FrozenTime Object
                (
                    [date] => 2021-10-12 09:48:20.000000
                    [timezone_type] => 3
                    [timezone] => UTC
                )

        )

    [1] => Array
        (
            [id] => 469
            [supplier_id] => 40
            [buy_price] => 44.89
            [sell_price] => 7.87
            [category_id] => 50
            [product_name] => test
            [is_deleted] => 0
            [created] => Cake\I18n\FrozenTime Object
                (
                    [date] => 2021-10-12 11:44:28.000000
                    [timezone_type] => 3
                    [timezone] => UTC
                )

        )

)
  • Can I check, the array you show us is coming from the `print_r($value);` inside that foreach loop? Or did you run the print_r on `print_r($data)` – RiggsFolly Oct 24 '21 at 14:53
  • @RiggsFolly, Yes the shown array is coming from print_r($value); inside foreach loop – Tinku Tiwari Oct 24 '21 at 14:54

2 Answers2

0

Loop over the inner array and grab the parts you are interested in from there

UPDATE: Ok so it looks like the array is not inside another so a simple foreach will do it

$newArray = []; // initialise the array
foreach ($data as $inner) { 
    
    $newArray[] = [ 'id' => $inner['product']['id'], 
                    'name' => $inner['product']['name'] 
                ]
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • I tried your code, but unfortunately it was throewing error, dont know why it is not picking product array. I mean to say is i have added print_r($inner); but it is not printing product array.. i am getting below oytput with error.. PFA.. 468406.237.8745test10Cake\I18n\FrozenTime Object ( [date] => 2021-10-12 09:48:20.000000 [timezone_type] => 3 [timezone] => UTC ) {"message":"An Internal Error Has Occurred.","url":"\/stocks\/get-category","code":500} – Tinku Tiwari Oct 24 '21 at 15:25
  • I did like this $newArray = []; foreach ($data as $value){ foreach ($value as $inner) { print_r($inner); $newArray[] = [ 'id' => $inner['product']['id'], 'name' => $inner['product']['name'] ]; } //print_r($newArray);exit; } – Tinku Tiwari Oct 24 '21 at 15:27
  • Can you do a `print_r($data);` just before you start the loop and show the result in you question please – RiggsFolly Oct 24 '21 at 15:39
  • I have added print_r($data); values in my question, but not getting why it is coming twice..plz check my question for response data. – Tinku Tiwari Oct 24 '21 at 16:01
  • Did you do it Inside the foreach or before it? – RiggsFolly Oct 24 '21 at 16:03
  • Yes mistakenly...Now i corrected this and placed outside foreach loop and now its not printing repeated data – Tinku Tiwari Oct 24 '21 at 16:08
  • Got it with my little correction in code..below is my answer..Still thank you for your time and help i am accepting your answer too.. – Tinku Tiwari Oct 24 '21 at 16:14
0
$newArray = []; // initialise the array
foreach ($data as $key=> $value) { 
    $newArray[] = [
            'id' => $value['product']['id'], 
            'name' => $value['product']['name'] 
    ];                        
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149