Home > Net >  How to iterate values in foreach and create array with new values in cakephp 3.x
How to iterate values in foreach and create array with new values in cakephp 3.x

Time:10-25

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
                )

        )

)

CodePudding user response:

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'] 
                ]
}

CodePudding user response:

$newArray = []; // initialise the array
foreach ($data as $key=> $value) { 
    $newArray[] = [
            'id' => $value['product']['id'], 
            'name' => $value['product']['name'] 
    ];                        
}
  • Related