PHP多维数组计数重复值并添加相应的值

时间:2019-05-17 09:37:48

标签: php sorting multidimensional-array mapping

我有一个php数组

Array
(
    [0] => Array
        (
            [location] => Kansas
            [kit] => Moving Kit
            [quantity] => 1
        )

[1] => Array
    (
        [location] => Louisiana
        [kit] => Anchored4Life DVD Kit
        [quantity] => 1
    )

[2] => Array
    (
        [location] => Louisiana
        [kit] => Anchored4Life DVD Kit
        [quantity] => 1
    )

[3] => Array
    (
        [location] => Louisiana
        [kit] => Anchored4Life DVD Kit
        [quantity] => 1
    )

[4] => Array
    (
        [location] => Pennsylvania
        [kit] => Anchored4Life DVD Kit
        [quantity] => 5
    )

[5] => Array
    (
        [location] => Pennsylvania
        [kit] => Welcome Kit
        [quantity] => 1
    )

)

我想对它进行排序,以便在套件更薄的情况下根据位置和套件以及套件的数量和数量接收数据。

Louisiana Anchored4Life DVD Kit上重复执行一次,所以我想知道在每种状态下,例如Louisiana Anchored4Life DVD Kit数量3的订单数量。

我尝试过array_count_values(),但这仅根据字符串进行计数,我需要用于多个工具包,而工具包尚未预定义。

2 个答案:

答案 0 :(得分:0)

您的问题根本不清楚。但是,我认为您想计算按产品分组的订单。我建议您创建一个非常简单的助手,例如:

    public function getCount($items, $kitName) : int
    {
        $count = 0;
        foreach ($items as $item) {
            if ($item['kit'] === $kitName) {
                $count ++;
            }
        }

        return $count;
    }

此帮助程序会获得与给定套件对应的订单数量

答案 1 :(得分:0)

我本来打算发表,但随后我看到其他答案。

您说的是地点分组,所以我认为这就是您所需要的:
我将关联阵列中的位置分组,然后对不同的试剂盒进行子阵列,如果已经添加了试剂盒,则求和。

        rate = rospy.Rate(self._publish_rate)
        while not rospy.is_shutdown():
            if self._msg_lock.acquire(False):
                msg = self._last_msg
                self._last_msg = None
                self._msg_lock.release()
            else:
                rate.sleep()
                continue

            if msg is not None:
                np_image = self._cv_bridge.imgmsg_to_cv2(msg, 'mono8')
                np_image = cv2.merge((np_image, np_image, np_image))

                # Run detection
                results = self._model.detect([np_image], verbose=0)
                result = results[0]
                result_msg = self._build_result_msg(msg, result) #TODO

                self._result_pub.publish(result_msg)
                self._mask_pub.publish(self.center_mask(result))


                # Visualize results
                if self._visualization:
                    vis_image = self._visualize(result, np_image)
                    cv_result = np.zeros(shape=vis_image.shape, dtype=np.uint8)
                    cv2.convertScaleAbs(vis_image, cv_result)
                    image_msg = self._cv_bridge.cv2_to_imgmsg(cv_result, 'bgr8')
                    vis_pub.publish(image_msg)

            rate.sleep()

    def center_mask(self, result):
        centers = xyz()
        pt = Point()

        ret, thresh = cv2.threshold(result['masks'], 0, 255, 0)
            #
        im, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
        if(contours != []):
            for c in contours:
                print("in the forloop met contouren")
                M = cv2.moments(c)

                if(M["m10"] and M["m01"] and M["m00"]) != 0:
                    cX = int(M["m10"] / M["m00"])
                    cY = int(M["m01"] / M["m00"])

                    print("cx: ", cX)
                    print("cy:", cY)
                    pt.x = cX
                    pt.y = cY
                    pt.z = 0

                    centers.points.append(copy.deepcopy(pt))
        else:
            print('no pot found else')
            pt.x = 0
            pt.y = 0
            pt.z = 0
            centers.points.append(copy.deepcopy(pt))

        return centers

这将返回:

foreach($arr as $sub){
    if(!isset($res[$sub['location']])){
        $res[$sub['location']][$sub['kit']] = $sub;
    }elseif(!isset($res[$sub['location']][$sub['kit']])){
        $res[$sub['location']][$sub['kit']] = $sub;
    }else{
        $res[$sub['location']][$sub['kit']]['quantity'] += $sub['quantity'];
    }
}

var_dump($res);

https://3v4l.org/DgPKp