安排JSON数据将重复项保存到一行

时间:2017-04-06 13:42:57

标签: php arrays json sorting

早上好![/ p>

我很难弄清楚如何安排以下JSON:

$ python exrex.py 'AB[CD]1234'
ABC1234
ABD1234

进入这个:

{
   "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解码为变量:

{
   "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"
   }]
}

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

我想出了这个

$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) as $degree ) {

  $filter = array_filter($degrees, function($var) use ($degree) {
    return ( $var['degree'] === $degree );
  });

  $names = [];
  $numers = [];
  foreach ( $filter as $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"}]}

你想要的json输出无效,我已经在它的位置建立了一个数组结构。如果你想要逗号分隔的文本,只需使用我注释掉的连接语句。

答案 1 :(得分:0)

$str = '{
   "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"
   }]
}';
$str_arr = json_decode($str);
foreach($str_arr->degrees as $k=>$val){
    if($val->degree == 'Bachelor\'s Degree'){
        $new['Bachelor'][] = $val;
    }else{
        $new[] = $val;
    }
}

foreach($new['Bachelor'] as $aa){
    $nameStr[]= $aa->Name;
    $numStr[] = $aa->Number;
}
$nameStr = implode(', ', $nameStr);
$numStr = implode(', ', $numStr);
$degree = 'Bachelor\'s Degree';
$new[] =  (object) array($nameStr, $numStr, $degree);
unset($new['Bachelor']);

echo $json = json_encode($new);
echo "<pre>"; print_r(json_decode($json)); 

希望这有帮助。

Demo here

答案 2 :(得分:0)

这应该这样做:

<?php
$json = '{
"showElement": "1",
"degrees": [{
    "Name": "Bachelor in Psychology",
    "Number": "53",
    "degree": "Bachelors Degree"
}, {
    "Name": "Certificate",
    "Number": "56",
    "degree": "Certificate"
}, {
    "Name": "High School Diploma",
    "Number": "28",
    "degree": "High School"
}, {
    "Name": "Bachelor in Sociology",
    "Number": "109",
    "degree": "Bachelors Degree"
}]
}';

$items = json_decode($json, true);
$orderedItems = [];
$final = ['showElement' => 1];
foreach ($items['degrees'] as $item) {
    $orderedItems[$item['degree']]['Name'][] = $item['Name'];
    $orderedItems[$item['degree']]['Number'][] = $item['Number'];
    $orderedItems[$item['degree']]['degree'] = $item['degree'];
}

foreach ($orderedItems as $order) {
    $order['Name'] = (count($order['Name']) > 1) ? $order['Name'] : $order['Name'][0];
    $order['Number'] = (count($order['Number']) > 1) ? $order['Number'] : $order['Number'][0];
    $final['degrees'][] = [
        'Name' => $order['Name'],
        'Number' => $order['Number'],
        'degree' => $order['degree']
    ];
}

echo json_encode($final);