1

これを説明できるかしら。

多次元配列があります。その配列に表示される特定の値の数を取得したいと思います。

以下に、配列のスニペットを示します。profile_typeで確認しています。

だから私は配列にprofile_typeのカウントを表示しようとしています

編集

申し訳ありませんが、主なものではなく、何かについて言及するのを忘れました。profile_type==pのカウントが必要です。

Array
(
    [0] => Array
        (
            [Driver] => Array
                (
                    [id] => 4
                    [profile_type] => p                    
                    [birthyear] => 1978
                    [is_elite] => 0
                )
        )
        [1] => Array
        (
            [Driver] => Array
                (
                    [id] => 4
                    [profile_type] => d                    
                    [birthyear] => 1972
                    [is_elite] => 1
                )
        )

)
4

5 に答える 5

2

RecursiveArrayIteratorを使用した簡単なソリューションなので、寸法を気にする必要はありません。

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));

$counter = 0
foreach ($iterator as $key => $value) {
  if ($key == 'profile_type' && $value == 'p') {
    $counter++;
  }
}
echo $counter;
于 2012-04-25T12:16:27.497 に答える
0

Hi You can get count of profuke_type==p from a multi dimensiona array

    $arr = array();
    $arr[0]['Driver']['id'] = 4;
    $arr[0]['Driver']['profile_type'] = 'p';
    $arr[0]['Driver']['birthyear'] = 1978;
    $arr[0]['Driver']['is_elite'] = 0;


    $arr[1]['Driver']['id'] = 4;
    $arr[1]['Driver']['profile_type'] = 'd';
    $arr[1]['Driver']['birthyear'] = 1972;
    $arr[1]['Driver']['is_elite'] = 1;

    $arr[2]['profile_type'] = 'p';
    $result = 0;
    get_count($arr, 'profile_type', 'd' , $result);
    echo $result;
    function get_count($array, $key, $value , &$result){
        if(!is_array($array)){
            return;
        }

        if($array[$key] == $value){
            $result++;
        }

        foreach($array AS $arr){
            get_count($arr, $key, $value , $result);
        }
    }

try this..

thanks

于 2012-04-25T13:20:47.123 に答える
0

このような何かがうまくいくかもしれません...

$counts = array();
foreach ($array as $key=>$val) {
    foreach ($innerArray as $driver=>$arr) {
        $counts[] = $arr['profile_type']; 
    }
}

$solution = array_count_values($counts);
于 2012-04-25T12:15:25.800 に答える
0

私は次のようなことをします:

$profile = array();
foreach($array as $elem) {
    if (isset($elem['Driver']['profile_type'])) {
        $profile[$elem['Driver']['profile_type']]++;
    } else {
        $profile[$elem['Driver']['profile_type']] = 1;
    }
}
print_r($profile);
于 2012-04-25T12:15:44.723 に答える
0

array_walk($ array、 "test")を使用して、配列の各項目の'type'をチェックし、array_walk($ arrayElement、 "test")の型の項目を再帰的に呼び出す関数"test"を定義することもできます。 、それ以外の場合は条件をチェックします。条件が満たされる場合は、カウントをインクリメントします。

于 2012-04-25T13:00:02.203 に答える