2017-08-29 26 views
0

注意:嘗試在JSON

<?php 
 

 

 
$json= '{ 
 
     "fields" : 
 
     [ 
 
      { 
 
       "name" : "news_title", 
 
       "type" : "text", 
 
       "value" : "old title" 
 
      }, 
 
      { 
 
       "name" : "news_content", 
 
       "type" : "textarea", 
 
       "value" : "old content" 
 
      } 
 
     ]    
 
    }'; 
 
\t 
 
echo $json; 
 
    $jsonInPHP = json_decode($json,true); 
 
    $results = count($jsonInPHP['fields']); 
 
\t for ($r = 0; $r < $results; $r++){ 
 
    // look for the entry we are trying to find 
 
    if ($jsonInPHP->fields[$r]['name'] \t == 'news_title' 
 
    && $jsonInPHP->fields[$r]->value == 'old title'){ 
 
     // remove the match 
 
     unset($jsonInPHP->fields[$r]); 
 
    if(empty($jsonInPHP->fields[$r]->value)) 
 
\t { 
 
\t \t $jsonInPHP['fields'][$r]['name'] == 'news_title'; 
 
\t \t $jsonInPHP->fields[$r]->value=='no'; 
 
\t } 
 
     break; 
 
    } 
 
\t 
 
    } 
 
    function gog($status) 
 
    { 
 
\t  $results = count($status->fields); 
 
\t for ($r = 0; $r < $results; $r++){ 
 
\t \t $status->fields[$r]->value == 'old rr'; 
 
\t \t } 
 
    } 
 
    $jsonInPHP->fields = array_values($jsonInPHP->fields); 
 
\t echo json_encode($jsonInPHP); 
 

 

 

 

 
?>

我想從

「{ 「田」 搜索後改變以獲得非對象的財產:[{「名稱「:」news_title「,」type「:」text「,」value「:」old title「},{」name「:」news_content「,」type「:」textarea「,」value「:」old content「} ]}'

「{ 「田」:[{ 「Name」: 「news_title」, 「類型」: 「文本」, 「值」: 「我的新頭銜」},{「名稱「:」 news_content」, 「類型」: 「textarea的」, 「值」: 「我的新內容」}]}」的功能json_decode($json,true);json_decode第二個參數

+0

PHP中的第二個參數將對象轉換爲關聯數組,因此您不需要使用' - >',並且考慮一個'foreach'循環可能會更好。 – Script47

回答

0

檢查官方的文檔(在你的情況true )確定是否將結果轉換爲關聯數組。如果你想使用結果作爲對象將該值設置爲false或更好地省略。