0

So what I am doing is creating a function that will pass in what array to append to another array. Here is what I am "trying" to do:

Global $shirts;
Global $prices;

$shirts = array(
        'Option1' => array(),
        'Option2' => array(),
    );

$prices = array(
        'product1' => array(
                        'bronze' => 1, 
                        'silver' => 2, 
                        'gold' => 3,
                       ),
        'product2' => array(
                        'bronze' => 4, 
                        'silver' => 5, 
                        'gold' => 6,
                       ),  
    );


function shirts($shirts_model) {

        global $shirts;
        global $prices;

        foreach ($shirts => $shirt) {

            $result = array_merge($shirt, $prices[$shirt_model]);

        }
         print_r($result);  

    }

shirts('product2');

so now the $shirts array would now look like:

$shirts = array(
            'Option1' => array(
                            'bronze' => 4, 
                            'silver' => 5, 
                            'gold' => 6,
                           ),
            'Option2' => array(
                            'bronze' => 4, 
                            'silver' => 5, 
                            'gold' => 6,
                           ),
        );

with the "product2" array. Basically now I could call the shirts(); function and pass in any option to append that options array to the shirts array. But this approach is not working? I am getting a white screen and I dont think this is working.

Hope that made sense.

10
  • 1
    And your exact question is what? Commented Feb 6, 2016 at 21:25
  • Y echo $shirts??? Why not print_r($result); ?? Commented Feb 6, 2016 at 21:27
  • Do u want to return merged array in result? Commented Feb 6, 2016 at 21:30
  • Sorry, changed the question. But yes I want to just make sure that the shirts array is now listing with the whatever option array I choose added. Commented Feb 6, 2016 at 21:31
  • can you explain your expected results Commented Feb 6, 2016 at 21:34

2 Answers 2

1

As I mentioned in comments now I am convert it into answer.

First of all you need to fix your foreach() function as:

foreach($array as $value)

Modified code:

function shirts($shirts_model) 
{ 
    global $shirts, $prices; 

    foreach ($shirts as $key => $shirt) 
    { 
        $result[$key] = $prices[$shirts_model];
    } 
    return $result;
}

Now calling it:

$record = shirts('product2');
echo "<pre>";
print_r($record);

Error Reporting:

Add error_reporting ON in your code this will help you to find out the issue.

error_reporting();
Sign up to request clarification or add additional context in comments.

7 Comments

Hmmmm this makes sense but returns nothing. I am not getting the white screen of death, just nothing.
@packy don't know but use global not Global
Thanks. Still not printing anything but I will poke around with it
@packy r u using my code???? I have updated it.. Was using shirt_model this should be shirts_model
@packy ahan glad to know your problem is solve don't forgot to appreciate. Happy coding
|
1
function shirts($shirt_model) {
    global $shirts;
    global $prices;

    // create an empty array for the results
    $results = array();

    // loop so that you have both the key, value, and we'll only use the key
    foreach ($shirts as $option => $shirt) {
        // just add the prices to the results
        $results[$option] = $prices[$shirt_model]);
    }
    return $results;  
}

2 Comments

My friend u have an small issue this is shirts_model not shirt_model
Glad to know my friend u earn my vote I suggest also add some story of foreach()

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.