0

First I declare a variable that I want to use in multiple arrays through the code. This is a snippet of it where it should work, but it doesn't:

The variable:

$test = '<div id="test"> dit is een test </div>'; 

The array:

$sections[] = array( 
                'title' => $test, 
                'icon' => '/img/icons/home.png' 
                );  

The title is always empty :-/

Thanks !!

5
  • Can you provide more of your script? The code you are providing should work. Commented Jul 30, 2012 at 12:01
  • There is nothing wrong with your code. $sections[0]['title'] should display it fine (assuming it's the first element in the array) Commented Jul 30, 2012 at 12:01
  • yes thats possible, did you try doing echo $sections[0]['title']; to get title ... that should give you the title Commented Jul 30, 2012 at 12:01
  • 1
    The code is fine. Likely the array declaration is in a place that cannot read $test (i.e. a scoping issue). Either way, we'll need to see more code. Commented Jul 30, 2012 at 12:01
  • did you var_dump($sections) to see values stored in your array?? Commented Jul 30, 2012 at 12:10

2 Answers 2

1

Correct declaration is

$test = '<div id="test"> dit is een test </div>'; 
$sections = array( 
                'title' => $test, 
                'icon' => '/img/icons/home.png' 
                );  
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the fast response everybody. In fact, tha code is a snippet from a wordpress framework. It is one menu item in the admin panel. The above code, doesn't work, I get an error "Invalid argument supplied for foreach() in xxx/options.php" so it is more complicated than I thought.
I have found it, it was the first line of the code above that was the problem: $sections = array(); Thanks everyone !
0

Try with this code :

$test = '<div id="test"> dit is een test </div>';

$sections['title'] = $test;
$sections['icon'] = '/img/icons/home.png';

echo $sections['title'];

Comments

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.