计算多维数组内值的出现次数

时间:2013-12-15 21:09:02

标签: php arrays count

所以我检索一个json,将其转换为数组,我得到了这个输出:

Array
(
[Sid] => 23888555
[pages] => Array
    (
        [0] => Array
            (
                [id] => 13111071
                [name] => Page 1
                [slots] => Array
                    (
                        [0] => Array
                            (
                                [SlotId] => 6
                                [info] => Array
                                    (
                                        [id] => 5247
                                        [color] => red
                                    )

                            )

                        [1] => Array
                            (
                                [SlotId] => 4
                                [info] => Array
                                    (
                                        [id] => 5267
                                        [color] => blue
                                    )

                            )

                        [2] => Array
                            (
                                [SlotId] => 7
                                [info] => Array
                                    (
                                        [id] => 5267
                                        [color] => green
                                    )

                            )                       

                    )

            )

        [1] => Array
            (
                [id] => 13111072
                [name] => Page 2
                [slots] => Array
                    (
                        [0] => Array
                            (
                                [SlotId] => 6
                                [info] => Array
                                    (
                                        [id] => 5247
                                        [color] => red
                                    )

                            )

                        [1] => Array
                            (
                                [SlotId] => 4
                                [info] => Array
                                    (
                                        [id] => 5267
                                        [color] => blue
                                    )

                            ) 
                    )

            )

    )

)

我没有任何问题阅读它,我想做的是每一页我有多少相似的“最后”id。

例如:

[pages][0][slots][0][info][id]
[pages][0][slots][1][info][id]
[pages][0][slots][3][info][id]

对于第1页,我想比较它们之间的这3个ID并计算出现次数。

[pages][1][slots][0][info][id]
[pages][1][slots][1][info][id]

对于第2页,我想比较它们之间的这两个ID并计算出现次数。

我想要的输出如下:

page 1 -> 1x5247
       -> 2x5267

page 2 -> 1x5247
       -> 1x5267

编辑:

我尝试使用

 foreach ($data['pages'] as $item) { 
    foreach ($item['slots'] as $slotnumber => $value) {
         print_r(array_count_values($item['slots'][$slotnumber]['info'])); 
    } 
} 

返回给我:

Array ( [5247] => 1 [red] => 1 ) 
Array ( [5267] => 1 [blue] => 1 ) 
Array ( [5267] => 1 [green] => 1 ) 

所以我想我可以使用它,但我不知道如何。

2 个答案:

答案 0 :(得分:0)

我手动声明了数组,然后创建了countstuff()函数。这应该适合你。我测试了它,它在我的最后工作。经历了所有这些麻烦后,如果你选择我的答案并投票,我真的很感激。

<?php


$data = Array("Sid" => "23888555", "pages" => Array("0" => Array("id" => "13111071", "name" => "Page 1", "slots" => Array("0" => Array("SlotId" => "6", "info" => Array("id" => "5247", "color" => "red")), "1" => Array("SlotId" => "4", "info" => Array("id" => "5267", "color" => "blue")), "2" => Array("SlotId" => "7","info" => Array("id" => "5267", "color" => "green")))),

       "1" => Array
        (
            "id" => "13111072",
            "name" => "Page 2",
            "slots" => Array
                (
                    "0" => Array
                        (
                            "SlotId" => "6",
                            "info" => Array
                                (
                                    "id" => "5247",
                                    "color" => "red"
                                )

                        ),

                    "1" => Array
                        (
                            "SlotId" => "4",
                            "info" => Array
                                (
                                    "id" => "5267",
                                    "color" => "blue"
                                )
                        ) 
                )

        )

   )

);
//End of array declaration






//Now the really convoluted coding starts


//Create a function
function countstuff($yourarray){

    foreach($yourarray as $mainarraykey => $mainarray){
       if($mainarraykey == "pages"){
          foreach($mainarray as $pageskey => $pagesarray){

            //echo "Page \"$pageskey\"<br/>";

                foreach($pagesarray as $pagessubarraykey => $pagessubarray_array){
                    if($pagessubarraykey == "slots"){

                        foreach($pagessubarray_array as $slotskey => $slots){

                            foreach($slots as $slotssubkey => $slotssub){
                                if($slotssubkey == "info"){

                                    foreach($slotssub as $key => $value){
                                        if($key == "id"){
                                        //echo $value."<br/>";
                                        $pages[$pageskey][] = $value;
                                        }
                                    }
                                }
                            }
                        }

                    }

              }

        }
      }
   }


    return $pages;

}




//Execute the countstuff() function
$output = countstuff($data);



function showresults($input){    
    foreach($input as $pagekey => $page){

        echo "Page $pagekey:<br/>";    

        $results = array_count_values($page);

        foreach($results as $resultkey => $result){
            echo $result."x".$resultkey."<br/>";
        }

    echo "<br/>";
    }
}


showresults($output);

?>

答案 1 :(得分:0)

我尝试了一些让我知道你们对此的看法。

我得到每个id然后将它们输入数组然后使用array_count_values

$array_ids = array();

foreach ($data['pages'] as $item) {

    $numberOfElements = count($item['slots']);
    $z= 0; 

    foreach ($item['slots'] as $slotnumber => $value) {

        $array_ids[$z] = $item['slots'][$slotnumber]['info']['id'];

        // search for occurrences when the array is full 
        if (count($array_ids) == $numberOfElements) {

            var_dump(array_count_values($array_ids));

            // reset the array to 0 every time we loop through the whole infos
            $array_ids = array();
        }
        $z++;
    }
}

这似乎适用于每一页。

array (size=2)
  5267 => int 2
  5247 => int 1

array (size=2)
  5267 => int 1
  5247 => int 1