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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting data out of $_POST using foreach()?



    I am beyond confused.

    <?php

    foreach($_POST as $key =>$value){
    foreach($value as $k =>$val){
    $$k=$k;
    $$val = $val;
    }
    }
    echo $$val."Likes" $$val;
    ?>

    My little test form has fields 'name' and 'stuff' I've typed in Page in the 'name' field and Cookies in the 'stuff' field.

    I'm expectiong to see
    Page Likes Cookies.

    Instead I get an error "Invalid arrguement supplied in foreach() line 4. This error is displayed 3 times followed by "Likes"

    So this is what I thought I knew...

    POST is a multidimentional array, that contains the field names of the form as one index, and the value of those fields as a element inthe array

    That is

    $_POST[SUBMIT][name]=Page

    Therefore

    foreach ($_POST as $key =>$value)

    should get me

    $key = submit
    $value[name] =Page

    Obviously I am wrong....somewhere. PHP doesn't think $value is another array.

    Help!!!

    Page

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    value isnt an array its the value of $key

    PHP Code:
    <?php

    foreach($_POST as $key =>$value){
    $
    $key=$value
    }
    echo 
    $name."Likes" $stuff;
    ?>
    or
    PHP Code:
    extract($_POST);
    echo 
    $name."Likes" $stuff
    PHP Code:
    echo $$val."Likes" $$val
    just doesnt make any sense at all if you were expecting "name likes stuff", $$val cant simultaneously = name and stuff

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yep yep, other than using an extract function the best bet would be to create a recursive function for this. I say this as $_POST can have as many grandchildren as you want it to have, and in order to correctly extract the values you would need to recursively call them.

    However, (theres always a however ), this may not be practical for you either. I myself wouldn't ever use a recursive function on an array such as this, simply because I would later evaluate what could possibly be an array.

    The biggest part is your call to a variable variable, you might have a misconseption of how that one works. Though $$k can equal $k, this is doubtful what you are looking for.
    PHP Code:
    $varname 'Hello';
    $
    $varname 'World';

    echo 
    $Hello;

    // Ouput: 'World'; 
    Thats how the variable variables work, it assigns the value of the original variable as the name of the new variable.
    Variable variables, are an extremely handy thing in php.

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I'd watch out with those recursive functions. In PHP you can have recursive arrays.
    I'd also watch out for variable variables as these produce hard to find errors.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by marek_mar
    I'd watch out with those recursive functions. In PHP you can have recursive arrays.
    I'd also watch out for variable variables as these produce hard to find errors.
    Good point about the errors, you gotta stay on top of whats happening in order to avoid them.
    As with the recursive array, can you explain? Offhand I can't quite put together 'recursive' and 'array' to provide myself with any mental feedback, lol. Chances are I've used whatever it is, but I just can't quite picture it. Can you give me a refreshing course?

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    $GLOBALS is a recursive array. It's an array that has a reference to itself...
    PHP Code:
    <?php
    $a 
    = array(1);
    $a[] = &$a;
    print_r($a); 
    ?>
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #7
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by NancyJ
    value isnt an array its the value of $key

    PHP Code:
    <?php

    foreach($_POST as $key =>$value){
    $
    $key=$value
    }
    echo 
    $name."Likes" $stuff;
    ?>
    Okay, this is what I needed to see. Not sure where I got the bright idea that $value was another array. Too many late nights with the PC ?

    PHP Code:
    echo $$val."Likes" $$val
    just doesnt make any sense at all if you were expecting "name likes stuff", $$val cant simultaneously = name and stuff
    Well it made perfect sense at 3a.m. $$val should have a different value each time it was called cause I was iterating through an array! DOH! LOL. Anyway, I've got it now.

    I'm sure I'll have more Green Horn Qs that are only slightly less silly than this one you've answered!

    Thanks
    Page

  • #8
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by marek_mar
    I'd watch out with those recursive functions. In PHP you can have recursive arrays.
    I'd also watch out for variable variables as these produce hard to find errors.
    Can you elaborate or give an example?

    Thanks
    Page

  • #9
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I've given two examples of recursive arrays in my previous post. The second point should be the result of the experiance you have gained by asking this question.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

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