View Full Version : echo out non "0" values from array

11-05-2012, 07:13 PM
I have a large $_POST array and would like to echo out all the keys and values where the value is not "0". I have tried :

foreach ($_POST as $key => $value) {
if ($_POST{$value} !==" ");
print $key . " " . $value . "</br>";

also with
{$value} !==0
without success, I still get all 153 strings and values. I am sure this must be easy, but I am not yet experienced enough to crack it.
Clues greatly appreciated!

11-05-2012, 07:50 PM
!== 0 indicates that its an integer and only an integer. When it comes from a form, it is always a string. Use != instead. $value is correct, not $_POST{$value} (also don't use these curly accessors; I'll take a look to see if I can find it, but I could swear they were indicated as deprecated).

On a side note, if you don't care about any of the other input, anything that is false equivalent can be removed easily with a filter.

$aNonFalse = array_filter($_POST);

All false equivalent values are not present in $aNonFalse.

11-07-2012, 09:57 PM
I did not care that I lost the 0 data from the array, so tried the filter and it worked a treat.
As for the {} I think I am learning from an old book - must look for a more up to date title.
Thanks - little by little, I'm learning.

11-07-2012, 10:45 PM
You bet. I mean other values as well, so if any values equate to false (that is: "", '0', 0, false, null, array()), then they will also be removed. Sounds to me that is how you'd like it to go though.
Books go out of date fast on languages like PHP. Even a PHP version 5.0 will be obsolescent with 5.4 available.