Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1087121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:53:55+00:00 2026-05-16T22:53:55+00:00

I have a multidimensional array of form data thats an been unserialized from YAML.

  • 0

I have a multidimensional array of form data thats an been unserialized from YAML. As such it looks something like this:

Array(
  'name' => 'Somone',
  'email' => 'someone@none.local',
  'billing' => Array(
     'address_1' => '1234 Somewhere'
     'address_2' => NULL,
     'city' => 'Somewhere',
     'state' => 'ST'
     'country' => 'CO'
     'postal_code' => '12345'
   ),
  'shipping' => Array(
     'address_1' => '1234 Somewhere'
     'address_2' => NULL,
     'city' => 'Somewhere',
     'state' => 'ST'
     'country' => 'CO'
     'postal_code' => '12345'
  )
);

What i need to do is to flatten this so I can output some CSV, so it should look something like this:

Array(
  'name' => 'Somone',
  'email' => 'someone@none.local',
  'billing_address_1' => '1234 Somewhere'
  'billing_address_2' => NULL,
  'billing_city' => 'Somewhere',
  'billing_state' => 'ST'
  'billing_country' => 'CO'
  'billing_postal_code' => '12345'
  'shipping_address_1' => '1234 Somewhere'
  'shipping_address_2' => NULL,
  'shipping_city' => 'Somewhere',
  'shipping_state' => 'ST'
  'shipping_country' => 'CO'
  'shipping_postal_code' => '12345'
);

I will never know for sure how deep the array/hash is – it coul be only 2 levels as shown here or it could be 5.

Also this is in Symfony 1.4 so the sfForm with all of its luxuries is available if needed. I’m thinking there should be a sensible way to do this using the widget schema(s) and widgets. However, i would like to avoid binding the data back to the form if possible. This isn’t part of the actual form submission process but is completely separate in an action for admins to download sets of the submitted data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:53:55+00:00Added an answer on May 16, 2026 at 10:53 pm

    Just a quick-hack but it works pretty good:

    function array_flatten($array, $prefix = '') {
        $newArray = array();
        foreach($array as $key => $value) {
            if(is_array($value)) {
                    $newArray = array_merge($newArray, array_flatten($value, $key));
            }
            else {
                    $index = empty($prefix) ? $key : $prefix.'_'.$key;
                    $newArray[$index] = $value;
                 }
         }
         return $newArray;
    }
    

    Test:

    $a = array(
            "a" => "b",
            "ca" => array(
                "de" => "ef",
                "ef" => "gd"
            )
    );
    
    var_dump(array_flatten($a));
    
    // Output:
    /*
    
    array(3) {
      ["a"]=>
      string(1) "b"
      ["ca_de"]=>
      string(2) "ef"
      ["ca_ef"]=>
      string(2) "gd"
    }
    
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.