Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2012
    Location
    France
    Posts
    60
    Thanks
    15
    Thanked 0 Times in 0 Posts

    echo out non "0" values from array

    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 :
    PHP Code:
    <?php
    foreach ($_POST as $key => $value) {
    if (
    $_POST{$value} !==" ");
    print 
    $key " " $value "</br>";
    }
    ?>
    also with
    PHP Code:
    {$value} !==
    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!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    !== 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.
    PHP Code:
    $aNonFalse array_filter($_POST); 
    All false equivalent values are not present in $aNonFalse.

  • Users who have thanked Fou-Lu for this post:

    oddshoes (11-07-2012)

  • #3
    New Coder
    Join Date
    Aug 2012
    Location
    France
    Posts
    60
    Thanks
    15
    Thanked 0 Times in 0 Posts
    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •