2

I am calling an API an receiving the following object response:

+"284": "Khaki's with Black polo shirt (no logo)"
+"286": "Black pants with Yellow shirt"
+"349": "Black pants with white collared shirt"
+"705": "See "Details" Section for Dress Attire"

I want to convert this object to an associative array but I am having some trouble.

Here's what I want:

[
    0 => ["id" => "284", "name" => "Khaki's with Black polo shirt (no logo)"],
    1 => ["id" => "286", "name" => "Black pants with Yellow shirt"],
    2 => ["id" => "349", "name" => "Black pants with white collared shirt"],
    3 => ["id" => "705", "name" => "See "Details" Section for Dress Attire"]
]

Or even this:

[
    "284" => "Khaki's with Black polo shirt (no logo)"
    "286" => "Black pants with Yellow shirt"
    "349" => "Black pants with white collared shirt"
    "705" => "See "Details" Section for Dress Attire"
]

Either of these are acceptable to me.

I am trying to do collect($response->result->attire)->toArray() but that obviously just gives me a list of names:

array:4 [
  0 => "Khaki's with Black polo shirt (no logo)"
  1 => "Black pants with Yellow shirt"
  2 => "Black pants with white collared shirt"
  3 => "See "Details" Section for Dress Attire"
]

I've tried using mapWithKeys unsuccessfully.

Any help is greatly appreciated.

Kevin Pimentel
  • 2,056
  • 3
  • 22
  • 50
  • 3
    If it's json response - decode it to array, no? – u_mulder Mar 27 '19 at 13:37
  • 1
    You are not receiving an object response from the API. Something or someone is converting the response to a PHP object. That something/someone can probably convert it to a PHP associative array instead. – apokryfos Mar 27 '19 at 13:42
  • To be clear, the API returns an object response. `On success, an object will be returned` – Kevin Pimentel Mar 27 '19 at 13:42
  • To any sensible API an object response is a JSON object which is actually a fancy string – apokryfos Mar 27 '19 at 13:43
  • I understand what you are saying but that isn't what I asked. I asked how to convert the object I posted above to one of the two acceptable arrays formats I listed. Thank you. – Kevin Pimentel Mar 27 '19 at 13:53

2 Answers2

4

In Laravel, you can use Laravel Collection helper functions. Here is the example for this:-

$arr = [
    "284" =>  "Khaki's with Black polo shirt (no logo)",
    "286" => "Black pants with Yellow shirt",
    "349" => "Black pants with white collared shirt",
    "705" => "See 'Details' Section for Dress Attire"
];
$collection = collect($arr);
$multiplied = $collection->map(function ($name, $key) {
    return [
        "id" => $key,
        "name" => $name
    ];
});
dd($multiplied->values()->toArray());

I think this will help you.

Lakhwinder Singh
  • 5,536
  • 5
  • 27
  • 52
1

Assuming that your response is a json response like this example.

$response = response()->json(['id' => 'string', 'id2' => 'string2']);
$response->getContent();

Makes a json string like the one I assume you are recieving from the API:

"{"id":"string", "id2":"string2"}"

and therefore:

(array) json_decode($response->getContent());

returns:

[
    "id" => "string",
    "id2" => "string2",
]

Which I think is what you wanted. In your case, without viewing the whole response json but based on the example you writed I think it would be like:

(array) json_decode($response->getContent()->result->attire);
namelivia
  • 2,657
  • 1
  • 21
  • 24