0

2つのアレイを取得しています

Array
(
    [0] => 1990
    [1] => 1991
    [2] => 1992
    [3] => 1993
    [4] => 1994
    [5] => 1995
    [6] => 1996
    [7] => 1997
    [8] => 1998
    [9] => 1999
    [10] => 2000
    [11] => 2001
    [12] => 2002
    [13] => 2003
    [14] => 2004
    [15] => 2005
    [16] => 2006
    [17] => 2007
    [18] => 2008
    [19] => 2009
    [20] => 2010
    [21] => 2011
    [22] => 2012
    [23] => 2013
)

Array
(
    [0] => Array
        (
            [year] => 2011
            [conv_value] => 80
        )

    [1] => Array
        (
            [year] => 2012
            [conv_value] => 95
        )

    [2] => Array
        (
            [year] => 2004
            [conv_value] => 60
        )

    [3] => Array
        (
            [year] => 2000
            [conv_value] => 55
        )

)

1番目の配列の2番目の配列から年の一致を検索し、正確に長さのある新しい配列を作成する1番目の配列の検索は次のようになります。

年が見つかった場合は、新しいインデックスが設定されます。名前conv_valueには、2番目の配列インデックスconv_valueの値が含まれ、見つからなかった場合は、conv_valueは空になります。

このような

Array
(
    [0] => Array
        (
            [year] => 1990
            [conv_value] => 
        )

    [1] => Array
        (
            [year] => 1991
            [conv_value] => 
        )

    [2] => Array
        (
            [year] => 1992
            [conv_value] => 
        )

    [3] => Array
        (
            [year] => 1993
            [conv_value] => 
        )

    [4] => Array
        (
            [year] => 1995
            [conv_value] => 
        )

    [5] => Array
        (
            [year] => 1996
            [conv_value] => 
        )

    [6] => Array
        (
            [year] => 1997
            [conv_value] => 
        )

    [7] => Array
        (
            [year] => 1998
            [conv_value] => 
        )

    [8] => Array
        (
            [year] => 1999
            [conv_value] => 
        )

    [9] => Array
        (
            [year] => 2000
            [conv_value] => 55
        )

    [10] => Array
        (
            [year] => 2001
            [conv_value] => 
        )

    [11] => Array
        (
            [year] => 2002
            [conv_value] => 
        )

    [12] => Array
        (
            [year] => 2003
            [conv_value] => 
        )

    [13] => Array
        (
            [year] => 2004
            [conv_value] => 95
        )

    [14] => Array
        (
            [year] => 2005
            [conv_value] => 
        )

    [15] => Array
        (
            [year] => 2006
            [conv_value] => 
        )

    [16] => Array
        (
            [year] => 2007
            [conv_value] => 
        )

    [17] => Array
        (
            [year] => 2008
            [conv_value] => 
        )

    [18] => Array
        (
            [year] => 2009
            [conv_value] => 
        )

    [19] => Array
        (
            [year] => 2010
            [conv_value] => 
        )
    [20] => Array
        (
            [year] => 2011
            [conv_value] => 80
        )

    [21] => Array
        (
            [year] => 2012
            [conv_value] => 95
        )

    [22] => Array
        (
            [year] => 2013
            [conv_value] => 
        )

    )

スクリプトを作成しました

for ( $y = 0; $y < sizeof($cc); $y++ ){
    for ( $z = 0; $z < sizeof($years); $z++ ){
        if ($cc[$y]['year'] == $years[$z]) {
            echo 'Hay<br>';
        }else{
            echo 'Nahee hay<br>';
        }
    }
}

しかし、結果は得られませんでした。

どこでミスをしているの?

4

2 に答える 2

1

in_arrayを使用してみてください...

if (in_array($cc[$y]['year'], $years)) {
于 2012-06-26T12:13:21.550 に答える
0

それは長いコードですが、私はそれを書いて私の問題を解決しました:

<?php
                // all years                
                for ($i = 1990; $i <= (date('Y')+1); $i++){
                    $years[] = $i;
                }

                // db years
                $i=0;
                foreach ( $cur_conv as $key ){
                    $cc[$i]['year'] = $key['year'];
                    $cc[$i]['conv_value'] = $key['conv_value'];
                    $i++;
                }

                // make array which contain only years
                for($i=0; $i<sizeof($cc); $i++){
                    $db_years[] = $cc[$i]['year'];
                }

                // finally search local array in db array
                for($j=0; $j<count($years); $j++)
                {
                    $index = array_search($years[$j], $db_years);

                    if ( $index === FALSE )
                    {
                        $new_arr[$j]['year'] = $years[$j];
                        $new_arr[$j]['conv_value'] = '';

                    } else {

                        $new_arr[$j]['year'] = $cc[$index]['year'];
                        $new_arr[$j]['conv_value'] = $cc[$index]['conv_value'];
                    }
                }
                ?>
于 2012-06-27T04:53:54.263 に答える