Php 从数组中拉出第一个引用

Php 从数组中拉出第一个引用,php,arrays,sorting,Php,Arrays,Sorting,我有一个数组: Array ( [user1] => Array ( [id] => 1 [name] => 'john' [types] => null ) [user2] => Array ( [id] => 2 [name] => 'jane' [types] => Array( [t

我有一个数组:

Array
(
    [user1] => Array
    (
        [id] => 1
        [name] => 'john'
        [types] => null
    )
    [user2] => Array
    (
        [id] => 2
        [name] => 'jane'
        [types] => Array(
            [t_id] => 2
        )
    )
    [user3] => Array
    (
        [id] => 3
        [name] => 'jeff'
        [types] => null
    )
    [user4] => Array
    (
        [id] => 4
        [name] => 'steve'
        [types] => Array(
            [t_id] => 1
        )
    )
    [user5] => Array
    (
        [id] => 5
        [name] => 'rob'
        [types] => Array(
            [t_id] => 2
        )
    )
我需要找出t_id为1的第一个用户和t_id为2的第一个用户

在上面的例子中,Jane是第一个t_id为2的用户,Steve是第一个t_id为1的用户

我知道我可以在阵列中循环:

 private $tid1;
 private $tid2;

 foreach($data as $_v) {
     if($_v['types']['t_id'] === 1) $tid1 = $_v;
     if($_v['types']['t_id'] === 2) $tid2 = $_v;
 }
不过,这似乎效率低下,而且上述操作无法完全工作,因为循环将继续进行,稍后出现的t_id将替换变量


有更有效的方法吗?

找到td后,您可以停止循环:

foreach($data as $_v) {
 if(($_v['types']['t_id'] === 1) && !$tid1) $tid1 = $_v;
 if(($_v['types']['t_id'] === 2) && !$tid2) $tid2 = $_v;

 if($tid1 && $tid2) break;
}

您可以在找到td后停止循环:

foreach($data as $_v) {
 if(($_v['types']['t_id'] === 1) && !$tid1) $tid1 = $_v;
 if(($_v['types']['t_id'] === 2) && !$tid2) $tid2 = $_v;

 if($tid1 && $tid2) break;
}

若要修复循环解决方案并在第一次出现时停止,您可以检查是否已找到决策

 if(!$tid1 && $_v['types']['t_id'] === 1) $tid1 = $_v;

“foreach”作为解决方案通常足够有效。特别是在需要查找多个条件的情况下。

若要修复循环解决方案并在第一次出现时停止,您可以检查是否已找到决策

 if(!$tid1 && $_v['types']['t_id'] === 1) $tid1 = $_v;
“foreach”作为解决方案通常足够有效。尤其是在需要查找多个条件的情况下。

也许这会起作用

 foreach($data as $_v) {
   if(!$tid1 && isset($_v['types']['t_id']) && $_v['types']['t_id'] === 1) $tid1 = $_v;
   if(!$tid2 && isset($_v['types']['t_id']) && $_v['types']['t_id'] === 2) $tid2 = $_v;

   if ($tid1 && $tid2) break;
 }
也许这样行

 foreach($data as $_v) {
   if(!$tid1 && isset($_v['types']['t_id']) && $_v['types']['t_id'] === 1) $tid1 = $_v;
   if(!$tid2 && isset($_v['types']['t_id']) && $_v['types']['t_id'] === 2) $tid2 = $_v;

   if ($tid1 && $tid2) break;
 }
我的做法:

$users = [
    'user1' => [
        'id' => 1,
        'name' => 'john',
        'types' => null,
    ],
    'user2' => [
        'id' => 2,
        'name' => 'jane',
        'types' => [
            't_id' => 2
        ]
    ],
    'user3' => [
        'id' => 3,
        'name' => 'jeff',
        'types' => null
    ],
    'user4' => [
        'id' => 4,
        'name' => 'steve',
        'types' => [
            't_id' => 1
        ]
    ],
    'user5' => [
        'id' => 5,
        'name' => 'rob',
        'types' => [
            't_id' => 2
        ]
    ]
];

$first = false;
$second = false;

foreach ($users as $user) {
    if ($user['types'] != null) {
        if ($user['types']['t_id'] == 1 && $first === false) {
            $first = $user; //Steve
            continue;
        }

        if ($user['types']['t_id'] == 2 && $second === false) {
            $second = $user; //Jane
            continue;
        }
    }
  if($first && $second) 
      break;
}
echo '<pre>';
print_r($first);
print_r($second);
echo '</pre>';
$users=[
'user1'=>[
'id'=>1,
“name”=>“john”,
'types'=>null,
],
'user2'=>[
'id'=>2,
“name”=>“jane”,
“类型”=>[
't_id'=>2
]
],
'user3'=>[
'id'=>3,
“name”=>“jeff”,
“类型”=>null
],
'user4'=>[
'id'=>4,
'姓名'=>'史蒂夫',
“类型”=>[
't_id'=>1
]
],
'user5'=>[
“id”=>5,
'name'=>'rob',
“类型”=>[
't_id'=>2
]
]
];
$first=false;
$second=假;
foreach($users作为$user){
如果($user['types']!=null){
if($user['types']['t_id']==1&&$first==false){
$first=$user;//史蒂夫
继续;
}
if($user['types']['t_id']==2&&$second==false){
$second=$user;//Jane
继续;
}
}
如果($first&$second)
打破
}
回声';
打印(第一);
打印(秒);
回声';
我的方法:

$users = [
    'user1' => [
        'id' => 1,
        'name' => 'john',
        'types' => null,
    ],
    'user2' => [
        'id' => 2,
        'name' => 'jane',
        'types' => [
            't_id' => 2
        ]
    ],
    'user3' => [
        'id' => 3,
        'name' => 'jeff',
        'types' => null
    ],
    'user4' => [
        'id' => 4,
        'name' => 'steve',
        'types' => [
            't_id' => 1
        ]
    ],
    'user5' => [
        'id' => 5,
        'name' => 'rob',
        'types' => [
            't_id' => 2
        ]
    ]
];

$first = false;
$second = false;

foreach ($users as $user) {
    if ($user['types'] != null) {
        if ($user['types']['t_id'] == 1 && $first === false) {
            $first = $user; //Steve
            continue;
        }

        if ($user['types']['t_id'] == 2 && $second === false) {
            $second = $user; //Jane
            continue;
        }
    }
  if($first && $second) 
      break;
}
echo '<pre>';
print_r($first);
print_r($second);
echo '</pre>';
$users=[
'user1'=>[
'id'=>1,
“name”=>“john”,
'types'=>null,
],
'user2'=>[
'id'=>2,
“name”=>“jane”,
“类型”=>[
't_id'=>2
]
],
'user3'=>[
'id'=>3,
“name”=>“jeff”,
“类型”=>null
],
'user4'=>[
'id'=>4,
'姓名'=>'史蒂夫',
“类型”=>[
't_id'=>1
]
],
'user5'=>[
“id”=>5,
'name'=>'rob',
“类型”=>[
't_id'=>2
]
]
];
$first=false;
$second=假;
foreach($users作为$user){
如果($user['types']!=null){
if($user['types']['t_id']==1&&$first==false){
$first=$user;//史蒂夫
继续;
}
if($user['types']['t_id']==2&&$second==false){
$second=$user;//Jane
继续;
}
}
如果($first&$second)
打破
}
回声';
打印(第一);
打印(秒);
回声';

尝试使用
array\u walk
array\u unique
作为

$targetIds = array(3, 2, 1); // required list of ids
$result = array();

while (current($users) && !empty($targetIds)) {

    $cuKey = key($users);
    $cu = current($users);

    if (    !empty($cu['types']['t_id'])
         &&  in_array($cu['types']['t_id'], $targetIds)) { // process match

        $result[$cuKey] = $cu;
        $targetIds = array_diff($targetIds, array($cu['types']['t_id'])); // remove processed
    }

    next($users);
}

var_dump($result, $targetIds);

尝试使用
array\u walk
array\u unique
作为

$targetIds = array(3, 2, 1); // required list of ids
$result = array();

while (current($users) && !empty($targetIds)) {

    $cuKey = key($users);
    $cu = current($users);

    if (    !empty($cu['types']['t_id'])
         &&  in_array($cu['types']['t_id'], $targetIds)) { // process match

        $result[$cuKey] = $cu;
        $targetIds = array_diff($targetIds, array($cu['types']['t_id'])); // remove processed
    }

    next($users);
}

var_dump($result, $targetIds);

将“目标ID”放在一个数组中

  • 扫描$user数组并检查是否与任何“目标id”匹配
  • 如果匹配:添加到结果并从“目标ID”中删除
  • 如果“目标ID”列表为空,则停止
代码:


将“目标ID”放入数组中

  • 扫描$user数组并检查是否与任何“目标id”匹配
  • 如果匹配:添加到结果并从“目标ID”中删除
  • 如果“目标ID”列表为空,则停止
代码:


你能把
t\u id
作为
索引吗?例如,
Array([1]=>Array(..
)?能否将
t\u id
作为
索引?例如,
Array([1]=>Array(..
?这不起作用。如果在
t\u id==2
发生之前,您有两次出现
t\u id==1
,您将覆盖第一次。tid1可以替换为它的下一次出现。@jurgemaister我编辑了我的答案,以确保值不会在下一次出现时被替换。@panthro请查看这也“等于空字符串”。这不是我今天看到的最好的测试。这不起作用。如果在发生
t\u id==2
之前发生了两次
t\u id==1
,您将覆盖第一次。tid1可以替换为下一次出现的值。@jurgemaister我编辑了我的答案,以确保下次出现时值不会被替换。@panthro请也查看这一点“等于空字符串”。这不是我今天见过的最好的测试