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

  • Home
  • SEARCH
  • 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 7957555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:16:03+00:00 2026-06-04T04:16:03+00:00

I have a multi-dim array from POST [key:value key:value[array one {key1:value, key2:value}{key1:value, key2:value}][array two{key1:value,

  • 0

I have a multi-dim array from POST

[key:value key:value[array one {key1:value, key2:value}{key1:value, key2:value}][array two{key1:value, key2:value}...] key:value key:value]

I am producing xml entries with the nested arrays and identify them as follows:

foreach ($_POST as $key){
        if (is_array($key)){
               foreach ($key as $key2 => $value){
                   $array_type = substr($key2, 0,2);
                    if ($array_type == 'ec'){
                        $xml .= '<tns:PersonNamesInfoType>';

                    if ($key2 == 'ec_fname'){
                        $xml .= '<GivenName>'.$value.'</GivenName>';
                        //<MidInitial>MidInitial</MidInitial>
                        }
                        else if ($key2 == 'ec_lname'){
                            $xml .= '<FamilyName>'.$value.'</FamilyName>';
                            //</tns:PersonNamesInfoType>
                            }
                        else if ($key2 == 'ec_rel'){
                            $xml .= ' <Relationship>'.$value.' </Relationship>';
                            // <tns:PhoneInfoType>
                            //<tns:PhoneType></tns:PhoneType>
                            }
                        else if ($key2 == 'ec_ophone'){
                            $xml .= '<Number>'.$value.'</Number>';
                            }
                        else {
                            $xml .= '<IsPrimary>'.$value.'</IsPrimary>';
                            }
                            $xml .= '</tns:PersonNamesInfoType>';
                   }
                   else if($array_type == 've') {
                       $xml .= '<tns:AutoInfoType>';

                        foreach ($key as $key2 => $value){

                            if ($key2 == 'veh_year'){
                            $xml .= '<Year>'.$value.'</Year>';
                            }
                            else if ($key2 == 'veh_make'){
                            $xml .= '<Make>'.$value.'</Make>';
                            }
                            else if ($key2 == 'veh_model'){
                            $xml .= '<Model>'.$value.'</Model>';
                            }
                            else if ($key2 == 'veh_plate'){
                            $xml .= '<Plate>'.$value.'</Plate>';
                            }
                            else if ($key2 == 'veh_state'){
                            $xml .= '<tns:StateType>'.$value.'</tns:StateType>';
                            }
                            else {$xml .= '<IsPrimary>'.$value.'</IsPrimary>';}

                }
            $xml .= '</tns:AutoInfoType>';
                   }

            }
    }
}

This produces incorrect results

<tns:PersonNamesInfoType>
<GivenName>'.$value.'</GivenName>
</tns:PersonNamesInfoType>


<tns:PersonNamesInfoType>
<FamilyName>'.$value.'</FamilyName>
</tns:PersonNamesInfoType>

Instead of:

<tns:PersonNamesInfoType>
<GivenName>'.$value.'</GivenName>
<FamilyName>'.$value.'</FamilyName>
</tns:PersonNamesInfoType>

What is the better solution for segregating the data for each array based on the first key value coming across as

1[ec_fname]
1[veh_lname]
2[ec_fname]
2[veh_lname]
  • 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-06-04T04:16:04+00:00Added an answer on June 4, 2026 at 4:16 am

    This will do what you need, and it’s shorter, simpler, and more easily expandable:

    $personTags = array(
        "ec_fname" => "GivenName",
        "ec_lname" => "FamilyName",
        "ec_rel" => "Relationship",
        "ec_ophone" => "Number"
    );
    
    $vehicleTags = array(
        "veh_year" => "Year",
        "veh_make" => "Make",
        "veh_model" => "Model",
        "veh_plate" => "Plate",
        "veh_state" => "StateType"
    );
    
    $xml = "";
    foreach ($_POST as $key)
    {
        if (!is_array($key))
            continue;
    
        $personNamesInfoTypes = array();
        $autoInfoTypes = array();
    
        foreach ($key as $key2 => $value)
        {
            $array_type = substr($key2, 0, 2);
            if ($array_type == 'ec')
            {
                if (array_key_exists($key2, $personTags))
                    $tag = $personTags[$key2];
                else
                    $tag = "IsPrimary";
    
                $personNamesInfoTypes[] = " <{$tag}>{$value}</{$tag}>";
            }
            else if ($array_type == 've')
            {
                if (array_key_exists($key2, $vehicleTags))
                    $tag = $vehicleTags[$key2];
                else
                    $tag = "IsPrimary";
    
                $autoInfoTypes[] = "    <{$tag}>{$value}</{$tag}>";
            }
        }
    
        if (!empty($personNamesInfoTypes))
            $xml .= "<tns:PersonNamesInfoType>\n". implode("\n", $personNamesInfoTypes) ."\n</tns:PersonNamesInfoTypes>\n";
        if (!empty($autoInfoTypes))
            $xml .= "<tns:AutoInfoType>\n". implode("\n", $autoInfoTypes) ."\n</tns:AutoInfoTypes>\n";
    }
    

    It looks like you had your loops set up wrong (there was a foreach loop in the vehicle section, but not one in the person section) and you were creating a new parent node every time a child node was created.

    With my solution, instead of adding the nodes directly to the string on each loop iteration, they will instead be appended to an array, and then at the end of that $_POST variable, the two arrays will be dumped into the correct parent node all at once.

    If you have to add another possible tag to either the person section or the vehicle section, all you need to do is add an entry to the $personTags or $vehicleTags array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-dimensional array, which basically consists of one sub-array for each year.
I have a multi-dimensional array, no problem. How do I interrogator one of the
I have a multi-dim string array something like this:- string[,] names = new string[2,
Let's suppose I have below nested/multi-dim array: array( 'World'=>array( 'Asia'=>array( 'Japan'=>array( 'City'=>'Tokyo' ) )
It is possible to put an array into a multi dim array? I have
So I have a multi-dim array. I'm defining a few array keys with boolean
I have a multi view application with individual UIViewControllers and xibs. I have one
I have a multi dimensional array. The only actual values (other than other arrays)
I have an multi-dimensional array that I want to send to a PHP script
i have multi dimensional array like below, Array ( [14289] => Array ( [0]

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.