-1

I have the following PHP array:

array(3) {
  ["NDQ"]=>
  array(2) {
    [0]=>
    string(6) "Google"
    [1]=>
    string(5) "Yahoo"
  }
  ["NYSE"]=>
  array(2) {
    [0]=>
    string(5) "3M Co"
    [1]=>
    string(19) "Abbott Laboratories"
  }
  ["FX"]=>
  array(1) {
    [0]=>
    string(17) "Euro vs US Dollar"
  }
}

I want to save this as a JSON document so I do the follwoing:

$instrument_names = json_encode($instrument_names, JSON_FORCE_OBJECT);
file_put_contents("../data/instrument_names.json", $instrument_names);

But the JSON file I get is as follows:

{
    "NDQ":
    {
        "0":"Google",
        "1":"Yahoo"
    },

    "NYSE":
    {
        "0":"3M Co",
        "1":"Abbott Laboratories"
    },

    "FX":
    {
        "0":"Euro vs US Dollar"
    }
}

where each name has been given a key as the index of the array (which was sequentially numbered).

How do I turn this into an array instead?

"NDQ":
{
    ["Google","Yahoo"]
}
3
  • 4
    Remove JSON_FORCE_OBJECT? Commented Nov 18, 2013 at 18:38
  • @Savvas, Woudn't that format break your JSON ? Commented Nov 18, 2013 at 18:39
  • @ShankarDamodaran, what would be wrong with the JSON? Commented Nov 18, 2013 at 18:50

1 Answer 1

0

As someone kindly pointed out:

I was using JSON_FORCE_OBJECT option that forces sequential arrays to be JSON objects.

When I removed this, it worked fine.

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.