3

この質問に関連して、まずこの質問から。私の問題は、私の友人が、データベースに挿入する必要がある約 300 個以上の配列を持っていることです。リンクした質問でわかるように、データベースの部分を取得します。その部分はダウンしています。ただし、すべての配列を取得してそれらをまとめて、配列に対して foreach を実行し、値が配列であるかどうかを確認できるようにする方法について疑問が生じます。 INSERT クエリのテーブル。

これは私の更新されたコードです:

        $colors['Colors_All'] = array("Black","Charcoal"); // Add unique indexes 
        $colors['Colors_Bright_All'] = array("Silver","White"); // Add unique indexes 

        $AllArrays = get_defined_vars(); // Get all defined vars
        $Arrays = array(); // Set a default array

        foreach ($AllArrays as $varName => $value) { // Run through all the variables set in the get_defined_vars
            if(is_array($value) && $varName == 'colors') { // If it is an array and if the array is colors[] then
                $Arrays = array_merge($Arrays, $value); // Merge those arrays into the new array
            }
        }

これで、すべてのデータにアクセスできるようになります。

4

1 に答える 1

2

どうぞ:

$colors['Colors_All']        = array("Black","Charcoal","Light_Gray","Silver","White","Gold","Bronze","Copper","Platinum","Navy","Royal_Blue","Dodger_Blue","Deep_Sky_Blue","Turquoise","Tiffany_Blue");
$colors['Colors_Bright_All'] = array("Silver","White","Gold","Royal_Blue","Dodger_Blue","Deep_Sky_Blue","Deep_Green","Forest_Green","Bright_Green","Violet");
$colors['Colors_Light_All']  = array("Light_Gray","Silver","White","Gold","Dodger_Blue","Deep_Sky_Blue","Light_Blue","Bright_Green","LightGreen","Light_Green");

// This will store the merged results of each array
$colorVars = array();

// Loop through all of the defined variables
foreach ($colors as $colorKey => $value) {
    // Add the results of this array to the main $colorVars array
    $colorVars = array_merge($colorVars, $value);
}
于 2013-04-20T15:01:15.530 に答える