删除第一个数组元素

时间:2017-05-01 10:48:14

标签: php arrays

我有一个像这样的数组结构:

conda

我想删除第一个索引,并从0重新索引数组。

我尝试了以下内容: $ newData = array_splice($ data,0,1);

但它提供以下输出

$data=    Array
    (
        [1] => Array
            (
                [A] => BANK
                [B] => IFSC
                [C] => MICR
                [D] => BRANCH
                [E] => ADDRESS
                [F] => CONTACT
                [G] => CITY
                [H] => DISTRICT
                [I] => STATE
            )

        [2] => Array
            (
                [A] => ABHYUDAYA COOPERATIVE BANK LIMITED
                [B] => ABHY0065001
                [C] => 400065001
                [D] => RTGS-HO
                [E] => ABHYUDAYA BANK BLDG., B.NO.71, NEHRU NAGAR, KURLA (E), MUMBAI-400024
                [F] => 25260173
                [G] => MUMBAI
                [H] => GREATER MUMBAI
                [I] => MAHARASHTRA
            )

        [3] => Array
            (
                [A] => ABHYUDAYA COOPERATIVE BANK LIMITED
                [B] => ABHY0065002
                [C] => 400065002
                [D] => ABHYUDAYA NAGAR
                [E] => ABHYUDAYA EDUCATION SOCIETY, OPP. BLDG. NO. 18, ABHYUDAYA NAGAR, KALACHOWKY, MUMBAI - 400033
                [F] => 24702643
                [G] => MUMBAI
                [H] => GREATER MUMBAI
                [I] => MAHARASHTRA
            )
    )

我要删除的数组部分是留下的部分,其余部分将被删除。

4 个答案:

答案 0 :(得分:1)

您可以使用array_slice($array, 1)

$result = array_slice($data, 1);
print_r($result);

答案 1 :(得分:0)

试试这个简单的...希望这会帮助你。我们在这里使用key函数,该函数将首先返回key,此处我们取消设置top index并返回array_values

Try this code snippet here     

unset($data[key($data)]);//unsetting the top index
$data=array_values($data);//returning array_values
print_r($data);

<强>输出:

Array
(
    [0] => Array
        (
            [A] => ABHYUDAYA COOPERATIVE BANK LIMITED
            [B] => ABHY0065001
            [C] => 400065001
            [D] => RTGS-HO
            [E] => ABHYUDAYA BANK BLDG., B.NO.71, NEHRU NAGAR, KURLA (E), MUMBAI-400024
            [F] => 25260173
            [G] => MUMBAI
            [H] => GREATER MUMBAI
            [I] => MAHARASHTRA
        )

    [1] => Array
        (
            [A] => ABHYUDAYA COOPERATIVE BANK LIMITED
            [B] => ABHY0065002
            [C] => 400065002
            [D] => ABHYUDAYA NAGAR
            [E] => ABHYUDAYA EDUCATION SOCIETY, OPP. BLDG. NO. 18, ABHYUDAYA NAGAR, KALACHOWKY, MUMBAI - 400033
            [F] => 24702643
            [G] => MUMBAI
            [H] => GREATER MUMBAI
            [I] => MAHARASHTRA
        )

)

答案 2 :(得分:0)

您可以使用array_shift()。使用数字键时,它们会自动重置,这就是您所需要的:

$first = array_shift($data);
// or just: array_shift($data);
var_dump($data);

答案 3 :(得分:0)

为什么不使用array_shift()。它从数组中删除第一个元素,并返回已删除元素的值:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a)."<br>";
print_r ($a);
?>

输出将是

red
Array ( [b] => green [c] => blue )