Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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"]
}
share|improve this question
4  
Remove JSON_FORCE_OBJECT? –  jszobody Nov 18 '13 at 18:38
    
@Savvas, Woudn't that format break your JSON ? –  Shankar Damodaran Nov 18 '13 at 18:39
    
@ShankarDamodaran, what would be wrong with the JSON? –  Savvas Nicholas Nov 18 '13 at 18:50

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.