Flatten a multidimensional array without recursion

Discussion in 'PHP' started by makamo66, Dec 22, 2012.

  1. #1
    This is the meal planner app I'm still working on. I've gotten a lot of useful help so far. You can see the view source at http://maureenmoore.com/momp_tests/122212.html


    I am using the following in my process.php (which the jquery form gets submitted to)


    $stack = array();
    
    
    foreach ($_REQUEST as $key => $value) {
    			array_push($stack,$value);
              }
    $stack = array_values($stack);
    
    
    print_r($stack);
    
    
    $comma_separated = implode(",", $stack);
    
    
    
    
    echo "\n\n" . $comma_separated . "\n\n";
    
    Code (markup):
    When I click the submit button at localhost it shows an error for array to string conversion because it's a multidimensional array. How can I get rid of the first null key without resorting to a recursive function? I don't see the error any more now that I've uploaded it to my hosting account but it's not printing a comma-separated list because it's a multidimensional array with a key of null.
     
    makamo66, Dec 22, 2012 IP
  2. Deluxious

    Deluxious Greenhorn

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #2
    What does the print_r of $_REQUEST look like?

    I think, though I'm not sure at all, that you might need to select the first element in the $_REQUEST array to get the results you want.

    So instead of foreach ($_REQUEST....

    you'd use foreach ($_REQUEST[0]...

    That's my guess at this point. Hope it helps.
     
    Deluxious, Dec 22, 2012 IP
  3. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    When I do that I get the error "undefined offset 0"
     
    makamo66, Dec 22, 2012 IP
  4. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    Thanks anyway Deluxious
     
    makamo66, Dec 22, 2012 IP
  5. Deluxious

    Deluxious Greenhorn

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #5
    Can you post the print_r of $_REQUEST?
     
    Deluxious, Dec 22, 2012 IP
  6. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    makamo66, Dec 22, 2012 IP
  7. Deluxious

    Deluxious Greenhorn

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #7
    If I'm understanding correctly, based on your code the print_r output is showing the $stack variable - it would be easier if we could see the original array that's being passed into your loop. Let me know if I'm missing something.
     
    Deluxious, Dec 22, 2012 IP
  8. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #8
    Using $_REQUEST['data'] as suggested by jeroen at stack overflow solved the problem.
     
    makamo66, Dec 22, 2012 IP