PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 11, 2022

[FIXED] How to iterate values in foreach and create array with new values in cakephp 3.x

 March 11, 2022     arrays, cakephp, cakephp3, php     No comments   

Issue

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
                )

        )

)

Solution

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


Answered By - RiggsFolly
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing