php - Arrange JSON data saving duplicates into one row -


good morning guys!

i'm having hard time trying figure out how arrange following json:

{    "showelement": "1",    "degrees": [{        "name": "bachelor in psychology",        "number": "53",        "degree": "bachelor's degree"    }, {        "name": "certificate",        "number": "56",        "degree": "certificate"    }, {        "name": "high school diploma",        "number": "28",        "degree": "high school"    }, {        "name": "bachelor in sociology",        "number": "109",        "degree": "bachelor's degree"    }] } 

into this:

{    "showelement": "1",    "degrees": [{        "name": "bachelor in psychology", "bachelor in sociology",        "number": "53","109",        "degree": "bachelor's degree"    }, {        "name": "certificate",        "number": "56",        "degree": "certificate"    }, {        "name": "high school diploma",        "number": "28",        "degree": "high school"    }] } 

basically, put same degrees in 1 place , have names of said degree separated comma

i have json decoded variable:

$data = json_decode($topdegrees[1]["diplomas"],true); 

thanks in advance help!

i came this

$json = <<<json {    "showelement": "1",    "degrees": [{        "name": "bachelor in psychology",        "number": "53",        "degree": "bachelor's degree"    }, {        "name": "certificate",        "number": "56",        "degree": "certificate"    }, {        "name": "high school diploma",        "number": "28",        "degree": "high school"    }, {        "name": "bachelor in sociology",        "number": "109",        "degree": "bachelor's degree"    }] } json;  $data = json_decode( $json, true );  $degrees = $data['degrees']; $names = array_column($degrees, 'degree'); $count = array_count_values($names); $duplicates = array_filter($count, function($var) {   return $var > 1; });   foreach ( array_flip($duplicates) $degree ) {    $filter = array_filter($degrees, function($var) use ($degree) {     return ( $var['degree'] === $degree );   });    $names = [];   $numers = [];   foreach ( $filter $item ) {     $names[] = $item['name'];     $numbers[] = $item['number'];   }   $indices = array_keys($filter);   $index = array_shift($indices);   $degrees[$index]['name'] = $names; // = join(', ', $names);   $degrees[$index]['number'] = $numbers; // = join(', ', $numbers);    while ( count($indices) ) {     unset($degrees[array_shift($indices)]);   } }  $data['degrees'] = $degrees;  print_r(json_encode($data)); // {"showelement":"1","degrees":[{"name":["bachelor in psychology","bachelor in sociology"],"number":["53","109"],"degree":"bachelor's degree"},{"name":"certificate","number":"56","degree":"certificate"},{"name":"high school diploma","number":"28","degree":"high school"}]} 

your wanted json output not valid, have made array structure in place. if want comma separated text, use join statements i've commented out.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -