2015-05-11 7 views
4

Мне нужна помощь в правильном форматировании моего JSON. Создайте родительские объекты для каждого из действий, а затем добавьте к ним детей. В приведенных ниже примерах это будет одно родительское действие для «Тест» с двумя дочерними и другим родителем для «Test2» с тремя детьми. Я связался в двух jsonblobs с форматом, который я получаю, и в формате, который мне нужен. Любая помощь будет оценена по достоинству.Формат JSON с PHP

+---------------+-------+--------------+------------+------------+--------+ 
| ACTIVITY_NAME | GROUP | START_DATE | END_DATE | COMPLETED | TOTAL | 
+---------------+-------+--------------+------------+------------+--------+ 
|   Test |  1 | 04/30/2015 | 05/01/2015|  10 | 15 | 
|   Test |  2 | 04/30/2015 | 05/01/2015|  20 | 25 | 
|   Test2 |  1 | 05/2/2015 | 05/03/2015|  30 | 35 | 
|   Test2 |  2 | 05/2/2015 | 05/03/2015|  40 | 45 | 
|   Test2 |  3 | 05/2/2015 | 05/03/2015|  50 | 55 | 
+---------------+-------+--------------+------------+------------+--------+ 

PHP:

<?php 
include("connect.php"); 

if($conn === false) { 
    echo "Could not connect.\n"; 
    die(print_r(sqlsrv_errors(), true)); 
} 
/* Set up and execute the query. */ 
$sql = "<query>"; 
$stmt = sqlsrv_query($conn, $sql); 

do { 
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { 
    $json[] = $row; 

    } 
} while (sqlsrv_next_result($stmt)); 

foreach ($json as $result) { 
    $data[data][][$result['ACTIVITY_NAME']]['children'] = $result; 
} 
echo json_encode($data); 
?> 

Это то, что я получаю: https://jsonblob.com/5550c921e4b002ae4e370469

Это то, что мне нужно: https://jsonblob.com/5550c942e4b002ae4e370471

Edit - Вот что моя работа нг сценарий в конечном итоге выглядит как:

<?php 
include("connect.php"); 

if($conn === false) { 
    echo "Could not connect.\n"; 
    die(print_r(sqlsrv_errors(), true)); 
} 
/* Set up and execute the query. */ 
$sql = "<query> "; 
$stmt = sqlsrv_query($conn, $sql); 

// This is where the data will be organized. 
// It's better to always initialize the array variables before putting data in them 
$data = array(); 

// Get the rows one by one 
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { 
    // Extract the activity name; we want to group the rows by it 
    $name = $row['ACTIVITY_NAME']; 
    $group = ''; 
    $sdate = ''; 
    $edate = ''; 
    $completed = ''; 
    $total = ''; 
    $perc = ''; 

    // Check if this activity was encountered before 
    if (! isset($data[$name])) { 
     // No, this is the first time; we will make room for it, first 
     $data[$name] = array(
      // Remember the name 
      'ACTIVITY_NAME' => $name, 
      'MAINTENANCE_GROUP' => $group, 
      'START_DATE' => $sdate, 
      'END_DATE' => $edate, 
      'COMPLETED' => $completed, 
      'TOTAL_CLUSTERS' => $total, 
      'COMPLETE_PERC' => $perc, 
      // No children yet 
      'children' => array(), 
     ); 
    } 
    // Put the row into the list of children for this activity 
    $data[$name]['children'][] = $row; 
} 

// Here, the entries in $data are indexed by the values they also have in     'ACTIVITY_NAME' 
// If you want them numerically indexed, all you have to do is: 
$data = array_values($data); 
echo json_encode(array('data' => $data)); 
//echo json_encode($data); 
?> 

ответ

5

Вы не сделали этого ow запрос/запросы, которые вы запускаете, но для такой простой задачи я думаю, что одного запроса достаточно. Внешний do/while петля на sqlsrv_next_result() не требуется. Вы должны использовать его, когда вы отправляете несколько запросов (разделенных точкой с запятой) одним вызовом на sqlsrv_query().

Вам не нужно запускать два раза через результирующий набор. Вы можете организовать свои данные, как только вы получите его из базы данных.

Все, что вам нужно проверить значения, полученные из базы данных и создать структуру данных, как вам нужно:

// ... 
$stmt = sqlsrv_query($conn, $sql); 

// This is where the data will be organized. 
// It's better to always initialize the array variables before putting data in them 
$data = array(); 

// Get the rows one by one 
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { 
    // Extract the activity name; we want to group the rows by it 
    $name = $row['ACTIVITY_NAME']; 
    // Check if this activity was encountered before 
    if (! isset($data[$name])) { 
     // No, this is the first time; we will make room for it, first 
     $data[$name] = array(
      // Remember the name 
      'ACTIVITY_NAME' => $name, 
      // No children yet 
      'children' => array(), 
     ); 
    } 
    // Put the row into the list of children for this activity 
    $data[$name]['children'][] = $row; 
} 

// Here, the entries in $data are indexed by the values they also have in 'ACTIVITY_NAME' 
// If you want them numerically indexed, all you have to do is: 
$data = array_values($data); 

// That's all 
+0

Спасибо! Это именно то, что мне нужно! – solar411

+0

Как получить имена объектов/массивов, как во втором блобе? – solar411

+0

Выяснил это 'echo json_encode (массив ('data' => $ data));' – solar411

1

Изменить эти 3 линии:

foreach ($json as $result) { 
    $data[data][][$result['ACTIVITY_NAME']]['children'] = $result; 
} 

это:

foreach ($json as $result) { 
    $data['data'][] = array('ACTIVITY_NAME' => $result['ACTIVITY_NAME'], 'children' => $result); 
} 
+0

Это добавило все ребенок все родители. – solar411

1

Вы можете спасти детей поля в массиве

do { 
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { 
    $children = array("MAINTENANCE_GROUP" => $row["MAINTENANCE_GROUP"], "START_DATE" => $row["START_DATE"], 
     "END_DATE" => $row["END_DATE"], "COMPLETED" => $row["COMPLETED"], "TOTAL" => $row["TOTAL"]); 

    $json[] = array("MAINTENANCE_GROUP" => $row["MAINTENANCE_GROUP"], "children" => $children); 

} 
} while (sqlsrv_next_result($stmt)); 
1

я предполагаю использовать ниже код

while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { 
// Extract the activity name; we want to group the rows by it 
$name = $row['ACTIVITY_NAME']; 
// Check if this activity was encountered before 
if (! isset($data[$name])) { 
    // No, this is the first time; we will make room for it, first 
    $data[$name] = array(
     // Remember the name 
     'ACTIVITY_NAME' => $name, 
     // No children yet 
     'children' => array(), 
    ); 
} 
// Put the row into the list of children for this activity 
$data[$name]['children'][] = $row; 
} 
$json=json_encode($data); 
Смежные вопросы