json-php赠送两次?

时间:2014-08-21 05:25:04

标签: php json

json数组中有关于login_ids的记录。我想在json-php解码函数中获取所需的结果。它在每行的顶部和底部给出了login_id两次。我有什么办法不在值归档中打印login_id?代码详情如下。

<?php
    $json = '{"records" : 
       [
            {
                "Name": "Jhon",
                "Age": "45",
                "Place": "Kettle",
                "Phone": "9834453",
                "log_id": "216" 
            },
            {
                "Name": "Bill",
                "Age": "41",
                "Place": "Ottava",
                "Phone": "4513453",
                "log_id": "215"         
        },
            {
               "Name": "James",
                "Age": "39",
                "Place": "Mexico",
                "Phone": "3456734",
                "log_id": "217" 
            },
        {
            "Name": "larry",
                "Age": "51",
                "Place": "New city",
                "Phone": "34890453",
                "log_id": "213" 
        }
        ]
    }';

$myjson = json_decode($json, true);  // decode the json string to an array
  foreach ( $myjson['records'] as $row ) {  // loop the records
            echo 'Details of  login id:&nbsp;'.  $row['log_id']. '<br/>';
            foreach ( $row as $field => $value ) {  // loop the fields
            echo  $field . ': '. $value . '<br>';
            }
         echo '<br/>'; 
    }
?>

还有任何简单的方法来打印所需的登录ID(即login_id =“217”),以便给出一个人的地址。

2 个答案:

答案 0 :(得分:0)

在打印前测试字段名称。

foreach ($row as $field => $value {
    if ($field != "log_id") {
        echo $field . ': ' . $value . '<br>';
    }
}

答案 1 :(得分:0)

遵循代码我会更好但没有经过测试。

foreach ( $myjson['records'] as $row ) {  
        if ($row['log_id'] =='216') { // login id to be checked
               echo 'Details of  login id:&nbsp;'.  $row['log_id']. '<br/>';
               foreach ( $row as $field => $value ) {  // loop the fields
                    if ($field != "log_id") { // hide the login id t bottom           
                           echo $field . ': ' . $value . '<br>';
                           }
              }
              echo '<br/>'; 
    }
 }