我在这个阵列中有点混乱......
其实我有这样的数组
Array
(
[0] => stdClass Object
(
[restaurant_id] => 1
[food_item] => Chicken Spicy Pizza
)
[1] => stdClass Object
(
[restaurant_id] => 1
[food_item] => Pepper Chicken Sandwich
)
[2] => stdClass Object
(
[restaurant_id] => 6
[food_item] => Chicken Tikka Roll
)
[3] => stdClass Object
(
[restaurant_id] => 6
[food_item] => Grilled Chicken
)
)
这里我需要创建逗号分隔值food_item
并且必须有一个条件,具有相同restaurant_id
的数组也合并..
即; ..
我的最终数组应该是这样的..
Array
(
[0] => stdClass Object
(
[restaurant_id] => 1
[food_item] => Chicken Spicy Pizza,Pepper Chicken Sandwich
)
[1] => stdClass Object
(
[restaurant_id] => 6
[food_item] => Chicken Tikka Roll,Grilled Chicken
)
)
任何建议?!! ...
谢谢!。
答案 0 :(得分:2)
让您的实际数组位于名为// Controller for binding galleries to view
app.controller('newMainCtrl', function($scope, ImageGalleriesService) {
$scope.galleries = ImageGalleriesService.getGalleries();
$scope.$watch(function(){
return ImageGalleriesService.getGalleries();
}, function(newVal){
$scope.galleries = newVal;
});
});
// Single Image controller
app.controller('imageCtrl', function($scope, ImageGalleriesService) {
$scope.imageinit = function(value)
{
$scope.imageitem = value;
};
});
// Gallery controller
app.controller('galleryCtrl', function($scope, ImageGalleriesService) {
$scope.init = function(gallery) {
$scope.gallery = gallery;
};
$scope.duplicate = function()
{
ImageGalleriesService.addImageGallery($scope.gallery, null, 2);
};
});
// Service that holds all the galleries
app.service('ImageGalleriesService', function() {
var imagegalleries = [];
this.getGalleries = function(){
return imagegalleries;
};
this.addImageGallery = function(imagegallery){
imagegalleries.push(imagegallery);
};
});
的变量中。所以这是代码
$actualArr
这是代码链接
答案 1 :(得分:2)
您可以使用array_reduce来完成它
$myArr = array_reduce($myArr, function ($carry, $item) {
if (!isset($carry[$item->restaurant_id])) {
$carry[$item->restaurant_id] = $item;
} else {
$carry[$item->restaurant_id]->food_item .= ',' . $item->food_item;
}
return $carry;
}, array());