2016-12-12 5 views
0

私はこのようなAPI呼び出しからこのオブジェクトの配列を返します。その$結果(注)$結果と配列の配列である[データ] ToDoリストのオブジェクトを保持し、その結果、[成功] API呼び出しの状態保持:私はusort罰金で配列をソートして、私が頼るたいphp unsetを使ってforeachループ内の項目を参照する

 array(9) { [0]=> object(stdClass)#3 (5) { ["todo_id"]=> string(10) "1480430478" ["title"]=> string(13) "Check Printer" ["description"]=> string(8) 
    "Room 233" ["due_date"]=> string(10) "11/29/2016" ["is_done"]=> string(4) "true" } [1]=> object(stdClass)#4 (5) { ["todo_id"]=> string(10) "148043046" ["title"]=> string(18) "Doctor Appointment" ["description"]=> string(7) 
    "@4pm. " ["due_date"]=> string(10) "11/30/2016" ["is_done"]=> string(4) "true" } 
     etc.. 

を"is_done"フィールドに入力し、日時順にtodoリストの一番下に置きます。これを行うにはPHPは次のとおりです。私が午前

//Sort by is_done 
    foreach($result[data] as $arrayElement) { 
     foreach($arrayElement as $valueKey => $value) { 
      if(($valueKey == 'is_done') && ($value == 'true')){ 
       $temp = $arrayElement; 
      //delete this particular object from the $array 
       unset($result[data][$arrayElement]); 
       array_push($result[data], $temp); 
      } 
     } 
    } 

問題は私の完了した項目が配列の末尾になりましたですが、彼らは元の位置のままでもあります。設定が解除されていません。私は$ result [data]項目を参考にして、すべてのバリエーションを試してみました。これはおそらく単純なものですが、可能であれば助けが必要です。このサイトをグーグルでチェックしてみると、このような状況では未設定の例はありません。前もって感謝します。

アップデート:私は、メインプログラムで

//json_decode the result 
     $result = @json_decode($result); 

     //check if we're able to json_decode the result correctly 
     if ($result == false || isset($result->success) == false) { 
      throw new Exception('Request was not correct'); 
     } 

     //if there was an error in the request, throw an exception 
     if ($result->success == false) { 
      throw new Exception($result['errormsg']); 
     } 

     //if everything went great, return the data 
     return $result->data; 
    } 

、その後の操作を行い、コールの終了時

object(stdClass)#3 (6) { ["2"]=> object(stdClass)#4 (5) { ["todo_id"]=> int(1480698596) ["title"]=> string(7) "Test #4" ["description"]=> string(4) "test" ["due_date"]=> string(10) "12/02/2016" ["is_done"]=> string(5) "false" } ["3"]=> object(stdClass)#5 (5) { ["todo_id"]=> string(10) "1480617975" ["title"]=> string(13) "Check Printer" ["description"]=> string(4) 
"Test" ["due_date"]=> string(10) "12/06/2016" ["is_done"]=> string(5) "false" } ["5"]=> object(stdClass)#6 (5) { ["todo_id"]=> int(1481136023) ["title"]=> string(9) "Todo item" ["description"]=> string(7) "test123" ["due_date"]=> string(10) "01/19/2017" ["is_done"]=> string(5) "false" } etc... 

APIは現在、このデータ構造を返しているcolburtonの溶液を塗布した後、私は$結果を

$result = $todo_items[0]; 
として参照しています

これは、ここで致命的なエラーが発生する場所です。

アップデートII: は、あなたがその配列

$result['data'] = array_values($result['data']); 

のインデックスを再作成する必要があることを追加したい私は、これはjson_decodeのバグであることhereをお読みください。助けてくれてありがとう....

答えて

1

あなたの配列インデックスの引用符を使ってください。これはあなたが望むものをセットしません:

foreach ($result['data'] as $idx => $arrayElement) { 
    foreach ($arrayElement as $valueKey => $value) { 
     if (($valueKey == 'is_done') && ($value == 'true')) { 
      $temp = $arrayElement; 
      //delete this particular object from the $array 
      array_push($result['data'], $temp); 
      unset($result['data'][$idx]); 
     } 
    } 
} 
+0

ありがとう。しかし、私は "$ result = $ todo_items [0];と$ resultを参照している私のtodo_clientプログラムの91行目で、"致命的なエラー:配列として型stdClassのオブジェクトを使うことはできません "私はこれの前にjson_decodeを使います。私はどこかで項目を削除し、json_decodeを使って配列を再インデックスする必要があります。 – alan

+0

array_valuesを使用して「再インデックスする」ことができます。例えば。 $ result ['data'] =配列の値($ result ['data']);ループの後に。 – colburton

関連する問題