简体   繁体   中英

Remove Specific Elements from 2D Array

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

I have 2D array as of $arr[0][$i];

$i is the only part that increments, whereas 0 is fixed.

1. I need to remove element a and b via a search inside the array.

My attempt did not delete the elements:

$posts = array_diff($posts, array("a", "b"));

2. I would like to eliminate all duplicated values and leave only one ie removing all c's except one c.

The final output would be:

Array ( 
        [0] => Array (  
                      [0] => c
                      [1] => d
                     )
       ) 

I'm trying to figure it out, however, your help will be very much appreciated.

if you select the inner array it should work as you aspect it

$posts[0] = array_diff($posts[0], array("a", "b"));

The elimination should work that way

$post[0] = array_values(array_unique($posts[0]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM