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 7597229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:06:28+00:00 2026-05-30T22:06:28+00:00

I have this type of array coming back: Array ( [data(0)] => somevalue [data(1)]

  • 0

I have this type of array coming back:

Array
(
    [data(0)] => somevalue
    [data(1)] => somevalue
    [data(2)] => somevalue
)

And I need converted to this:

Array (
    [data] => Array (somevalue
        [0] => somevalue
        [1] => somevalue
        [2] => somevalue
    )
)

Keep in mind there can be other keys, so simply running a for loop on the count and feeding each value in is not an option.

I thought I would really simplify things by posting the simple example above, but I guess that didn’t tell enough of the story.

The data is a string back from PayPal. PayPal has a function called deformatNVP() which does the initial job of putting this in to an array, however it’s not an array that PHP can handle. It can look like this:

Array(
    [responseEnvelope.ack] => somevalue
    [responseEnvelope.timestamp] => somevalue
    [responseEnvelope.build] => somevalue
    [responseEnvelope.correlationId] => somevalue
)

It can also have something like:

Array(
    [personalData(0)] => somevalue
    [personalData(1)] => somevalue
    [personalData(2)] => somevalue
)

That’s completely ugly, so the first step below fixes the dotted arrays, but I am left with these keys with parenthesis to deal with.

$data = array();
foreach ($array as $key => $value) {

    $subkey = "";
    $_keys = explode('.', $key);

    if(count($_keys) > 1){
        $key = $_keys[0];
        $subkey = $_keys[1];
    }

    if(preg_match("/([0-9]+)/", $key, $matches)){
        $key = preg_replace("/\([0-9]+\)/", "", $key);
        $data[$key][$matches[0]][$subkey] = $value;
    }
    elseif($subkey)
        $data[$key][$subkey] = $value;
    else
        $data[$key] = $value;

}
return $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-30T22:06:29+00:00Added an answer on May 30, 2026 at 10:06 pm

    Sometimes there are keys that are dotted like: key.subkey

    If something . can appear, sometimes other things can appear too. This specification should be clear enough before developing any algorithm to parse it.

    So far It can be done by looping over each element and splitting information using preg_split function. Following code put the resultant array in $result variable.

    $result = array();
    foreach ($array as $key => $value) {
        list($text, $subkey) = preg_split('/\W+/', "$key:END", 3, PREG_SPLIT_NO_EMPTY);
        if($subkey=='END'){
        $result[$text] = $value; continue;
        }else{
        $result[$text][$subkey] = $value;
        }
    }
    

    See http://ideone.com/CJHHZ

    Remember it just splits the string with non word characters (\W). \W matches any characters other than a-z, A-Z, 0-9 and _.

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

Sidebar

Related Questions

I have this Javascript data: [{id:123,type:test},{id:154,type:another}] How would you transform that into something so
I have a data tables with this type of setup. Table A AID AName
I have this $_POST array coming from a paypal transaction Array ( [address_city] =>
i have this simple type from an external webservice: <xsd:element name=card_number maxOccurs=1 minOccurs=1> <xsd:simpleType>
Lets say have this immutable record type: public class Record { public Record(int x,
I have this mysql table called comments which looks like this: commentID parentID type
I have this quiz application where I match what people type with the right
I have this code here, which is intended to allow any type of arguments:
In mysql database i have this column called: Name: Date Type: datetime I have
I have this input text: <html><head><meta http-equiv=content-type content=text/html; charset=utf-8></head><body><table cellspacing=0 cellpadding=0 border=0 align=center width=603>

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.