doujiu3882 2016-12-12 17:24
浏览 101
已采纳

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

I return this array of objects from an API call like so. Note that $result is an array of arrays with $result[data] holding todo list objects and result[success] holding status of API call:

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

I sort the array with usort fine and then I want to resort on the "is_done" field and put them at bottom of todo list in date order. The php to do this is :

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

The problem I am having is my completed items are now at the end of the array but they are also still in their original position. The unset is not working. I have tried all variations on referencing the $result[data] item to no avail. This is probably something simple but I need some help if possible. Googling and checking this site shows no examples of unset in this type of situation. Thanks in advance.

Update: after applying colburton's solution the API is now returning this data structure:

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

At the end of the call i do a

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

and then in main program I reference $result as

$result = $todo_items[0];

And that is where fatal error occurs now.

Update II: Wanted to add that you then need to reindex the array

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

I read here that this is a bug in json_decode. Thanks for the help....

  • 写回答

1条回答 默认 最新

  • dongyi1996 2016-12-12 17:26
    关注

    Please use quotes around your array indices. This unsets what you want:

    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]);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题