-1

I'm trying to get my head around arrays. This a is a followup to previous question. PHP How can I pass values from one array to another?

So, I've figured out how to pass data from one array to another thanks to: Explosion Pills but now i'm trying to take the array and pass it through to a new function and then create the new array within the new function. I'm guessing this is easier than it appears to be to me right now.

Here is the array:

$theSetup = array(

        'option1' => array('title'=>'my_title','label'=>'The Big Title','val'=>'NoneyoBizness'),
        'option2' => array('title'=>'my_watch,'label'=>'Big Watch','val'=>'Seiko'),
        'option3' => array('title'=>'facebook','label'=>'Facebook Page','val'=>'http://facebook.com/bigwatch'),

);

Here is my test function:

function testArray($theSetup) {
    foreach($theSetup as $v) {
        $defaultOptions[$v['title']] = $v['val'];
    }
    foreach($defaultOptions as $k=>$v) {
        echo $k .' : '.$v .'<br>';
    }
} 
testArray($theSetup);

After running the code I get an error: undefined variable theSetup

5
  • 2
    can you post the full code?, so we know the scope where you are creating $theSetup Commented Mar 29, 2013 at 18:09
  • 1
    Where have you defined $theSetup? If it is in a different scope/context PHP won't be able to find it. Commented Mar 29, 2013 at 18:09
  • 1
    This is a simple debugging problem. Commented Mar 29, 2013 at 18:12
  • Did you define the variable before or after you called the function? Commented Mar 29, 2013 at 18:19
  • you guys were right. it was a scope issue. i was trying to call it from outside of the original function. However, am going to edit the question because i need to figure out how to pass the array from one function to another Commented Mar 29, 2013 at 18:35

1 Answer 1

1

You need to use a text editor that shows you when you have a mistake in your PHP code.

This works fine:

<?php
    $theSetup = array(

            'option1' => array('title'=>'my_title','label'=>'The Big Title','val'=>'NoneyoBizness'),
            'option2' => array('title'=>'my_watch','label'=>'Big Watch','val'=>'Seiko'),
            'option3' => array('title'=>'facebook','label'=>'Facebook Page','val'=>'http://facebook.com/bigwatch'),

    );

    function testArray($theSetup) {
        foreach($theSetup as $v) {
            $defaultOptions[$v['title']] = $v['val'];
        }
        foreach($defaultOptions as $k=>$v) {
            echo $k .' : '.$v .'<br>';
        }
    } 
    testArray($theSetup);
?>

This does not:

<?php
    $theSetup = array(

            'option1' => array('title'=>'my_title','label'=>'The Big Title','val'=>'NoneyoBizness'),
            'option2' => array('title'=>'my_watch,'label'=>'Big Watch','val'=>'Seiko'),
            'option3' => array('title'=>'facebook','label'=>'Facebook Page','val'=>'http://facebook.com/bigwatch'),

    );

    function testArray($theSetup) {
        foreach($theSetup as $v) {
            $defaultOptions[$v['title']] = $v['val'];
        }
        foreach($defaultOptions as $k=>$v) {
            echo $k .' : '.$v .'<br>';
        }
    } 
    testArray($theSetup);

?>

The difference is I closed your quote mark after "my_watch."

Output

This works fine: my_title : NoneyoBizness
my_watch : Seiko
facebook : http://facebook.com/bigwatch
Sign up to request clarification or add additional context in comments.

1 Comment

Adobe DreamWeaver, NetBeans, Aptana, Komodo, Notepad++, and GUI versions of Emacs all have code syntax highlighting that will tell you when something unexpected happens in the code. Just like in your example on this page, it would show you that something wasn't quoted properly.

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.