-1

This is my array:

array( 
    "tracker"=> array( 
            0=> array( 
                0=> "[" ,
                1=> "asiangames" ,
                2=> "," ,
                3=> "asiangames2018" ,
                4=> "," ,
                5=> "asian" ,
                6=> "]" 
            ),
            1=> array( 
                0=> "[" ,
                1=> "2019gantipresiden" ,
                2=> "]" 
            ) 
    ) 
) 

How to unset value, if not word in PHP for example in this array unset value [ and ] and ,, ??

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Ade
  • 154
  • 3
  • 16
  • https://stackoverflow.com/questions/7260468/php-unset-in-a-multidimensional-array – Alive to die - Anant Sep 21 '18 at 04:21
  • here array: array(1) { ["tracker"]=> array(2) { [0]=> array(7) { [0]=> string(1) "[" [1]=> string(10) "asiangames" [2]=> string(1) "," [3]=> string(14) "asiangames2018" [4]=> string(1) "," [5]=> string(5) "asian" [6]=> string(1) "]" } [1]=> array(3) { [0]=> string(1) "[" [1]=> string(17) "2019gantipresiden" [2]=> string(1) "]" } } } – Ade Sep 21 '18 at 04:26
  • 1
    @Ade How are you generating this array. I reckon I would fix the data earlier in your code. Show me, I'll help. – mickmackusa Sep 21 '18 at 04:42
  • 2
    Possible duplicate of [PHP removing values meeting specific conditon from array](https://stackoverflow.com/questions/35951715/php-removing-values-meeting-specific-conditon-from-array) – nageen nayak Sep 21 '18 at 04:54
  • @nageen As I have demonstrated in my answer, `array_diff()` is a superior/cleaner/more direct call to `array_filter()` or nested iterattions of `unset()`. – mickmackusa Sep 21 '18 at 04:56
  • @Ade In the future, please include your best coding attempt with your question. Normally, I wouldn't post an answer until a question includes code, but AlivetoDie sabotaged that stance by offering a solution that I found to be unrefined. I say this so that you can ask upvote-worthy questions in the future. – mickmackusa Sep 23 '18 at 10:46

2 Answers2

4

PHP has a function that does this task for you in one eloquent call for a clean, direct snippet.

array_diff()

Iterate each subarray in tracker and apply the "blacklist" of values as the 2nd parameter in array_diff(). The & before $tracker in the foreach() declaration makes the variable "modifiable by reference" -- effectively, this means that new data will be applied to the original array's $tracker and not a copy of the original input array.

Code: (Demo)

$array = [
    "tracker" => [
        ["[", "asiangames", ",", "asiangames2018", ",", "asian", "]"],
        ["[", "2019gantipresiden", "]"]
    ]
];

foreach ($array["tracker"] as &$tracker) {
    $tracker = array_diff($tracker, ["[", ",", "]"]);
}

var_export($array);

Output:

array (
  'tracker' => 
  array (
    0 => 
    array (
      1 => 'asiangames',
      3 => 'asiangames2018',
      5 => 'asian',
    ),
    1 => 
    array (
      1 => '2019gantipresiden',
    ),
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
1

You need to use foreach() like below:-

$remove_values = array('[',']',',');

foreach($your_array['tracker'] as &$value){
  foreach($value as $key=>$val){
     if(in_array($val,$remove_values)){
       unset($value[$key]);
     }
  }
}

print_r($your_array);

Output:-https://3v4l.org/1FaO1

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98