代码之家  ›  专栏  ›  技术社区  ›  Alix Axel

array_splice()-关联数组的数值偏移量

  •  1
  • Alix Axel  · 技术社区  · 16 年前

    我正在尝试做一些事情,但是我找不到任何解决方案,我也有一些困难,所以这里有一个示例代码,也许它足以证明我的目标:

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

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

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

    上面将从中移除并返回1个元素(第三个参数) $input (第一个参数),在偏移量2处(第二个参数),所以 $spliced 将保持价值 more .

    我要重复一遍 美元投入 对于foreach循环,我知道要拼接的键,但问题是我不知道它 数值偏移 从那时起 array_splice 只接受整数,我不知道该怎么做。

    一个非常枯燥的例子:

    $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() 但这是毫无意义的,因为它将返回关联索引….

    如何确定关联索引的数字偏移量?

    3 回复  |  直到 16 年前
        1
  •  4
  •   Gordon Haim Evgi    16 年前

    只是抓取和 unset 确定价值是一种更好的方法(也可能更快) 但是不管怎样,你可以一直数数

    $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
    )
    

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

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

    那么索引2是“baz”。但自从 array_slice 接受一个 抵消 ,这与数字键不同,它使用在数组中该位置找到的元素(按元素出现的顺序),这就是为什么计数有效。

    另一方面,如果数组中有数字键,您会得到有趣的结果,因为您是在测试相等性而不是标识。成功 $key === 'more' 相反,为了防止“更多”被打字。因为关联键是唯一的,所以也可以在找到“more”之后返回,因为检查后续键是没有意义的。但事实上:

    if(array_key_exists('more', $input)) unset($input['more']);
    
        2
  •  3
  •   Alix Axel    16 年前

    我找到了解决方案:

    $offset = array_search('more', array_keys($input)); // 2
    

    即使使用“有趣”的数组,例如:

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

    这是:

    echo '<pre>';
    print_r(array_keys($input));
    echo '</pre>';
    

    正确输出:

    Array
    (
        [0] => who
        [1] => what
        [2] => more
        [3] => when
        [4] => 0
        [5] => 1
        [6] => 2
    )
    

    这是一个微不足道的解决方案,但到达那里有些模糊。

    感谢大家的帮助。=)

        3
  •  1
  •   Markus    16 年前
    $i = 0;
    foreach ($input as $key => $value)
    {
        if ($key == 'more')
        {
            // Remove the index "more" from $input and add it to $result.
            $result[] = key(array_splice($input, $i , 1));
    
        }
        $i++;
    }