0

Mycode:

$firstArr[][$titles[0]]  = (string) $data[1];
$firstArr[][$titles[1]]  = (string) $data[2];
$firstArr[][$titles[2]]  = (string) $data[3];
$firstArrIndex = json_encode($firstArr, JSON_UNESCAPED_UNICODE). "\r";

Laravel output:

[{"code":"11000"},{"postal":"0988"},{"prefecture_kana":""}]

I want to get array result is:

[{"code":"11000","postal":"0988","prefecture_kana":""}]

Could you help me: [{"code":"11000","postal":"0988","prefecture_kana":""}] like that result.

2 Answers 2

1

If Your response is json response then try this else remove json_decode and use only loop :)

$a = '[{"code":"11000"},{"postal":"0988"},{"prefecture_kana":""}]';
$b = json_decode($a);
$array = [];
foreach ($b as $key => $value) {
    foreach ($value as $keySub => $valueSub) {
        $array[$keySub] = $valueSub;
    }
}
dd($array);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use Laravel's array_collapse function e.g:

array_collapse(json_decode('[{"code":"11000"},{"postal":"0988"},{"prefecture_kana":""}]', true))

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.