将正确的多维数组值传递给函数(没有重复)

时间:2017-11-27 16:30:27

标签: php arrays function multidimensional-array foreach

我试图通过在oop编程中正确使用循环和函数(方法)来手动和重复地做事;但是,在将多维数组传递给负责数据库操作的必要抽象函数(方法)时,我遇到了一个关于多维数组的主要绊脚石。

任何帮助都非常受欢迎,这将使我能够摆脱这个磕磕绊绊的障碍,我一直试图推开几天,但没有进展,这是出于真正的挫折和痛苦,我是在这里寻求帮助。

下面是代码,为了简单起见,我尽可能地缩短了(可以通过复制和粘贴在本地轻松测试):

// array with table properties and form values - start
$form_fields_arr = [
    'group' => [
        'anime' => [ // genre
            'table_prop' => [ // for update query - table properties
                'table_name'    => 'anime_tbl',
                'account_id'    => 2,
                'visible'       => 'yes'
            ],

            'form_data' => [ // for update query - form values
                '2'             => 'Attack on Titan',
                '4'             => 'RWBY',
                '6'             => 'Rurouni Kenshin',
                '8'             => 'A Silent Voice'
            ]
        ],

        'movie' => [ // genre
            'table_prop' => [ // for update query - table properties
                'table_name'    => 'movie_tbl',
                'account_id'    => 4,
                'visible'       => 'yes'
            ],

            'form_data' => [ // for update query - form values
                '1'             => 'Queen of Katwe',
                '3'             => 'Forest Gump',
                '5'             => 'War Horse',
                '7'             => 'The Fault in our Stars'
            ]
        ]
    ]
]; // ... end


// loop through multidimensional array and pass values to function - start
foreach ($form_fields_arr['group'] as $frm_key_1 => $frm_val_1) { // 2d array
    foreach ($frm_val_1 as $frm_key_2 => $frm_val_2) { // 1d array

        if (strcasecmp($frm_key_1, $frm_key_1) === 0) { // group by genre

            foreach ($frm_val_2 as $frm_key_3 => $frm_val_3) { // 1d array

                if (strcasecmp($frm_key_2, 'form_data') === 0) {
                    $title =  $form_fields_arr['group'][$frm_key_1]['form_data'][$frm_key_3]; // anime/movie title
                }

                if (isset($frm_val_2['table_name']) &&
                    isset($frm_val_2['account_id']) &&
                    isset($frm_val_2['visible']) &&
                    isset($title)
                ) {
                    dbUpdate(
                        $frm_val_2['table_name'],
                        $frm_val_2['account_id'],
                        $frm_val_2['visible'],
                        $title
                    );
                }

            } // 1d array

        } // if block

    } // 1d array
} // 2d array
// ... end


// function that receives passed values - start
function dbUpdate($table_name, $account_id, $title_col, $form_value) {
    $test_val_arr = [$table_name, $account_id, $title_col, $form_value];

    return print_r($test_val_arr);
} // ... end

以上代码输出:

// array values passed to and returned from function
Array (
    [0] = movie_tbl
    [1] = 4
    [2] = yes
    [3] = A Silent Voice
)

Array (
    [0] = movie_tbl
    [1] = 4
    [2] = yes
    [3] = A Silent Voice
)

Array (
    [0] = movie_tbl
    [1] = 4
    [2] = yes
    [3] = A Silent Voice
)

Array (
    [0] = movie_tbl
    [1] = 4
    [2] = yes
    [3] = A Silent Voice
)

但我想要实现的理想结果是:

// for anime genre - array values passed to and returned from function
Array (
    [0] = anime_tbl
    [1] = 2
    [2] = yes
    [3] = Attack on Titan
)

Array (
    [0] = anime_tbl
    [1] = 2
    [2] = yes
    [3] = RWBY
)

Array (
    [0] = anime_tbl
    [1] = 2
    [2] = yes
    [3] = Rurouni Kenshin
)

Array (
    [0] = anime_tbl
    [1] = 2
    [2] = yes
    [3] = A Silent Voice
)


// for movie genre - array values passed to and returned from function
Array (
    [0] = movies_tbl
    [1] = 4
    [2] = yes
    [3] = Queen of Katwe
)

Array (
    [0] = movies_tbl
    [1] = 4
    [2] = yes
    [3] = Forest Gump
)

Array (
    [0] = movies_tbl
    [1] = 4
    [2] = yes
    [3] = War Horse
)

Array (
    [0] = movies_tbl
    [1] = 4
    [2] = yes
    [3] = The Fault in our Stars
)
所以,一切都在皇室失败,我花了大约一个星期试图解决这个问题,告诉自己这很简单,我真的不应该被困在这里,出于绝望,我决定回到我的重复方式,尝试了以下内容:

// new array without table properties - start
$new_array = [];
$new_array['group']['anime'] = $form_fields_arr['group']['anime']['form_data'];
$new_array['group']['movie'] = $form_fields_arr['group']['movie']['form_data']; // ... end

// loop through multidimensional array and pass values to function - start
foreach ($new_array['group'] as $key_1 => $val_1) { // 2d array
    foreach ($val_1 as $key_2 => $val_2) { // 1d array

        if (strcasecmp($key_1, $key_1) === 0) {
            dbUpdate('anime_tbl', 2, 'yes', $val_2);
            dbUpdate('movie_tbl', 4, 'yes', $val_2);
        } // if block

    } // 1d array
} // 2d array
// ... end

但结果仍然非常不受欢迎。一切都工作正常,直到我开始使用多维数组,只是因为我意识到利用多维数组可以帮助我在其他方面大大缩短我的代码。但我被困在这里,如果我不能让它发挥作用,将不得不进一步向上并撤消相当多的变化。我正在寻求任何善良灵魂的帮助。请帮帮我一个人!任何人!

我在这里很乐观并且假设如果我在任何情况下得到一些帮助来解决上述问题,有人也可以教我如何循环一个类似下面的阵列结构,同时获得所需的结果重复(我已经尝试但确实失败了):

// array with table properties and form values - start
$form_fields_arr = [
    'table_prop' => [ // table properties group
        'anime' => [ // for update query - table properties
            'table_name'    => 'anime_tbl',
            'account_id'    => 2,
            'visible'       => 'yes'
        ],

        'movie' => [ // for update query - table properties
            'table_name'    => 'movie_tbl',
            'account_id'    => 4,
            'visible'       => 'yes'
        ]
    ],

    'form_data' => [ // for update query - form values
        'anime' => [ // genre
            '2'             => 'Attack on Titan',
            '4'             => 'RWBY',
            '6'             => 'Rurouni Kenshin',
            '8'             => 'A Silent Voice'
        ],

        'movie' => [ // genre
            '1'             => 'Queen of Katwe',
            '3'             => 'Forest Gump',
            '5'             => 'War Horse',
            '7'             => 'The Fault in our Stars'
        ]
    ]
]; // ... end

0 个答案:

没有答案