-1

I am trying to replace an array value's spacing with hypen and then recollect back all values of the same array but with hypen in spaces.

INPUT:

$gFontsList = array("-1", "Agency FB", "28", "Aharoni Bold", "Bookshelf Symbol", "100", "Bookshelf Symbol", "111", "Browallia New Bol");

function toReplaceSpacing($gFontsListValues, $gFontsListIndex){
   if (gettype($gFontsListValues) === 'string'){
      if(preg_match('/ /',$gFontsListValues)){
        $gFontsListValues = str_replace(' ','-',$gFontsListValues);
        $gFontsChoiceOrder[] = $gFontsListValues;
      }
    } else {
      $gFontsChoiceOrder[] = $gFontsListValues;
    }
}
$gFontsChoiceOrder = array_map('toReplaceSpacing',$gFontsList);
print_r($gFontsChoiceOrder);

If I print it's just NULL. I don't know Why I am not getting resultant array?.

Common Man
  • 103
  • 2
  • 13
  • assumed `toReplaceSpacing` returns new `$gFontsListValues` – Deadooshka Jun 08 '17 at 17:31
  • I even tried returning `$gFontsChoiceOrder` but no result. – Common Man Jun 08 '17 at 17:34
  • Your function doesn't return anything. From what i can see all your code is doing is replacing spaces with hyphens, in which case you could just do `$gFontsChoiceOrder = array_map(function($v) { return str_replace(' ', '-', $v); }, $gFontsList);` – billyonecan Jun 08 '17 at 17:34

2 Answers2

1

Two issues:

function toReplaceSpacing($gFontsListValues){
   if (gettype($gFontsListValues) === 'string'){
        $gFontsListValues = str_replace(' ','-',$gFontsListValues);
   }
   return $gFontsListValues;
}
  1. You need to return the new value
  2. The callback only takes the value, so only one argument

Also, I don't see any reason to check for a space (especially with a regex) before replacing and it makes the code longer.

As billyonecan points out in the comments, it can be done with an anonymous function, but this version doesn't check for string so can cause issues with arrays, objects, etc:

$gFontsChoiceOrder = array_map(function($v) {
                                   return str_replace(' ', '-', $v);
                               }, $gFontsList);

For future reference, you can modify the original array with array_walk() and a reference:

function toReplaceSpacing(&$gFontsListValues){
   if (gettype($gFontsListValues) === 'string'){
        $gFontsListValues = str_replace(' ','-',$gFontsListValues);
   }
}
array_walk($gFontsList, 'toReplaceSpacing');
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

Remove that second parameter $gFontsListIndex and then just return a value gFontsListValues.

$gFontsList = array("-1", "Agency FB", "28", "Aharoni Bold", "Bookshelf Symbol", "100", "Bookshelf Symbol", "111", "Browallia New Bol");

function toReplaceSpacing($gFontsListValues){

   $gFontsChoiceOrder = array();

   if (gettype($gFontsListValues) === 'string'){

      if(preg_match('/ /',$gFontsListValues)){

        $gFontsListValues = str_replace(' ','-',$gFontsListValues);
      }
   }

   return $gFontsListValues;
}

$gFontsChoiceOrder = array_map('toReplaceSpacing',$gFontsList);
print_r($gFontsChoiceOrder);
heliosk
  • 1,113
  • 3
  • 23
  • 45