3

I am trying to convert the key values of an array into a multi-dimensional array. The key values look to be multidimensional, but are just text. I have tried exploding the the string and creating a new array, but I feel there must be something simpler than just that.

Example Below:

Array (
[Template[URL]] => http://www.asdasdda.com
[Template[UPC]] => 5484548546314
[Field[value]] =>  Test Example
[Field[answer]] => 20 )

All help is very much appricated. :)

UPDATE: This is the exact output of the data before I run json_decode on the data.

{"Template[URL]":"http://www.asdasdda.com","Template[UPC]":"5484548546314","Field[value]":"Test Example","Field[answer]":"20"}
8
  • Can't fix it where the array is being created? Commented Apr 22, 2015 at 16:24
  • this surely shall work out if yr representation Template[URL] held a string value. What is this Template[URL]? It is not a variable.. if Template was an array, then refer it appropriate - like: $Template['URL'] Commented Apr 22, 2015 at 16:25
  • @Devon This comes in from an API from another department in my company, and the person who built this is impossibly hard to work with, so I rather just try and convert it. The funny thing is he sent it through as json, but didnt encode the multidimensional array, just flattened it. Commented Apr 22, 2015 at 16:27
  • @animaacija the key value of the arrays is formatted 'Template[URL]' not Template['URL']. That's the problem I am currently having. Commented Apr 22, 2015 at 16:30
  • maybe it is not an array but stdClass or smth? Where this comes from? What is URL then? Constant? if so , it has to have value in it. Commented Apr 22, 2015 at 16:34

2 Answers 2

4

Fiddled around for a bit, and I think I got it. I don't think there is any simpler way:

foreach ($array as $key=>$value) {
    preg_match("/\[(.+)\]/",$key,$match);
    $newKey = preg_replace("/\[.+\]/","",$key);
    $newArray[$newKey][$match[1]] = $value;
}

Where a print_r() of $newArray is as follows:

Array ( 
    [Template] => Array ( 
        [URL] => http://www.asdasdda.com 
        [UPC] => 5484548546314 
        ) 
    [Field] => Array ( 
        [value] => Test Example 
        [answer] => 20 
        ) 
    )
Sign up to request clarification or add additional context in comments.

3 Comments

If you only have single level, for instance Template[URL], then this will work great. If you ever have multiple levels, for instance Template[1][URL], then you can consult my answer's use of references in a loop.
@KevinWeber ^ above comment was supposed to be directed at you.
@Devon Thank you Devon, I will be bookmarking this page for future use of these scripts.
2

The problem should probably be addressed where the array is being created. If you don't have access to that, then you can use a regex with a reference loop to convert the array:

$array = [
"Template[URL]" => 'http://www.asdasdda.com',
"Template[UPC]" => '5484548546314',
"Multi[Level][Array]" => 'Hello World'
];

function convert(&$array, $key, $value) {
   preg_match_all("/(?=^)[^]]+(?=\[)|(?<=\[)[^]]+(?=\])/", $key, $keys);
   if ($keys = $keys[0]) {
       // Unset original key
       unset($array[$key]);
       // Dig into each level of keys and reassign the reference
       foreach($keys as $key) {
           if (!isset($array[$key])) $array[$key] = null;
           $array = &$array[$key];
       }
       // Set the final level equal to the original value
       $array = $value;
   }
}

foreach($array as $key=>$value) {
   convert($array, $key, $value);
}

print_r($array);

Outputs:

Array
(
    [Template] => Array
        (
            [URL] => http://www.asdasdda.com
            [UPC] => 5484548546314
        )

    [Multi] => Array
        (
            [Level] => Array
                (
                    [Array] => Hello World
                )

        )

)

References are used so you can dig into multiple levels if you should need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.