Counting Values in Multidimensional Array

前端 未结 5 2068
慢半拍i
慢半拍i 2020-12-03 18:22

I currently have the following array:

Array(
        [0] => Array
            (
                [user] => Name 1
                [group] => 1
               


        
5条回答
  •  猫巷女王i
    2020-12-03 18:44

    Your initial attempt was close. You were simply using the wrong key inside the loop:

    $newArr = array();
    foreach ($details['user_groups'] as $key => $value) {
            // What you were using:
            // $newArr[$value['user_groups']]++;
    
            // What you should be using:
            $newArr[$value['group']]++;
    }
    

提交回复
热议问题