Yii2 合并数组
Yii2 merge arrays
我需要 json API 的合并数组。我有 3 个请求:
$data['winners'] = Winners::find()->limit(8)->all();
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)->where(['coupon' => $value->coupon])->one();
我需要获取所有 $data['winners'],从 $data['winners'] 中获取具有 ID 的用户,并从 $data['winners'] 中获取产品。在我需要合并 1 json 我所有的数据之后。
我这样试:
$data['winners'] = Winners::find()->limit(8)->all();
foreach ($data['winners'] as $value){
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)->where(['coupon' => $value->coupon])->one();
$result = ArrayHelper::merge($product, $user);
}
如果我理解正确,你质疑你需要一个包含所有 $data[winners] 实例的数组,并且 foreach 实例合并相关的 Produce 和 User
那么你可以使用
$result[] = array_merge($value, $product, $user);
.
$data['winners'] = Winners::find()->limit(8)->all();
foreach ($data['winners'] as $value){
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)->where(['coupon' => $value->coupon])->one();
$result[] = array_merge($value, $product, $user);
}
我找到了解决方案:
$winners = Winners::find()->limit(8)->all();
$i = 0;
foreach ($winners as $value){
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)->where(['coupon' => $value->coupon])->one();
$data[$i]['image'] = $product->prize_image;
$data[$i]['user'] = $user->firstname.' '.$user->lastname;
$data[$i]['title'] = $product->win_title;
$i++;
}
return $data;
PHP中有一个更优雅的解决方案:
$winners = Winners::find()->limit(8)->all();
foreach ($winners as $value){
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)
->where(['coupon' => $value->coupon])->one();
$data[] = [
'image'=> $product->prize_image,
'user' => $user->firstname.' '.$user->lastname,
'title' => $product->win_title,
];
}
return $data;
$data[]
中的PHP array operator[]
在$data
数组的末尾添加了一个数组元素。
我需要 json API 的合并数组。我有 3 个请求:
$data['winners'] = Winners::find()->limit(8)->all();
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)->where(['coupon' => $value->coupon])->one();
我需要获取所有 $data['winners'],从 $data['winners'] 中获取具有 ID 的用户,并从 $data['winners'] 中获取产品。在我需要合并 1 json 我所有的数据之后。
我这样试:
$data['winners'] = Winners::find()->limit(8)->all();
foreach ($data['winners'] as $value){
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)->where(['coupon' => $value->coupon])->one();
$result = ArrayHelper::merge($product, $user);
}
如果我理解正确,你质疑你需要一个包含所有 $data[winners] 实例的数组,并且 foreach 实例合并相关的 Produce 和 User 那么你可以使用
$result[] = array_merge($value, $product, $user);
.
$data['winners'] = Winners::find()->limit(8)->all();
foreach ($data['winners'] as $value){
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)->where(['coupon' => $value->coupon])->one();
$result[] = array_merge($value, $product, $user);
}
我找到了解决方案:
$winners = Winners::find()->limit(8)->all();
$i = 0;
foreach ($winners as $value){
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)->where(['coupon' => $value->coupon])->one();
$data[$i]['image'] = $product->prize_image;
$data[$i]['user'] = $user->firstname.' '.$user->lastname;
$data[$i]['title'] = $product->win_title;
$i++;
}
return $data;
PHP中有一个更优雅的解决方案:
$winners = Winners::find()->limit(8)->all();
foreach ($winners as $value){
$user = User::findIdentity($value->user_id);
$product = Products::find()->localized($lang)
->where(['coupon' => $value->coupon])->one();
$data[] = [
'image'=> $product->prize_image,
'user' => $user->firstname.' '.$user->lastname,
'title' => $product->win_title,
];
}
return $data;
$data[]
中的PHP array operator[]
在$data
数组的末尾添加了一个数组元素。