找到这个 Yii 2 查询中的位置
Find the position inside this Yii 2 query
我有以下 Yii 2 查询
$find = People::find()->where(['c_id' => $c_id])->orderBy('totals DESC, id DESC')->all();
想象一下这个查询是一个数组。此查询找到的所有内容都具有 "id" 属性。
因为它是按 "totals" 排序的,所以我基本上想要 return 数组中我可以找到这个特定 ID 的位置。
目前,我正在使用此代码。
foreach ($find as $t) {
$arr[] = $t->id;
if ($t->id == $id) {
break;
}
}
$key = count($arr);
return $key;
但是,此代码在 100k+ 结果查询上是任何方式。
有没有办法加快速度?
在 YII 中从查询中获取数组,你可以使用 queryAll();
$find = People::find()->where(['c_id' => $c_id])->orderBy('totals DESC, id DESC')->queryAll();
或者,另一种将对象转换为数组的方法是:
$find = json_decode(json_encode($find), true); // to convert all data into array.
一旦您获得数组中的结果,您就可以按照下面给出的要求实现实际代码。
您可以使用 array_search()
函数来获取您的值的索引。
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);
array_search() 函数在数组中搜索值和 returns 键。
您可以将结果作为数组(而不是对象)作为
$find = People::find()->where(['c_id' => $c_id])
->orderBy('totals DESC, id DESC')
->asArray()
->all();
然后您可以使用 array_search()
找到您的值
$my_index = array_search($id,$find);
但是对于 100k+ 你应该发现在 db 中使用直接 select...而不是在 php 上循环或在 php 中加载所有内容并使用 array_search 扫描()
也许我没有正确理解您的意思,但我假设您正在尝试检测返回的 array
中的 index
或 key
以获得您想要的 id
来自按 total
.
等其他列排序的 SQL
查询
所以让我们用您的查询从数据库中获取记录,稍作改动 asArray()
像这样
$find = People::find()
->where(['c_id' => $c_id])
->orderBy('totals DESC, id DESC')
->asArray()
->all();
在结果中,让我们假设 People
table returns 您的数组包含按列 total
和 id
[排序的以下数据集=22=].
[
0 => [
'id' => 2 ,
'c_id'=>2,
'name' => 'John' ,
'age'=>18,
'totals'=>100,
],
1=>[
'id'=>1,
'c_id'=>55,
'name'=>'Bob',
'age'=>20,
'totals'=>80,
],
2=>[
'id'=>3,
'c_id'=>85,
'name'=>'Peter',
'age'=>15,
'totals'=>75,
]
];
现在,如果您查看 \yii\helpers\ArrayHelper
,您会发现 ArrayHelper::getColumn()
。
让我们在从查询中收到的数组上使用它,我假设您正在 id
列内搜索 $id
因此我们将首先过滤掉 id
列如下所示。
$idsArray = ArrayHelper::getColumn($find, 'id');
这将按照以下顺序为我们提供 ID,该顺序与初始结果集的顺序相同。
[2,1,3]
然后让我们使用内置的 php
函数 array_search()
$key=array_search($yourId,$idsArray);
希望这就是您要找的。
我有以下 Yii 2 查询
$find = People::find()->where(['c_id' => $c_id])->orderBy('totals DESC, id DESC')->all();
想象一下这个查询是一个数组。此查询找到的所有内容都具有 "id" 属性。
因为它是按 "totals" 排序的,所以我基本上想要 return 数组中我可以找到这个特定 ID 的位置。
目前,我正在使用此代码。
foreach ($find as $t) {
$arr[] = $t->id;
if ($t->id == $id) {
break;
}
}
$key = count($arr);
return $key;
但是,此代码在 100k+ 结果查询上是任何方式。
有没有办法加快速度?
在 YII 中从查询中获取数组,你可以使用 queryAll();
$find = People::find()->where(['c_id' => $c_id])->orderBy('totals DESC, id DESC')->queryAll();
或者,另一种将对象转换为数组的方法是:
$find = json_decode(json_encode($find), true); // to convert all data into array.
一旦您获得数组中的结果,您就可以按照下面给出的要求实现实际代码。
您可以使用 array_search()
函数来获取您的值的索引。
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);
array_search() 函数在数组中搜索值和 returns 键。
您可以将结果作为数组(而不是对象)作为
$find = People::find()->where(['c_id' => $c_id])
->orderBy('totals DESC, id DESC')
->asArray()
->all();
然后您可以使用 array_search()
找到您的值$my_index = array_search($id,$find);
但是对于 100k+ 你应该发现在 db 中使用直接 select...而不是在 php 上循环或在 php 中加载所有内容并使用 array_search 扫描()
也许我没有正确理解您的意思,但我假设您正在尝试检测返回的 array
中的 index
或 key
以获得您想要的 id
来自按 total
.
SQL
查询
所以让我们用您的查询从数据库中获取记录,稍作改动 asArray()
像这样
$find = People::find()
->where(['c_id' => $c_id])
->orderBy('totals DESC, id DESC')
->asArray()
->all();
在结果中,让我们假设 People
table returns 您的数组包含按列 total
和 id
[排序的以下数据集=22=].
[
0 => [
'id' => 2 ,
'c_id'=>2,
'name' => 'John' ,
'age'=>18,
'totals'=>100,
],
1=>[
'id'=>1,
'c_id'=>55,
'name'=>'Bob',
'age'=>20,
'totals'=>80,
],
2=>[
'id'=>3,
'c_id'=>85,
'name'=>'Peter',
'age'=>15,
'totals'=>75,
]
];
现在,如果您查看 \yii\helpers\ArrayHelper
,您会发现 ArrayHelper::getColumn()
。
让我们在从查询中收到的数组上使用它,我假设您正在 id
列内搜索 $id
因此我们将首先过滤掉 id
列如下所示。
$idsArray = ArrayHelper::getColumn($find, 'id');
这将按照以下顺序为我们提供 ID,该顺序与初始结果集的顺序相同。
[2,1,3]
然后让我们使用内置的 php
函数 array_search()
$key=array_search($yourId,$idsArray);
希望这就是您要找的。