如何在PHP中取消设置值数组

时间:2018-09-21 04:15:34

标签: php arrays

这是我的数组:

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

如何取消设置值,如果不是在PHP中用单词表示,例如在此数组中 未设置值[],?

2 个答案:

答案 0 :(得分:3)

PHP具有一项功能,只需一个雄辩的呼吁就可以为您完成此任务,即获得清晰,直接的代码段。

array_diff()

迭代array_diff()中的每个子数组,并将值的“黑名单”用作&中的第二个参数。 $tracker声明中foreach()之前的$tracker使变量“可通过引用修改”-有效地,这意味着新数据将应用于原始数组的$array = [ "tracker" => [ ["[", "asiangames", ",", "asiangames2018", ",", "asian", "]"], ["[", "2019gantipresiden", "]"] ] ]; foreach ($array["tracker"] as &$tracker) { $tracker = array_diff($tracker, ["[", ",", "]"]); } var_export($array); ,并且不是原始输入数组的副本。

代码:(Demo

array (
  'tracker' => 
  array (
    0 => 
    array (
      1 => 'asiangames',
      3 => 'asiangames2018',
      5 => 'asian',
    ),
    1 => 
    array (
      1 => '2019gantipresiden',
    ),
  ),
)

输出:

<app-message [message]="message$ | async"></app-message>

答案 1 :(得分:1)

您需要使用foreach(),如下所示:-

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

输出:-{https://3v4l.org/1FaO1