0

I have this code:

function setWeek($week) {

preg_match_all("/[\d\/]+/", $week, $output, PREG_SET_ORDER, 0);

$this->week = array("week" =>
array('id' => $output[0][0], "start" => $output[1][0], "end" => $output[2][0]));
$json = json_encode($this->week);

echo $json;

}

It returns me this:

{"week":{"id":"247","start":"12\/04\/18","end":"19\/04\/18"}}{"week":{"id":"246","start":"05\/04\/18","end":"12\/04\/18"}}

But this form is wrong as it is multiple JSON's I would need to add [] and ',' How could I do this to stay like this:

[{"week":{"id":"247","start":"12\/04\/18","end":"19\/04\/18"}},{"week":{"id":"246","start":"05\/04\/18","end":"12\/04\/18"}}]
Bruno Andrade
  • 565
  • 1
  • 3
  • 17
  • Check this link https://stackoverflow.com/questions/20286208/merging-two-json-in-php – Abi Apr 24 '18 at 04:52
  • By observing the output it looks like you are iterating the `setWeek()` function but you have not included that part of code in your question. – Sagar Guhe Apr 24 '18 at 04:57
  • [JSON](http://json.org) is a text representation of some data structure. You can do the processing you describe in the question but this is not how JSON should be handled. Decode the JSON to recover the original data structure (or do not produce a JSON, in the first place), modify it and only when the data structure reaches the shape you need, encode it as JSON and output or store it. – axiac Apr 24 '18 at 06:21

3 Answers3

1

You need to make $this->week into an array.

The easiest way to do this would be to change your json_encode line to

$json = json_encode(array($this->week));

That said, there's a lot of refactoring to be done with this code that's outside of the scope of this question.

Josh McMillan
  • 724
  • 3
  • 8
  • The jsons are not generated at the same time, they are generated in different cycles of the software. The result I got was: string(68) "[{"week":{"id":"247","start":"12\/04\/18","end":"19\/04\/18"}}]" string(68) "[{"week":{"id":"246","start":"05\/04\/18","end":"12\/04\/18"}}]" – Bruno Andrade Apr 24 '18 at 04:57
1

By observing your output:

{"week":{"id":"247","start":"12\/04\/18","end":"19\/04\/18"}}{"week":{"id":"246","start":"05\/04\/18","end":"12\/04\/18"}}

Looks like you are iterating the setWeek function which is doing nothing but echoing json_encoded array many times in the iteration so the output is like this.

What you can do is return array in setWeek() and push that in a new a new array and then json_encode that new array like this:

$newArray = [];

loopStart: // just a placeholder from assumption made by me
  // some code

  $newArray[] = $this->setWeek($week);

loopEnd;
echo json_encode($newArray);

And your setWeek():

function setWeek($week) {

  preg_match_all("/[\d\/]+/", $week, $output, PREG_SET_ORDER, 0);

  $this->week = array("week" =>
    array('id' => $output[0][0], "start" => $output[1][0], "end" => $output[2][0]));
  $json = json_encode($this->week);

  return $this->week;

}

This should solve your problem.

P.S. This code also needs many refactors because this breaks many fundamental rules. Also my assumptions may be wrong if I am wrong please update your questions so that the question will be much clear.

Sagar Guhe
  • 1,041
  • 1
  • 11
  • 35
  • Perfect answer. If possible send me links of topics about the rules that are being broken. – Bruno Andrade Apr 24 '18 at 13:37
  • I cannot send you the links, but can hint. For eg: you are using `$this->week` var as a local variable as this is not meant for this purpose, this comes under OOP principle. Again you are using `setWeek()` method which should not return anything (according to standards) it should always set value for private property (or push new value in case of array property) and should use `getWeek()` to return whatever the value was set in `return $this->week`. – Sagar Guhe Apr 25 '18 at 10:34
0

Change:

$this->week = array("week" =>
array('id' => $output[0][0], "start" => $output[1][0], "end" => $output[2][0]));

To

$this->week = array(array("week" =>
array('id' => $output[0][0], "start" => $output[1][0], "end" => $output[2][0])));
Virb
  • 1,639
  • 1
  • 16
  • 25
Asad-ullah Khan
  • 1,573
  • 18
  • 22
  • The result is this: that it is not yet a Json in the correct format [{"week":{"id":"247","start":"12\/04\/18","end":"19\/04\/18"}}][{"week"":{"id":"246","start":"05\/04\/18","end":"12\/04\/18"}}] – Bruno Andrade Apr 24 '18 at 05:02