Php 从数组对象按键获取上一个和下一个数组
我有一个数组对象,希望通过特定键获得2个上一个和2个下一个数组组Php 从数组对象按键获取上一个和下一个数组,php,arrays,Php,Arrays,我有一个数组对象,希望通过特定键获得2个上一个和2个下一个数组组 Array ( [467] => stdClass Object ( [id] => 467 [user_id] => 1 ) [468] => stdClass Object ( [id] => 468 [user_id] => 1
Array
(
[467] => stdClass Object
(
[id] => 467
[user_id] => 1
)
[468] => stdClass Object
(
[id] => 468
[user_id] => 1
)
[469] => stdClass Object
(
[id] => 469
[user_id] => 1
)
[474] => stdClass Object
(
[id] => 474
[user_id] => 1
)
[475] => stdClass Object
(
[id] => 475
[user_id] => 1
)
[479] => stdClass Object
(
[id] => 479
[user_id] => 1
)
[480] => stdClass Object
(
[id] => 480
[user_id] => 1
)
)
如果键define 474将产生:
- 键469和468中的上一个数组组
- 键475和479中的下一个数组组
- 如果没有上一个和下一个数组,我不想要结果
$val = 474;
$currentKey = array_search($val, $array);
$before = (isset($array[$currentKey - 2])) ? $array[$currentKey - 2] :
$after = (isset($array[$currentKey + 2])) ? $array[$currentKey + 2] : $array[0];
var_dump($before, $after);
请提供帮助。我的方法是搜索
$key
值,并在数组中返回其偏移量。您对输入数组的值使用了array\u search()
然后,如果偏移量
值不为false,我将尝试从输入数组中切片5个所需的子数组。如果它没有返回5,那么它将失败
如果子阵列的集合小于5,则第二个代码不会触发故障
代码:()
下面是第二种方法的视觉表现和更多说明:
以下说明使用6元素数组来演示计算
I = 'elements labeled by their indices'
S = 'the slice'
T = 'target index'
L = 'length of slice'
I ST ST ST ST ST ST When $target index is:
0 ╗0 ╗ ╗ 0, then $offset=0 and $length=3
1 ║ ║1 ║ ╗ 1, then $offset=0 and $length=4
2 ╝ ║ ║2 ║ ╗ 2, then $offset=0 and $length=5
3 ╝ ║ ║3 ║ ╗ 3, then $offset=1 and $length=5
4 ╝ ║ ║4 ║ 4, then $offset=2 and $length=4
5 ╝ ╝ ╝5 5, then $offset=3 and $length=3
L: 3 4 5 5 4 3
由于数组不在序列中,请尝试使用此数组
如果目标元素之前或之后没有两个可用元素,您希望发生什么?我们是在“24小时”工作吗?“不工作”是因为出现错误或提供的结果不正确?还是没有结果?我不想要结果。请将相关细节作为编辑添加到您的问题中。你是说你想让整个事情失败?或者您可以接受1个前导元素和2个尾随元素吗?这将确定要调用的正确函数。好的,谢谢,我添加了如果没有上一个和下一个数组,我不想要结果
我想你应该检查偏移量是否小于2。很好的代码,但如果键define468
或479
@optional现在您希望它返回的子数组可能少于5个,那么如何使其仍然得到结果呢?如果你愿意,我可以这样编码。只有在我或其他人需要的情况下才有可能。让我们来看看。$next2变量有一个小的修正。出了差错。在上一次编辑中,它被更正了。在这个答案中有很多不合理的地方。您在每次迭代中都过度写入和空检查变量。条件句太多了。这是令人费解的,不必要的。任何人都不应该使用这种方法。绝对不是最佳实践。你甚至都懒得解释。@mickmackusa你有没有用468和479检查你的答案?你的代码给出了错误的答案。在我的回答中,只有两个变量被忽略了。你是对的。我用第二个脚本修复了那些$offset
和$length
问题,当OP扩展问题要求时,我匆忙编写了第二个脚本。
Fail
---
array (
0 =>
stdClass::__set_state(array(
'id' => 475,
'user_id' => 1,
)),
1 =>
stdClass::__set_state(array(
'id' => 479,
'user_id' => 1,
)),
2 =>
stdClass::__set_state(array(
'id' => 480,
'user_id' => 1,
)),
)
I = 'elements labeled by their indices'
S = 'the slice'
T = 'target index'
L = 'length of slice'
I ST ST ST ST ST ST When $target index is:
0 ╗0 ╗ ╗ 0, then $offset=0 and $length=3
1 ║ ║1 ║ ╗ 1, then $offset=0 and $length=4
2 ╝ ║ ║2 ║ ╗ 2, then $offset=0 and $length=5
3 ╝ ║ ║3 ║ ╗ 3, then $offset=1 and $length=5
4 ╝ ║ ║4 ║ 4, then $offset=2 and $length=4
5 ╝ ╝ ╝5 5, then $offset=3 and $length=3
L: 3 4 5 5 4 3
$arr = array( 467 => (object) ['id' => 467, 'user_id' => 1],
468 => (object) ['id' => 468, 'user_id' => 1],
469 => (object) ['id' => 469, 'user_id' => 1],
474 => (object) ['id' => 474, 'user_id' => 1],
475 => (object) ['id' => 475, 'user_id' => 1],
479 => (object) ['id' => 479, 'user_id' => 1],
480 => (object) ['id' => 480, 'user_id' => 1],);
$find = 474;
$before2 = $before1 = $next1 = $next2 = array();
$flag = false;
foreach ($arr as $key => $val) {
if($key == $find) {
$flag = true;
}
if(!$flag) {
if(!empty($before1)){
$before2 = $before1;
}
$before1 = $val;
}
if($key != $find) {
if($flag && empty($next2)){
if(!empty($next1)){
$next2 = $next1;
}
$next1 = $val;
}
if(!empty($next2)){
break;
}
}
}
if($flag) {
echo "matching values =>";
var_dump($before2);
var_dump($before1);
var_dump($next1);
var_dump($next2);
} else {
echo "given index not found!";
}