php搜索一维数组值,如果mutidimentional数组中没有值,则添加零值的元素

时间:2018-03-16 07:35:10

标签: php arrays

我有两个数组,一个是小时数组列表,第二个是我的结果数组。我必须使用24小时元素填充$current_play数组。

首先,我将小时数组值搜索到具有$watch_hour值的第二个数组。如果值不匹配,则不填充零值,并添加零值的数组元素。

第一个数组:

$hoursArray=

['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23']; 

第二阵列:

$result = array(
    array(
        "code" => "BA",
        "name" => "BARCA",
        "current_play" => array(
            array(
                "whours" => "20",
                "value" => "6"
            ),
            array(
                "whours" => "5",
                "value" => "6"
            ),
            array(
                "whours" => "6",
                "value" => "6"
            ),
            array(
                "whours" => "8",
                "value" => "6"
            ),
            array(
                "whours" => "9",
                "value" => "6"
            )
       )
   )
);

我正在尝试下面的代码,但它不起作用;

for($i = 0; $i < count ( $hoursArray ); $i ++) {
    foreach ($result as $t){
        if($t['watch_hour'] == $hoursArray[$i]){
            $result[$v['code']]['cplay'][] = array('whours'=> $hoursArray[$i],'value' => $v['value']);
        } else{
            $result[$v['code']]['cplay'][] = array('whours'=> $hoursArray[$i],'value' => "0");
        }
    }
}

必需的数组将是:

Array
(
    [0] => Array
        (
        [code] => BA
        [name] => BARCA
        [current_play] => Array
        (
            [0] => Array
                (
                    [whour] => 0
                    [value] => 0
                )

            [1] => Array
                (
                    [whour] => 1
                    [value] => 0
                )

            [2] => Array
                (
                    [whour] => 2
                    [value] => 0
                )

            [3] => Array
                (
                    [whour] => 3
                    [value] => 0
                )
            [4] => Array
            (
                    [whour] => 4
                    [value] => 6
            )
            [5] => Array
            (
                    [whour] => 5
                    [value] => 6
            )
            [6] => Array
            (
                    [whour] => 6
                    [value] => 6
            )
            and so on for each hours element....
        )
    )
)

2 个答案:

答案 0 :(得分:1)

我试图解决它的方法是使用array_column()将播放的小时数提取到索引数组中。然后每小时检查是否有值,如果没有找到则将其添加。然后取出结果并将其放回主数组(已排序)...

$currentPlay = array_column($result[0]['current_play'], null, "watch_hour" );
foreach ( $hoursArray as $hour )    {
    if ( isset($currentPlay[$hour]) === false )  {
        $currentPlay[$hour] = array(
                "watch_hour" => "$hour",
                "value" => "0"
        );
    }
}
sort($currentPlay);
$result[0]['current_play'] = $currentPlay;
print_r($result);

输出是......

Array
(
    [0] => Array
        (
            [code] => CA
            [name] => Canada
            [current_play] => Array
                (
                    [0] => Array
                        (
                            [watch_hour] => 0
                            [value] => 0
                        )

                    [1] => Array
                        (
                            [watch_hour] => 1
                            [value] => 0
                        )

                    [2] => Array
                        (
                            [watch_hour] => 2
                            [value] => 0
                        )

                    [3] => Array
                        (
                            [watch_hour] => 3
                            [value] => 0
                        )

                    [4] => Array
                        (
                            [watch_hour] => 4
                            [value] => 0
                        )

                    [5] => Array
                        (
                            [watch_hour] => 5
                            [value] => 6
                        )

                    [6] => Array
                        (
                            [watch_hour] => 6
                            [value] => 6
                        )

                    [7] => Array
                        (
                            [watch_hour] => 7
                            [value] => 0
                        )

                    [8] => Array
                        (
                            [watch_hour] => 8
                            [value] => 6
                        )

                    [9] => Array
                        (
                            [watch_hour] => 9
                            [value] => 6
                        )

                    [10] => Array
                        (
                            [watch_hour] => 10
                            [value] => 0
                        )

                    [11] => Array
                        (
                            [watch_hour] => 11
                            [value] => 0
                        )

                    [12] => Array
                        (
                            [watch_hour] => 12
                            [value] => 0
                        )

                    [13] => Array
                        (
                            [watch_hour] => 13
                            [value] => 0
                        )

                    [14] => Array
                        (
                            [watch_hour] => 14
                            [value] => 0
                        )

                    [15] => Array
                        (
                            [watch_hour] => 15
                            [value] => 0
                        )

                    [16] => Array
                        (
                            [watch_hour] => 16
                            [value] => 0
                        )

                    [17] => Array
                        (
                            [watch_hour] => 17
                            [value] => 0
                        )

                    [18] => Array
                        (
                            [watch_hour] => 18
                            [value] => 0
                        )

                    [19] => Array
                        (
                            [watch_hour] => 19
                            [value] => 0
                        )

                    [20] => Array
                        (
                            [watch_hour] => 20
                            [value] => 6
                        )

                    [21] => Array
                        (
                            [watch_hour] => 21
                            [value] => 0
                        )

                    [22] => Array
                        (
                            [watch_hour] => 22
                            [value] => 0
                        )

                    [23] => Array
                        (
                            [watch_hour] => 23
                            [value] => 0
                        )

                )

        )

)

<强>更新 为每个国家做这件事......

foreach ( $result as $key => $country ) {
    // $currentPlay = array_column($country['current_play'], null, "watch_hour" );
    $currentPlay = array();
    foreach ( $country['current_play'] as $item )   {
        $currentPlay[$item["watch_hour"]] = $item;
    }
    foreach ( $hoursArray as $hour )    {
        if ( isset($currentPlay[$hour]) === false )  {
            $currentPlay[$hour] = array(
                "watch_hour" => "$hour",
                "value" => "0"
            );
        }
    }
    sort($currentPlay);
    $result[$key]['current_play'] = $currentPlay;
}

答案 1 :(得分:0)

试试这个:

$currebt_hours = array_column($result[0]['current_play'], 'watch_hour');
$arrdiff = array_diff($hoursArray, $currebt_hours);
foreach ($arrdiff as $value) {
  $entry = array();
  $entry[watch_hour]=$value;
  $entry[value]=0;
  $result[0][current_play][]=$entry;
}