0

I'm trying to get JSON by name and save it as a variable with everything related to that nest, what would the best way to accomplish this? (in my scenario the names are unique)

[{
    "name": "Joe",
    "age": "30",
    "gender": "male"
},
{ 
    "name": "Logan", 
    "age": "27",
    "gender": "male"
}]

to something like this

{
"Joe": [{
    "name": "Joe",
    "age": "30",
    "gender": "male"
}],
"Logan": [{
    "name": "Logan",
    "age": "27",
    "gender": "male"
}]
}

i need to be able to search on the name to get the correct one, the API switches order so can't get it from just id

4
  • 5
    Hmm, step back a bit and think to yourself is this the correct way to do this? In your wanted output you are now duplicating a field when there's no need to do so. Whos to say that those names will always be unique? Commented Jan 5, 2018 at 14:51
  • 1
    Are you sure you want the result to be an object containing single-element arrays? Commented Jan 5, 2018 at 15:05
  • why do you need/want this, exactly? You already have the name information within the JSON, this just duplicates it. And an array-per-name which, if your names are unique, will only ever have one element in it seems a bit pointless. Commented Jan 5, 2018 at 15:24
  • can you paste original json_decode Commented Jan 5, 2018 at 16:15

2 Answers 2

1

Hi This might helps you

$list = '[ 
   { 
      "name":"Joe",
      "age":"30",
      "gender":"male"
   },
   {  
      "name":"Logan",
      "age":"27",
      "gender":"male"
   }]';


$newjson = json_decode($list, true);
$final = [];

foreach ($newjson as $key => $value) {
  $final[$value['name']][]=$value;
}
$finaloutput = json_encode($final, true);
echo "<pre>";
print_r($finaloutput);
echo "</pre>";
exit;

The output is

{"Joe":
[{"name":"Joe","age":"30","gender":"male"}],

"Logan":
[{"name":"Logan","age":"27","gender":"male"}]}
Sign up to request clarification or add additional context in comments.

2 Comments

This output does not quite match what the OP asked for. It appears that the new object with named properties should have "collections" (arrays) as those named properties.
@JAAulde Hi Now the output as expected
1

The Laravel Collections Library is a very useful lib to use cases like yours.

For this problem you could use the keyBy method!

$json = '[ 
   { 
      "name":"Joe",
      "age":"30",
      "gender":"male"
   },
   {  
      "name":"Logan",
      "age":"27",
      "gender":"male"
   }
]';

$array = collect(json_decode($json, true))->keyBy('name')->all();

print_r($array); // will be the array with the keys defined by the name!

See https://laravel.com/docs/5.5/collections#method-keyby

3 Comments

Your answer may be correct, but it should include a code-sample that operates on the OP's example data.
@TonyChiboucas Okay, i've provided a sample code segment.
Now it makes a strong case for how Laravel Collections can simplify these problems!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.