【问题标题】:Referencing an item in a foreach loop using php unset使用 php unset 在 foreach 循环中引用项目
【发布时间】:2016-12-12 17:24:12
【问题描述】:

我像这样从 API 调用返回这个对象数组。注意 $result 是一个数组数组,其中 $result[data] 保存待办事项列表对象,result[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];

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

更新二: 想补充一点,你需要重新索引数组

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

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

【问题讨论】:

    标签: php foreach


    【解决方案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]);
            }
        }
    }
    

    【讨论】:

    • 谢谢。但现在我在我的 todo_client 程序的第 91 行收到一个关于“致命错误:不能使用 stdClass 类型的对象作为数组”的错误,我将 $ result 引用为“$result = $todo_items[0];”在此之前我使用 json_decode 。我在某处读到了关于何时删除项目并使用 json_decode 你必须重新索引数组?
    • 您可以使用array_values“重新索引”。例如。 $result['data'] = array_values($result['data']);在循环之后。
    猜你喜欢
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    相关资源
    最近更新 更多