3

First off this question in in relation to this question. My issue is that a friend of mine has upwards of around 300 or so arrays that she needs to insert into the database. I get the database part as you notice in the question I linked I have that part down. My question however arises on just how exactly I am supposed to get all the arrays and bring them together so that I could could do a foreach on the arrays and check if a value is an array, if it is then use the arrays name as the table in the INSERT query.

This is my updated code :

        $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
            }
        }

This will now give me access to all the data.

9
  • 1
    Could you show some snippets of what you have so far? Just so we can see how the code you're working with works Commented Apr 20, 2013 at 14:56
  • Why doesn't your friend store those hard coded arrays for once and forever in the DB? Then they won't need to loop through all the arrays to check for a value before inserting it... Commented Apr 20, 2013 at 14:57
  • That is what we are trying to do with the arrays we want to store them in the database. Its just going kinda unconventional to make 300 or tables (a table for each array) and then insert 20 or 30 values per array... You know what I mean? Commented Apr 20, 2013 at 14:59
  • But you're going to do it once. Instead of looping through each array each time the code is executed... Commented Apr 20, 2013 at 15:00
  • No, the script I posted above is only used for back end "maintenance" to update the colors of her store. She will only need to do this probably 4 or 5 times total Commented Apr 20, 2013 at 15:01

1 Answer 1

2

Here you go:

$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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Brandon I won't be able to test this / accept this answer until I get in contact with her. So don't think I'm just ignoring this answering.
@RixhersAjazi You changed the code from your previous post. I've updated my example though, so it should work. Please accept my answer.
This part of the puzzle is completed so in all sense your answer is the correct answer. The next part is here though : stackoverflow.com/questions/16116730/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.