Page 1 of 1

php array ignoring values

Posted: Wed Feb 03, 2010 8:32 am
by kewpe20
I have created the following array.
$a_random = array( '<random-all>', '<random>', '<random-all-or>', '<random-1>',
'<random-all-1>','<random-all-2>','<random-all-3>','<random-all-4>','<random-all-5>','<random-all-6>',
'<random-all-7>','<random-all-8>','<random-all-9>','<random-all-10>',
'<random-or-1>','<random-or-2>','<random-or-3>','<random-or-4>','<random-or-5>','<random-or-6>',
'<random-or-7>','<random-or-8>','<random-or-9>','<random-or-10>'
);

However, when I print_r the array none of the values have been inserted into the array.
It has to do with the '<' '>' characters.
I need those specific values with the '<' '>' to be in the array. How can I force the array to populate them?

Re: php array ignoring values

Posted: Wed Feb 03, 2010 10:43 am
by AbraCadaver
It is creating the array with < and > but your browser interprets them as HTML tags just like <body> etc, so it doesn't display them. Do a view source on your browser and you will see them. You need to convert them to the HTML entities for the browser to display them. You could probably do this:

Code: Select all

print_r(array_map('htmlentities', $a_random));

Re: php array ignoring values

Posted: Wed Feb 03, 2010 1:27 pm
by kewpe20
That makes sense but it is making it very difficult to trouble shoot an issue I'm having.
The code that follows is behaving like the array is empty. That's why I assumed the array was really empty.

Re: php array ignoring values

Posted: Wed Feb 03, 2010 3:05 pm
by AbraCadaver
kewpe20 wrote:That makes sense but it is making it very difficult to trouble shoot an issue I'm having.
The code that follows is behaving like the array is empty. That's why I assumed the array was really empty.
Make your own function to do this and also save typing. I would also wrap it in pre tags so that you get it nicely formatted in the browser(tabs, newlines, etc.):

Code: Select all

function pre($var) {
    echo '<pre>';
    print_r(array_map('htmlentities', $var));
    echo '</pre>';
}