array_splice() - 关联数组的数值偏移 [英] array_splice() - Numerical Offsets of Associative Arrays

查看:269
本文介绍了array_splice() - 关联数组的数值偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一些事情,但我找不到任何解决方案,我也有一些麻烦,把它变成作品所以这里是一个示例code,也许这将是足以证明我是什么M目标是:

I'm trying to do something but I can't find any solution, I'm also having some trouble putting it into works so here is a sample code, maybe it'll be enough to demonstrate what I'm aiming for:

$input = array
(
    'who' => 'me',
    'what' => 'car',
    'more' => 'car',
    'when' => 'today',
);

现在,我想用 array_splice() 从数组中删除(并返回)一个元素:

Now, I want to use array_splice() to remove (and return) one element from the array:

$spliced = key(array_splice($input, 2, 1)); // I'm only interested in the key...

以上将删除,并从 $输入(第一个参数)返回1元(第三个参数),偏移2(第二个参数),所以 $拼接将持有价值的 更多

The above will remove and return 1 element (third argument) from $input (first argument), at offset 2 (second argument), so $spliced will hold the value more.

我会遍历 $输入与foreach循环,我知道密钥进行拼接,但问题是我不知道它的数值偏移并从 array_splice 只接受整数,我不知道该怎么办。

I'll be iterating over $input with a foreach loop, I know the key to be spliced but the problem is I don't know its numerical offset and since array_splice only accepts integers I don't know what to do.

一个很平淡的例子:

$result = array();

foreach ($input as $key => $value)
{
    if ($key == 'more')
    {
        // Remove the index "more" from $input and add it to $result.
        $result[] = key(array_splice($input, 2 /* How do I know its 2? */, 1));
    }
}

我第一次使用,虽然 array_search() 但它是没有意义的,因为它会返回关联指数...

I first though of using array_search() but it's pointless since it'll return the associative index....

如何确定数字的一个关联指数的偏差?

推荐答案

就可以抓取和取消设置婷的值是一个更好的方法(可能得更快),但不管怎么说,你可以沿着

Just grabbing and unsetting the value is a much better approach (and likely faster too), but anyway, you can just count along

$result = array();
$idx = 0; // init offset
foreach ($input as $key => $value)
{
    if ($key == 'more')
    {
        // Remove the index "more" from $input and add it to $result.
        $result[] = key(array_splice($input, $idx, 1));
    }
    $idx++; // count offset
}
print_R($result);
print_R($input);

Array
(
    [0] => more
)
Array
(
    [who] => me
    [what] => car
    [when] => today
)

BUT 从技术上讲关联键没有数字索引。如果输入数组是

BUT Technically speaking an associative key has no numerical index. If the input array was

$input = array
(
    'who' => 'me',
    'what' => 'car',
    'more' => 'car',
    'when' => 'today',
    'foo', 'bar', 'baz'
);

则指数2为巴兹。但是,由于 array_slice 接受一个的偏置的,这是不一样的数字键,它使用在阵列中该位置找到的元件(以责令元素出现),这就是为什么沿作品计数。

then index 2 is "baz". But since array_slice accepts an offset, which is not the same as a numeric key, it uses the element found at that position in the array (in order the elements appear), which is why counting along works.

在阿里纳斯,在阵列中的数字键,你会得到有趣的结果,因为你正在测试的平等,而不是身份。让 $键===更多来代替prevent更多得到类型强制转换。由于联想钥匙都是独一无二的,你也可以返回多被发现后,因为后续的检查按键是没有意义的。但真正:

On a sidenote, with numeric keys in the array, you'd get funny results, because you are testing for equality instead of identity. Make it $key === 'more' instead to prevent 'more' getting typecasted. Since associative keys are unique you could also return after 'more' was found, because checking subsequent keys is pointless. But really:

if(array_key_exists('more', $input)) unset($input['more']);

这篇关于array_splice() - 关联数组的数值偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆