如何在 foreach 中迭代值并在 cakephp 中使用新值创建数组 3.x

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

我有下面的代码,我从中得到下面的数据作为响应..

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;
                }            
            } 
        }           
    }

下面是我从上面的代码得到的数据

    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

        )

)

我想在 $newArray 中收集 产品数组的 ID 和名称,但我无法迭代产品数组,也不知道如何在 $newArray 中添加产品的 ID 和名称$新数组。谁能帮我迭代产品数组值并创建仅包含 Id 和 Name 的新数组。

我需要内部帮助来迭代值并创建仅包含产品数组 ID 和名称的新数组。

**Print_r($data) 低于数据,我添加了 print_r($data);像下面的代码,我的意思是在 foreach 循环之前..

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
                )

        )

)

遍历内部数组并从中获取您感兴趣的部分

更新:好的,看起来数组不在另一个数组中,所以一个简单的 foreach 就可以做到

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