使用php unset引用foreach循环中的项

时间:2016-12-12 17:24:12

标签: php foreach

我从这样的API调用返回这个对象数组。注意$ result是一个数组数组,其中$ result [data]包含待办事项列表对象,结果[success]保持API调用状态:

        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..

我使用usort对数组进行排序,然后我想依靠“is_done”字段并按日期顺序将它们放在待办事项列表的底部。执行此操作的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]项目的所有变体都没有用。这可能很简单,但如果可能的话,我需要一些帮助。谷歌搜索和检查此网站没有显示在这种情况下未设置的示例。提前谢谢。

更新 在应用colburton的解决方案后,API现在返回此数据结构:

 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...

在通话结束时我做了

//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;
  } 

然后在主程序中我将$ result引用为

$result = $todo_items[0];

这就是现在发生致命错误的地方。

更新II: 想添加你需要重新索引数组

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

我读到here这是json_decode中的一个错误。谢谢你的帮助......

1 个答案:

答案 0 :(得分: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]);
        }
    }
}