首页 > 解决方案 > 使用php在json中分组相同的名称

问题描述

我正在尝试使用顶点图表创建时间线。我不能从字面上打印 Json 数据。

$temp[$r["Durum"]]['data'][] = array( 
    'x' => $r['Basladı'],
    'y' => array(
        strtotime($r['Saat']),
        strtotime($r['Bitis'])
    )
);

输出

{
    "P": {
        "data": [
            {
                "x": "Basladi",
                "y": [
                    1602418875,
                    1602418884
                ]
            },
            {
                "x": "Basladi",
                "y": [
                    1602418887,
                    1602418902
                ]
            },
          
          
        ]
    }
}

我想要的输出

{
    {   
       "name" : "P",
        "data": [
            {
                "x": "Basladi",
                "y": [
                    1602418875,
                    1602418884
                ]
            },
            {
                "x": "Basladi",
                "y": [
                    1602418887,
                    1602418902
                ]
            },
          
 
        ]
    }
}

标签: phpjson

解决方案


这是一个代码示例,其中包含一些适合您的示例输入。

示例数据(在您的情况下是数据库中的数据):

$yourdata = array(
  array(
    'Durum' => 'p',
    'Basladı' => 'Basladı',
    'Saat' => '12.12.2020',
    'Bitis' => '12.12.2020'
  ),
  array(
    'Durum' => 'p',
    'Basladı' => 'Basladı',
    'Saat' => '12.12.2020',
    'Bitis' => '12.12.2020'
  ),
  array(
    'Durum' => 's',
    'Basladı' => 'Basladı',
    'Saat' => '12.12.2020',
    'Bitis' => '12.12.2020'
  )
);

代码片段:

// create empty temp array for the result
$temp = [];

// loop through your data (only an example - replace with your data)
foreach($yourdata as $r)  {

  // defines a temp data array to add
  $temp_data = array( 
    'x' => $r['Basladı'],
    'y' => array(
      strtotime($r['Saat']),
      strtotime($r['Bitis'])
    )
  );

  // check if there is already an entry with that name
  if(($key = array_search($r["Durum"], array_column($temp, 'name'))) !== false) {
    // if there is, only add the data to it
    $temp[$key]['data'][] = $temp_data;
  } else  {
    // otherwise push a new array with the name and data to temp
    $temp[] = array(
      'name' => $r["Durum"],
      'data' => array($temp_data)
    );
  }
}

var_dump($temp); // process with the data

变量的内容$temp

Array
(
    [0] => Array
        (
            [name] => p
            [data] => Array
                (
                    [0] => Array
                        (
                            [x] => Basladı
                            [y] => Array
                                (
                                    [0] => 1607731200
                                    [1] => 1607731200
                                )

                        )

                    [1] => Array
                        (
                            [x] => Basladı
                            [y] => Array
                                (
                                    [0] => 1607731200
                                    [1] => 1607731200
                                )

                        )

                )

        )

    [1] => Array
        (
            [name] => s
            [data] => Array
                (
                    [0] => Array
                        (
                            [x] => Basladı
                            [y] => Array
                                (
                                    [0] => 1607731200
                                    [1] => 1607731200
                                )

                        )

                )

        )

)

推荐阅读