Sercan GÜLER
Sercan GÜLER

Reputation: 1

laravel http client to customize data

{
  "1": {
    "outcomes": {
      "1.1": {
        "outcome": "3.35",
        "handicap": null,
        "state": "active",
        "label": "1"
      },
      "1.2": {
        "outcome": "3.05",
        "handicap": null,
        "state": "active",
        "label": "X"
      },
      "1.3": {
        "outcome": "1.73",
        "handicap": null,
        "state": "active",
        "label": "2"
      }
    },
    "code": "20873",
    "mbc": "MBS 1"
  },
  "3": {
    "outcomes": {
      "3.1": {
        "outcome": "1.52",
        "handicap": null,
        "state": "active",
        "label": "1-X"
      },
      "3.2": {
        "outcome": "1.16",
        "handicap": null,
        "state": "active",
        "label": "1-2"
      },
      "3.3": {
        "outcome": "1.16",
        "handicap": null,
        "state": "active",
        "label": "X-2"
      }
    },
    "code": "20877",
    "mbc": "MBS 1"
  },
  "4": {
    "outcomes": {
      "7.1": {
        "outcome": "3.60",
        "handicap": null,
        "state": "active",
        "label": "1"
      },
      "7.2": {
        "outcome": "1.92",
        "handicap": null,
        "state": "active",
        "label": "X"
      },
      "7.3": {
        "outcome": "2.23",
        "handicap": null,
        "state": "active",
        "label": "2"
      }
    },
    "code": "21873",
    "mbc": "MBS 1"
  },
}

I am using Laravel http client. I want to edit this data as below, but I don't know how to do it. While pulling json data with HTTP Client, I want to edit and show this data as below. I hope I was able to explain my problem. Thank you in advance for your help.

{
    "1.1": {
        "outcome": "3.35",
        "handicap": null,
        "state": "active",
        "label": "1",
        "code": "20873",
        "mbc": "MBS 1"
    },
    "1.2": {
        "outcome": "3.05",
        "handicap": null,
        "state": "active",
        "label": "X",
        "code": "20873",
        "mbc": "MBS 1"
    },
    "1.3": {
        "outcome": "1.73",
        "handicap": null,
        "state": "active",
        "label": "2",
        "code": "20873",
        "mbc": "MBS 1"
    },
    "3.1": {
        "outcome": "1.52",
        "handicap": null,
        "state": "active",
        "label": "1-X",
        "code": "20877",
        "mbc": "MBS 1"
    },
    "3.2": {
        "outcome": "1.16",
        "handicap": null,
        "state": "active",
        "label": "1-2",
        "code": "20877",
        "mbc": "MBS 1"
    },
    "3.3": {
        "outcome": "1.16",
        "handicap": null,
        "state": "active",
        "label": "X-2",
        "code": "20877",
        "mbc": "MBS 1"
    },
    "7.1": {
        "outcome": "3.60",
        "handicap": null,
        "state": "active",
        "label": "1",
        "code": "21873",
        "mbc": "MBS 1"
    },
    "7.2": {
        "outcome": "1.92",
        "handicap": null,
        "state": "active",
        "label": "X",
        "code": "21873",
        "mbc": "MBS 1"
    },
    "7.3": {
        "outcome": "2.23",
        "handicap": null,
        "state": "active",
        "label": "2",
        "code": "21873",
        "mbc": "MBS 1"
    }
}

My code:

$json = collect($json['data']['markets']);
$json = $json->map(function ($item, $key) {
    return $item;
});
return $json;

How can we do it with "Laravel collect $json->map(function ... " ?

Upvotes: 0

Views: 146

Answers (2)

tanerkay
tanerkay

Reputation: 3930

Surprisingly not easy if you really insist on using map(), the hard part is flattening the array afterwards, as flattening relies on the keys in each dimension being unique, which may not always be the case.

For example, you could do this:

collect($json)->flatMap(
    fn ($group) => [
        collect($group->outcomes)->each(function ($outcome) use ($group) {
            $outcome->code = $group->code;
            $outcome->mbc = $group->mbc;
        }),
    ]
)->collapse()

But this would return unexpected results if your input JSON contained keys that are the same, for example:

{
  "1": {
    "outcomes": {
      "same-key": {
        "outcome": "3.35",
      }
    },
    "code": "20873",
    "mbc": "MBS 1"
  },
  "3": {
    "outcomes": {
      "same-key": {
        "outcome": "1.52",
      }
    },
    "code": "20877",
    "mbc": "MBS 1"
  }
}

I think the second one would overwrite the first.

Upvotes: 0

Gangadhar Darsi
Gangadhar Darsi

Reputation: 176

$json = collect($json);
$final = [];
foreach ($json as $item) {
    foreach ($item["outcomes"] as $key => $ver) {
        $ver["code"] = $item["code"];
        $ver["mbc"] = $item["mbc"];
        array_push($final, [$key => $ver]);
    }
};
return $final;

please check this way

Upvotes: 1

Related Questions