简体   繁体   English

根据内部值将数组中的特定索引移到最前面

[英]Shifting specific index's in array to front based on value inside

I have the following array. 我有以下数组。

{
  "flow":[
    {
      "tasks":[
        { "id":"1", "uid":"bryan" },
        { "id":"2", "uid":"eric" }
      ]
    },
    {
      "tasks":[
        { "id":"1", "uid":"bryan" },
        { "id":"2", "uid":"eric" }
      ]
    },
    {
      "tasks":[
        { "id":"1", "uid":"bryan" },
        { "id":"2", "uid":"eric" }
      ]
    },
    {
      "tasks":[
        { "id":"1", "uid":"bryan" },
        { "id":"2", "uid":"eric" },
        { "id":"3", "uid":"eric" },
        { "id":"4", "uid":"bryan" }
      ]
    }
  ]
}

Let's say I am eric . 假设我是埃里克 If I am eric, I am trying to move all items with the uid of eric to the front of the tasks array. 如果我是eric,则尝试将带有ericuid的所有项目移动到tasks数组的前面。

So that the array would end looking like this: 这样数组将看起来像这样:

{
  "flow":[
    {
      "tasks":[
        { "id":"2", "uid":"eric" },
        { "id":"1", "uid":"bryan" }
      ]
    },
    {
      "tasks":[
        { "id":"2", "uid":"eric" },
        { "id":"1", "uid":"bryan" }
      ]
    },
    {
      "tasks":[
        { "id":"2", "uid":"eric" },
        { "id":"1", "uid":"bryan" }
      ]
    },
    {
      "tasks":[
        { "id":"2", "uid":"eric" },
        { "id":"3", "uid":"eric" },
        { "id":"1", "uid":"bryan" },
        { "id":"4", "uid":"bryan" }
      ]
    }
  ]
}

I've attempted to make a function to do it, but for some reason it's not working the way I intended it to. 我试图创建一个函数来执行此操作,但是由于某种原因,它无法按我预期的方式运行。 Does anyone know what I'm doing wrong? 有人知道我在做什么错吗?

function reorder_flow($flow, $uid)
{   
    foreach($flow as &$step)
    {   
        //step is the array with tasks
        $tasks = $step['tasks'];
        $new_tasks = array();

        foreach($tasks as $key => $t)
        {
            if($t['uid'] == $uid)
            {
                $new_tasks = $new_tasks + $t;
                unset($tasks[$key]);
            }
        }
        $step['tasks'] = $new_tasks + $step['tasks'];
    }
    return $flow;
}

This is a traversal/sorting problem. 这是遍历/排序问题。

$json = <<<JSON
{
  "flow":[
    {
      "tasks":[
        { "id":"1", "uid":"bryan" },
        { "id":"2", "uid":"eric" }
      ]
    },
    {
      "tasks":[
        { "id":"1", "uid":"bryan" },
        { "id":"2", "uid":"eric" }
      ]
    },
    {
      "tasks":[
        { "id":"1", "uid":"bryan" },
        { "id":"2", "uid":"eric" }
      ]
    },
    {
      "tasks":[
        { "id":"1", "uid":"bryan" },
        { "id":"2", "uid":"eric" },
        { "id":"3", "uid":"eric" },
        { "id":"4", "uid":"bryan" }
      ]
    }
  ]
}
JSON;

$flow = json_decode($json, true);

array_walk($flow['flow'], function(&$flowItem, $key){
    usort($flowItem['tasks'], function($a, $b){
        $aIsMatch = $a['uid'] === 'eric';
        $bIsMatch = $b['uid'] === 'eric';
        if ($aIsMatch && $bIsMatch) {
            return 0;
        }
        if ($aIsMatch) {
            return -1;
        }
        if ($bIsMatch) {
            return 1;
        }
        // Fallback sorting criteria - you could use something else or just return 0
        return strcasecmp($a['uid'], $b['uid']);
    });
});


echo json_encode($flow, JSON_PRETTY_PRINT);

Please try this: 请尝试以下方法:

function sortByUID(&$array, $uid) {

    array_walk ($array, function($obj) use($uid) {
        uasort($obj->tasks, function($a, $b) use($uid)  {
            return $a->uid == $uid ? -1 : 1;
        });
    }); 

}

sortByUID($data->flow, 'eric');

Demo: https://3v4l.org/mOhDX 演示: https : //3v4l.org/mOhDX

I hope this will help. 我希望这将有所帮助。

notes: 笔记:

  • + does not work with arrays in that way +不适用于数组

  • json_decode should have the second parameter set to true to get nested arrays json_decode应该将第二个参数设置为true以获取嵌套数组

then try this in the foreach: 然后在foreach中尝试:

//step is the array with tasks
$new_tasks=array();
foreach($step['tasks'] as $t){
   if($t['uid'] == $uid){
      array_unshift($new_tasks,$t);
   } else {
      array_push($new_tasks,$t);
   }
}

$step['tasks'] = $new_tasks;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM