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

x0fgdtte  于 2022-11-11  发布在  PHP
关注(0)|答案(2)|浏览(137)

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

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和名称的新数组。
我需要内部的帮助来迭代值,并创建只有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
                )

        )

)
x7yiwoj4

x7yiwoj41#

在内部数组上循环,并从那里获取您感兴趣的部分
UPDATE:好的,看起来数组不在另一个数组中,因此使用简单的foreach即可完成

$newArray = []; // initialise the array
foreach ($data as $inner) { 

    $newArray[] = [ 'id' => $inner['product']['id'], 
                    'name' => $inner['product']['name'] 
                ]
}
093gszye

093gszye2#

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

相关问题