Skip to content
Advertisement

array_unique() not working while I use preg_match_all php

my code is :

$a = <<<'EOD'
function makeItBIG($a_lot_of_names) {
  foreach($a_lot_of_names as $the_simpsons) {
    $BIG[] = strtoupper($the_simpsons);
  }
  return $BIG;
}

$a_lot_of_names = ['Homer', 'Marge', 'Bart', 'Maggy', 'Lisa'];
var_dump(makeItBIG($a_lot_of_names));
EOD;
$input_lines = $a;
preg_match_all('/$w+/', $input_lines, $output_array);
$output_array = array_unique($output_array);
$output_array = array_values($output_array);
echo json_encode($output_array);

why out put is stil duplicated ? out put is:

[["$a_lot_of_names","$a_lot_of_names","$the_simpsons","$BIG","$the_simpsons","$BIG","$a_lot_of_names","$a_lot_of_names"]]

not diffrent between using array_unique and not use it .

Advertisement

Answer

Because preg match output is like

Array
(
  [0] => Array
    (
      [0] => ain
      [1] => AIN
      [2] => ain
      [3] => ain
    )
)

so use zero index in unique

$output_array = array_unique($output_array[0]);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement