3

I have the following PHP array:

array("Apple", "Pear", "Grape", "Orange")

And I'd like to get JSON output like the following:

[[{"fruit":"Apple"}],[{"fruit":"Pear"}],[{"fruit":"Grape"}],[{"fruit":"Orange"}]]

JSON confuses me :(

EDIT Sorry, those last two in the output should have been fruit, I corrected it, sorry guys.

5
  • Your JSON is an array of arrays of objects. You sure that's what you want? Commented Aug 18, 2011 at 20:02
  • 1
    How do you determine which array items are fruits and which are labels? Commented Aug 18, 2011 at 20:13
  • This is the format that $javascript->object() spits out in CakePHP, and what FusionCharts wants. The real question was how to convert an indexed PHP array to an associate PHP with all same keys. Commented Aug 18, 2011 at 20:22
  • @Pyrite: You have 2 different keys. fruit and label. Commented Aug 18, 2011 at 20:23
  • 1
    I just realized, those last two should not say label, but should say fruit, goddammit. Commented Aug 18, 2011 at 20:24

5 Answers 5

5

If you want your JSON output to look like that, you should change your PHP value to look like this:

array(array(array('fruit' => 'Apple')), array(array('fruit' => 'Pear')), array(array('fruit' => 'Grape')), array(array('fruit' => 'Orange')))

and then pass that array to json_encode()

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

3 Comments

Yes, and how do I change the indexed PHP array to an associative PHP array, all with the same key like that? That's what I don't know.
To get the JSON format the OP wants, your array should be array(array(array('fruit' => 'Apple')),array(array('fruit' => 'Pear')))
Fixed. I'm not sure what OP means about changing the indexed array to an associative array, all with the same key, though. An associative array cannot have more than one value per key, unless you make those values arrays or something.
2
$fruit = array("Apple", "Pear", "Grape", "Orange");
$json = array();
foreach($fruit as $f){
  $json[][] = array('fruit' => $f);
}
echo json_encode($json);
// [[{"fruit":"Apple"}],[{"fruit":"Pear"}],[{"fruit":"Grape"}],[{"fruit":"Orange"}]]

5 Comments

OP has two 'fruit' and two 'label'. you're doing only fruit.
@Marc: Oops, I didn't notice that.
@Marc: The OP fixed his question, and all the keys are fruit.
This works for me, however, is there a way that doesn't involve a loop? Cause I have a whole bunch of these and loops are expensive on performance I think.
@Pyrite: Loops aren't that expensive, plus I can't really think of another way. Even the built-in array methods will use loops internally.
1

You could write a little function which takes the index array and your desired key value, and spits out the required structure.

function associativify($array, $key) {
    $result = array();
    foreach ($array as $item) {
        $result[] = array(array($key => $item));
    }
    return $result;
}

$subject = array("Apple", "Pear", "Grape", "Orange");
$munged = associativify($subject, 'fruit');
$json   = json_encode($munged);

(Aside: choose a better function name than I have!)

1 Comment

I would +1 too, but that would skew things.
0

json_encode is the function you're looking for

Comments

0

http://php.net/json_encode (if you're lazy)

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.