1

if my json_encode outputs like this.

[{"id":"nameid","src":"http#"},{"id":"nameid","src":"http#"}]

how can i turn into something like:

[["name1","address1"],["name2","address2"]]
1
  • Aside from being an array with two members, the two data structures appear to have nothing in common, so there is no logical way to perform a conversion. Commented Jun 30, 2011 at 14:09

2 Answers 2

1
var obj, result, source, _i, _len;
  source = [
    {
      "id": "nameid",
      "src": "http#"
    }, {
      "id": "nameid",
      "src": "http#"
    }
  ];
  result = [];
  for (_i = 0, _len = source.length; _i < _len; _i++) {
    obj = source[_i];
    result.push([obj.id, obj.src]);
  }

(That was generated by coffeescript. The coffeescript source FYI is much smaller)

source = [{"id":"nameid","src":"http#"},{"id":"nameid","src":"http#"}]
result = []
result.push([obj.id, obj.src]) for obj in source
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you mean that name1 is the value nameid from the first object, etc...

Convert it to the array format you want before passing it to json_encode(). Something like this might work for you:

// Assuming your objects are an array $objects
$output_array = array();
foreach ($objects as $o) {
  // Put the two properties from the object into an array
  // and append it to $output_array
  $output_array[] = array($o->id, $o->src);
}

// Encode the array as json
$json = json_encode($output_array);

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.