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.

5
  • 3
    If it's json response - decode it to array, no? Commented Mar 27, 2019 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. Commented Mar 27, 2019 at 13:42
  • To be clear, the API returns an object response. On success, an object will be returned Commented Mar 27, 2019 at 13:42
  • To any sensible API an object response is a JSON object which is actually a fancy string Commented Mar 27, 2019 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. Commented Mar 27, 2019 at 13:53

2 Answers 2

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.

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

Comments

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);

1 Comment

Yeah, that's the "or event this" result.

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.