1

I have produced an array of fruits stored somewhere. Say it looks like this

$myFruits = array("apples"=>1, "oranges"=>3, "bananas"=>5);

This array is then passed into a function that will return json-encoded data for an API.

First, I wanted to be able to return a list of all the types of fruits I have

{"fruits":["apples", "oranges", "bananas"]}

I used this to accomplish it

echo json_encode(array("scripts" => array_keys($scripts)));

Now, I would like each type of fruit to be contained in its own hash, as such

{"fruits":
  [
    {name: "apples"
    },
    {name: "oranges"
    },
    {name: "bananas"
  ]
}

This way I can add additional fields to each fruit object without breaking existing code that may be using previous versions of this API (eg: if I decided to add the fruit counts in there as well).

Seeing how I can just create a new array and assign my list of fruits to a key called "fruits", I tried to do the same for each inner hash:

$myFruits = array("apples"=>1, "oranges"=>3, "bananas"=>5);
$data = array();
foreach ($myFruits as $key => $value) {

  // initialize an associative array for each fruit
  $val = array();
  array_push($val, array("name" => $key));

  // add it to the list of fruits
  array_push($data, $val);
}

// assign list of fruits to "fruits" key
$outData = array("fruits" => $data);
echo json_encode($outData);

But I get this instead

{"fruits":[[{"name":"apples"}],[{"name":"oranges"}],[{"name":"bananas"}]]}

There are extra square braces around each fruit hash, which I assume is because I'm using an array to store each key-value pair.

How would I get my desired output?

2 Answers 2

3

You're close to knowing what you're doing wrong. You're creating an array, and then just using it to add one item (another array) to it.

// initialize an associative array for each fruit
$val = array(); // this guy here is unnecessary!!
array_push($val, array("name" => $key));

// add it to the list of fruits
array_push($data, $val);

Instead, just push each individual array onto $data directly like this:

array_push($data, array("name" => $key));

DEMO

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

5 Comments

If I wanted to extend each fruit hash with a count pair (eg: {"name:banana", "count: 1"}), this approach doesn't seem to work. Which is why I initially thought of having that extra array.
Of course it works. Just use array("name" => $key, "count" => $someCount) instead.
Oh, in that case what is the difference between creating a new array and populating it first, and creating a new array in-line?
Because you were doing both. You were creating an empty array, and then putting your name/value pair array within that.
You're right, now that I look at it again, I assumed that was how key-value pairs were added, which evidently isn't the case!
1

You are creating an extra level in your array, simply push a new array onto $data in each iteration:

foreach ($myFruits as $key => $value) {
  $data[]=array("name" => $key, "count" => $value);
}

*edited as per your comment

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.