我有下面的代码,我从其中得到下面的数据作为响应。
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
)
)
)
2条答案
按热度按时间x7yiwoj41#
在内部数组上循环,并从那里获取您感兴趣的部分
UPDATE:好的,看起来数组不在另一个数组中,因此使用简单的foreach即可完成
093gszye2#