Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have this array $output which looks like this:

Array(
    [0] => Array(
        [0] => 1a
        [1] => 1b
        [2] => 1c
    )
    [1] => Array(
        [0] => 2a
        [1] => 2b
        [2] => 2c
    )
    [2] => Array(
        [0] => 3a
        [1] => 3b
        [2] => 3c
    )
    [3] => Array(
        [0] => 4a
        [1] => 4b
        [2] => 4c
    )
)

and so on...

When I want to remove the second element I just use:

$output = unset($output[1]);

to get the following:

Array(
    [0] => Array(
        [0] => 1a
        [1] => 1b
        [2] => 1c
    )
    [1] => Array(
        [0] => 3a
        [1] => 3b
        [2] => 3c
    )
    [2] => Array(
        [0] => 4a
        [1] => 4b
        [2] => 4c
    )
)

My question is how to remove every second element of every element in the array ([0][1], [1][1], [2][1], [3][1] ,...) to get the following:

Array(
    [0] => Array(
        [0] => 1a
        [1] => 1c
    )
    [1] => Array(
        [0] => 2a
        [1] => 2c
    )
    [2] => Array(
        [0] => 3a
        [1] => 3c
    )
    [3] => Array(
        [0] => 4a
        [1] => 4c
    )
)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
234 views
Welcome To Ask or Share your Answers For Others

1 Answer

Can't be done with a simple command, but you can use a loop:

foreach(array_keys($output) as $key) {
   unset($output[$key][1]);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...