2017-04-19 4 views
1

Ich bin ein bisschen verwirrt, wie ich Antwortdaten wie diese erstellen.Daten erstellen JSON PHP

[ 
    { 
    "profile": "Profile 1", 
    "kumpulan_spot": [ 
     { 
     "urutan": "1", 
     "id": "0415AA9E2D", 
     "checkpoint": "3. OFFICE DEPAN LT. 2", 
     "lokasi": "WARU", 
     "waktu": "none", 
     "status": "none", 
     "kondisi": "none", 
     "foto": "none" 
     }, 
     { 
     "urutan": "2", 
     "id": "0415AAA007", 
     "checkpoint": "4. OFFICE KRIAN LT.3 - R. MIS", 
     "lokasi": "KRIAN", 
     "waktu": "none", 
     "status": "none", 
     "kondisi": "none", 
     "foto": "none" 
     } 
    ] 
    }, 
    { 
    "profile": "Profile 2", 
    "kumpulan_spot": [ 
     { 
     "urutan": "3", 
     "id": "0415AAA01B", 
     "checkpoint": "2. OFFICE DEPAN LT.1 - R. MARKETING", 
     "lokasi": "KRIAN", 
     "waktu": "none", 
     "status": "none", 
     "kondisi": "none", 
     "foto": "none" 
     } 
    ] 
    } 
] 

aber so weit, was ich

$myparams['nik'] = $_POST['nik']; 

$procedure_params = array( 
array(&$myparams['nik'] , SQLSRV_PARAM_IN) 

); 
$sql = "EXEC SP_GETJADWAL @nik=?"; 
$stmt = sqlsrv_prepare($conn, $sql,$procedure_params); 

if(!$stmt) { 
die(print_r(sqlsrv_errors(), true)); 
} 
$return_hasil=[]; 
$myArray = []; 
if(sqlsrv_execute($stmt)){ 
    do { 
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { 
     $arrItem=[];   
     $detailItem=[]; 
     // $i=0; 

     // while($i==0){ 
      $arrItem["Profile"]="Profile ".$row['Profile']; 
      // $i=1; 
      // } 
      $detailCheckPoint=[]; 
      $detailCheckPoint["urutan"]=$row['Urutan'];   
      $detailCheckPoint["id"]=$row['check_point_id']; 
      $detailCheckPoint["checkpoint"]=$row['check_point_name']; 
      $detailCheckPoint["lokasi"]=$row['check_point_loc']; 
      $detailCheckPoint["waktu"]="none"; 
      $detailCheckPoint["status"]="none"; 
      $detailCheckPoint["kondisi"]="none"; 
      $detailCheckPoint["foto"]="none"; 
      $detailItem[]=$detailCheckPoint;  
      $arrItem["ListCheckPoint"]=$detailItem; 
      $return_hasil[]=$arrItem; 


    } 


    } while (sqlsrv_next_result($stmt)); 
    echo json_encode($return_hasil); 
}else{ 
    die(print_r(sqlsrv_errors(), true)); 
} 
}else{ 
    echo 'Connection Failure<br/>'; 
} 

und führen bekommen versucht haben, wie diese

[ 
    { 
    "Profile": "Profile 1", 
    "ListCheckPoint": [ 
     { 
     "urutan": "1", 
     "id": "0415AA9E2D", 
     "checkpoint": "3. OFFICE DEPAN LT. 2", 
     "lokasi": "WARU", 
     "waktu": "none", 
     "status": "none", 
     "kondisi": "none", 
     "foto": "none" 
     } 
    ] 
    }, 
    { 
    "Profile": "Profile 1", 
    "ListCheckPoint": [ 
     { 
     "urutan": "2", 
     "id": "0415AAA007", 
     "checkpoint": "4. OFFICE KRIAN LT.3 - R. MIS", 
     "lokasi": "KRIAN", 
     "waktu": "none", 
     "status": "none", 
     "kondisi": "none", 
     "foto": "none" 
     } 
    ] 
    }, 
    { 
    "Profile": "Profile 1", 
    "ListCheckPoint": [ 
     { 
     "urutan": "3", 
     "id": "0415AAA01B", 
     "checkpoint": "2. OFFICE DEPAN LT.1 - R. MARKETING", 
     "lokasi": "KRIAN", 
     "waktu": "none", 
     "status": "none", 
     "kondisi": "none", 
     "foto": "none" 
     } 
    ] 
    } 
] 

im neuen Daten json zu schaffen und wenig verwirrt, wie Reaktion zu erreichen, was ich wollen

+0

Was ist das Problem? Das Format sieht korrekt aus. –

+0

Sie möchten mehrere Arrays in "kumpulan_spot" einfügen. Unter welchen Umständen entscheidest du dich, Element in "kumpulan_spot" zu setzen? –

+0

@TouheedKhan unter Bedingung $ arrItem ["Profil"] = "Profil". $ Row ['Profil']; sir so das Profil kann viele kumpulan_spot –

Antwort

1

-Code aktualisiert wie pro Antrag:

diesen Code Versuchen:

Ich hoffe, dass es funktioniert.

$arrItem["Profile"] ="Profile ".$row['Profile']; 
$detailCheckPoint=[]; 
$detailCheckPoint["urutan"]=$row['Urutan'];   
$detailCheckPoint["id"]=$row['check_point_id']; 
$detailCheckPoint["checkpoint"]=$row['check_point_name']; 
$detailCheckPoint["lokasi"]=$row['check_point_loc']; 
$detailCheckPoint["waktu"]="none"; 
$detailCheckPoint["status"]="none"; 
$detailCheckPoint["kondisi"]="none"; 
$detailCheckPoint["foto"]="none"; 
foreach ($return_hasil as $key1 => $value) { 
    if($value["Profile"] === $arrItem["Profile"]) { 
     var_dump($detailCheckPoint); 
     $return_hasil[$key1]["ListCheckPoint"][] = $detailCheckPoint; 
     continue 2; 
    } 
} 
$arrItem['ListCheckPoint'] = array($detailCheckPoint); 
$return_hasil[]=$arrItem; 
+0

seine Arbeit danke, aber es gibt 1 Daten können nicht zu meiner JSON-Struktur anzeigen @ Touheed Khan –

+0

@anasrachmadi welche Daten? Bitte erläutern Sie, damit ich das Problem beheben kann. –

+0

so in meinen Daten gibt es Profil 1 enthalten JSON-Array [1,2,3] und Profil 2 enthalten [1,2]. Zeigen Sie mit Ihrem Code nur Profil 1 [2,3] und Profil 2 [2] an. @Touheed Khan –

0

Entfernen Sie die Initialisierung von der While-Schleife

$detailItem=[]; 
+0

immer noch nicht arbeiten, Profil 1 fehlt und nur Profil 2 mit meinem allen Element –

+0

remove 'detailCheckPoint = []' aus während –

+0

immer noch nicht funktioniert, seine aussehen wie seine letzte Zeile Datentabelle so holen Profil 1 nicht angezeigt @Nishant Nair –