I want to take out a specific JSON whose json_decode doesn't work well and make it into an associative array.

Asked 2 years ago, Updated 2 years ago, 49 views

There is an array called $parentArr.I want to get a specific array of JSONs out of it and make it an associative array, but it doesn't work.

print_r($parentArr);

Result ↓

Array
(
[id] = > 1
company_id = > 2
fruits=>{"apple":4"peach":2,"orange":1}
);

Take the fruits out of here and say $fruitArray=['apple'=>4,'peach'=>1,'orange'=>1].

$fruitArray=json_decode($parentArr["fruits", true);
print_r($fruitArray);

Results

Nothing is displayed.

php laravel

2022-09-30 14:57

1 Answers

That's strange.I think the code below will work out as expected, so please compare the differences.

<?php
$json=json_encode(array("apple"=>4, "peach"=>2, "orange"=>1));
$parentArr=array("id"=>1, "company_id"=>2, "fruit"=>$json);
print_r($parentArr);
$fruitArray=json_decode($parentArr["fruits", true);
print_r($fruitArray);

Results

Array (
[id]=>1
[company_id] = >2
[fruits]=>{"apple":4, "peach":2, "orange":1}

Array (
[Apple]=>4
[peach]=>2
range=>1


2022-09-30 14:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.