1

I want to chunk json by it's value using PHP. Here sample json is

{
"16506": "10",
"16507": "10",
"16508": "10",
"16509": "10",
"16510": "20",
"16511": "20",
"16512": "20",
"16513": "30"
}

my expectation is to format like

10=>[16506,16507,16508,16509]
20=>[16510,16511,16512]
30=>[16513]

Is there any built-in function is php or have to use loop ? here I used json_decode() and array_flip(). But array_flip makes it distinct. Thanks in advance

2
  • you're probably looking for that one-liner, someone could probably get creative with one, but yeah just simply loop it and push via key Commented Oct 20, 2020 at 6:21
  • A simple foreach will suffice... Commented Oct 20, 2020 at 6:25

3 Answers 3

1

There is a build-in function called array_walk. Here you can read about it.

$json = '{"16506": "10","16507": "10","16508": "10","16509": "10","16510": "20","16511": "20","16512": "20","16513": "30"}';

array_walk(json_decode($json), function($value, $key) use (&$result)  {
  $result[$value][] = $key;
});

print_r($result);

Result:

Array
(
    [10] => Array
        (
            [0] => 16506
            [1] => 16507
            [2] => 16508
            [3] => 16509
        )

    [20] => Array
        (
            [0] => 16510
            [1] => 16511
            [2] => 16512
        )

    [30] => Array
        (
            [0] => 16513
        )

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

Comments

1

You can do something below, just need to go through a loop

$json = '{
"16506": "10",
"16507": "10",
"16508": "10",
"16509": "10",
"16510": "20",
"16511": "20",
"16512": "20",
"16513": "30"
}';


$ar = json_decode($json);
$newAr = []; 
foreach($ar as $k => $v){
    $newAr[$v][] = $k;
}
print_r($newAr);

Output will be:

Array
(
    [10] => Array
        (
            [0] => 16506
            [1] => 16507
            [2] => 16508
            [3] => 16509
        )

    [20] => Array
        (
            [0] => 16510
            [1] => 16511
            [2] => 16512
        )

    [30] => Array
        (
            [0] => 16513
        )

)

if you want as string, then implode it

foreach($newAr as $k => $v){
    $newAr[$k] = implode(',',$v);
}

print_r($newAr);

Output will be

Array
(
    [10] => 16506,16507,16508,16509
    [20] => 16510,16511,16512
    [30] => 16513
)

Comments

1

Here is my code

<?php
  $json =json_decode('{
  "16506": "10",
  "16507": "10",
  "16508": "10",
  "16509": "10",
  "16510": "20",
  "16511": "20",
  "16512": "20",
  "16513": "30"
  }',true);
$arrys = [];  
  foreach ($json as $k => $sd){ 
        $arrys[$sd][] = $k;   
  }
 
echo "<pre>"; print_r($arrys); die; 
 ?>

1 Comment

Why this? if(in_array($sd, $json)){? It will always be true since $sd comes from $json so naturally it has to be true. And your second foreach does nothing at all except moving the data from one array to the other. No difference what so ever. 3v4l.org/OoeYo

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.