-1

I have JSON:

"{"description":"Testing","site":"http:\/\/localhost","steps":{"step":[{"command":"grabimage","parameter":"img[alt=\"Next\"]"},{"command":"click","parameter":"img[alt=\"Previous\"]"}]}}"

that is generated dynamicall by SimpleXML from an XML file:

<?xml version="1.0"?>
<pullcase>
<description>Testing</description>
<site>http://localhost:81</site>
<steps>
<step>
   <command>grabimage</command>
   <parameter>img[alt="Next"]</parameter>
</step>
</steps>
</pullcase>

It consists of a potentially unlimited number of "step" within "steps". When there is a single step the array is generated as:

["steps"]=>
array(1) {
["step"]=>
array(2) {
  ["command"]=>
  string(9) "grabimage"
  ["parameter"]=>
  string(15) "img[alt="Next"]"
}
}

While when there are multiple steps it is generated as:

["steps"]=>
array(1) {
["step"]=>
array(2) {
  [0]=>
  array(2) {
    ["command"]=>
    string(9) "grabimage"
    ["parameter"]=>
    string(15) "img[alt="Next"]"
  }
  [1]=>
  array(2) {
    ["command"]=>
    string(5) "click"
    ["parameter"]=>
    string(19) "img[alt="Previous"]"
  }
  }
}

How do I get the array that is generated for one child element to follow the same rules as multiple?:

["steps"]=>
array(1) {
["step"]=>
array(1) {
  [0]=>
  array(2) {
  ["command"]=>
  string(9) "grabimage"
  ["parameter"]=>
  string(15) "img[alt="Next"]"
}
}
6
  • 1
    Didn't I just answer that here? stackoverflow.com/questions/29017588/… or do you have more information? Commented Mar 12, 2015 at 20:18
  • Not exactly, you answered how to handle the data structure, the OP now asks how to avoid it. Commented Mar 12, 2015 at 20:23
  • @ThW: Well we'll need to see the code that "dynamically" creates this then won't we? Commented Mar 12, 2015 at 20:27
  • @AbraCadaver To have a chance to solve the problem, yes I would think so. :-) Commented Mar 12, 2015 at 20:36
  • I've added the source XML to the question. Commented Mar 12, 2015 at 21:10

2 Answers 2

0

The json_decode() is not to blame. The JSON structure defines if it is an array or only a single element.

Here is a stripped down example. It avoids using JSON_OBJECT_AS_ARRAY, so the objects are decoded to stdClass instances and the array is easier to spot.

$json = <<<'JSON'
{
  "key": {
    "list_example": [
      {
        "key": "value"
      }
    ]
  }
}
JSON;

var_dump(json_decode($json));

Output:

object(stdClass)#1 (1) {
  ["key"]=>
  object(stdClass)#2 (1) {
    ["list_example"]=>
    array(1) {
      [0]=>
      object(stdClass)#3 (1) {
        ["key"]=>
        string(5) "value"
      }
    }
  }
}

You can see that the list_example contains an array.

The problem is in the source that generates the JSON. An XML to JSON mapper would be a typical candidate for that.

Sign up to request clarification or add additional context in comments.

Comments

0

You will just need to overwrite the lone subarray array as an indexed array containing "itself".

Code (Demo)

$json='{"description":"Testing","site":"http:\\/\\/localhost","steps":{"step":{"command":"grabimage","parameter":"img[alt=\\"1Next\\"]"}}}';
$array=json_decode($json,true);
if(key($array['steps']['step'])==='command'){  // if first key is "command" it is not indexed (in other words, "lone")
    $array['steps']['step']=[$array['steps']['step']];  // position the lone subarray (deeper) in an indexed subarray.
}
var_export($array);

Output:

array (
  'description' => 'Testing',
  'site' => 'http://localhost',
  'steps' => 
  array (
    'step' => 
    array (
      0 => 
      array (
        'command' => 'grabimage',
        'parameter' => 'img[alt="1Next"]',
      ),
    ),
  ),
)

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.