2016-09-05 35 views
-1

Ich habe eine JSON-Daten, die ich von meinem JSON-Array erhalten möchte, so dass ich die Daten auf meinem PHP-Formular anzeigen kann. Hier ist meine Json Objekt:Parsing JSON-URL in PHP

{ 
"response": { 
    "result": { 
     "Contacts": { 
      "row": [ 
       { 
        "no": "1", 
        "fl": [ 
         { 
          "val": "CONTACTID", 
          "content": "144120000000079041" 
         }, 
         { 
          "val": "First Name", 
          "content": "Tata" 
         }, 
         { 
          "val": "Last Name", 
          "content": "Nyerere" 
         }, 
         { 
          "val": "Email", 
          "content": "[email protected]" 
         }, 
         { 
          "val": "Account Name", 
          "content": "null" 
         }, 
         { 
          "val": "Phone", 
          "content": "null" 
         }, 
         { 
          "val": "Mobile", 
          "content": "null" 
         } 
        ] 
       }, 
       { 
        "no": "2", 
        "fl": [ 
         { 
          "val": "CONTACTID", 
          "content": "144120000000069001" 
         }, 
         { 
          "val": "First Name", 
          "content": "null" 
         }, 
         { 
          "val": "Last Name", 
          "content": "DANIEL NYERERE" 
         }, 
         { 
          "val": "Email", 
          "content": "[email protected]" 
         }, 
         { 
          "val": "Account Name", 
          "content": "null" 
         }, 
         { 
          "val": "Phone", 
          "content": "null" 
         }, 
         { 
          "val": "Mobile", 
          "content": "null" 
         } 
        ] 
       }, 
       { 
        "no": "3", 
        "fl": [ 
         { 
          "val": "CONTACTID", 
          "content": "144120000000065068" 
         }, 
         { 
          "val": "First Name", 
          "content": "null" 
         }, 
         { 
          "val": "Last Name", 
          "content": "Lawrence" 
         }, 
         { 
          "val": "Email", 
          "content": "[email protected]" 
         }, 
         { 
          "val": "ACCOUNTID", 
          "content": "144120000000065066" 
         }, 
         { 
          "val": "Account Name", 
          "content": "Zoho" 
         }, 
         { 
          "val": "Phone", 
          "content": "1 888 900 9646" 
         }, 
         { 
          "val": "Mobile", 
          "content": "null" 
         } 
        ] 
       } 
      ] 
     } 
    }, 
    "uri": "/api/json/contacts/getrecords" 
} 
} 

Ich brauche diese Daten auf meinem php Formular anzuzeigen, und hier ist die Probe ich versucht habe, aber ich bin fest mit ihm: `

<?php 
$url = "my url"; 
$json = file_get_contents($url); 
$json = json_decode($json, true); 
$response = $json['response']; 
$result = $response['result']; 
$contact = $result['Contacts']; 
$data = $contact['row']; 
$name = $dat[0]; 
echo "Contact name: " . $name; 
?> 

Ich möchte die Werte von ContactID, Vorname, Nachname, E-Mail in meinem php Form Jeder angezeigt werden, die auf das helfen kann, wird viel

+0

gut, wo ist dein Formular? Das Formular ist tatsächlich in HTML, PHP kann nur verwendet werden, um es zu generieren. Siehe - http://www.w3schools.com/html/html_forms.asp - http://www.w3schools.com/php/php_forms.asp - http://php.net/manual/de /faq.html.php –

Antwort

0

Hier ist einfach Art und Weise, ich eine Hilfsfunktion gemacht Wert von Array zu extrahieren. Schau dir dieses Beispiel an:

<?php 
$url  = "my url"; 
$json  = file_get_contents($url); 
$json  = json_decode($json, true); 
$response = $json['response']; 
$result = $response['result']; 
$contact = $result['Contacts']; 
$data  = $contact['row']; 

// Helper Function 
function getValByName($name, $array) 
{ 
    foreach ($array as $row) { 
     if ($row['val'] == $name) { 
      return $row['content']; 
     } 
    } 

    return ''; 
} 

foreach ($data as $row) { 
    $FirstName = getValByName('First Name', $row['fl']); 
    $ContactId = getValByName('CONTACTID', $row['fl']); 
    $LastName = getValByName('Last Name', $row['fl']); 
    $Email  = getValByName('Email', $row['fl']); 
    $Phone  = getValByName('Phone', $row['fl']); 
    $Mobile = getValByName('Mobile', $row['fl']); 

    echo $FirstName . ' ' . $LastName . '<br/>'; 
} 
+0

Danke moein-porkamel – Nyerere

+0

Froh, Ihnen zu helfen;) – MoeinPorkamel

0
schätzen

Dieser PHP-Code die Variable $data mit allen Zeilen als separate Daten

gibt
<?php 
$url = "my url"; 
$json = file_get_contents($url); 
$json = json_decode($json, true); 

$response = $json['response']; 
$result = $response['result']; 
$contacts = $result['Contacts']; 
$rows = $contacts['row']; 

$data = array(); 
foreach ($rows as $row) { 
    $fields = $row['fl']; 

    $tmp = array(); 
    foreach ($fields as $field) { 
     $tmp[$field['val']] = $field['content']; 
    } 

    array_push($data, $tmp); 
} 
var_dump($data); 
?> 

Die Daten, die zurückgegeben werden, sind für Ihr Beispiel JSON

array (size=3) 
    0 => 
    array (size=7) 
     'CONTACTID' => string '144120000000079041' (length=18) 
     'First Name' => string 'Tata' (length=4) 
     'Last Name' => string 'Nyerere' (length=7) 
     'Email' => string '[email protected]' (length=23) 
     'Account Name' => string 'null' (length=4) 
     'Phone' => string 'null' (length=4) 
     'Mobile' => string 'null' (length=4) 
    1 => 
    array (size=7) 
     'CONTACTID' => string '144120000000069001' (length=18) 
     'First Name' => string 'null' (length=4) 
     'Last Name' => string 'DANIEL NYERERE' (length=14) 
     'Email' => string '[email protected]' (length=25) 
     'Account Name' => string 'null' (length=4) 
     'Phone' => string 'null' (length=4) 
     'Mobile' => string 'null' (length=4) 
    2 => 
    array (size=8) 
     'CONTACTID' => string '144120000000065068' (length=18) 
     'First Name' => string 'null' (length=4) 
     'Last Name' => string 'Lawrence' (length=8) 
     'Email' => string '[email protected]' (length=23) 
     'ACCOUNTID' => string '144120000000065066' (length=18) 
     'Account Name' => string 'Zoho' (length=4) 
     'Phone' => string '1 888 900 9646' (length=14) 
     'Mobile' => string 'null' (length=4)