0

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

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
                )

        )

)
4

2 回答 2

0
$newArray = []; // initialise the array
foreach ($data as $key=> $value) { 
    $newArray[] = [
            'id' => $value['product']['id'], 
            'name' => $value['product']['name'] 
    ];                        
}
于 2021-10-24T16:14:14.303 回答
0

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

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

$newArray = []; // initialise the array
foreach ($data as $inner) { 
    
    $newArray[] = [ 'id' => $inner['product']['id'], 
                    'name' => $inner['product']['name'] 
                ]
}
于 2021-10-24T14:57:24.180 回答