Search code examples
phparrayslaravelcollectionseval

Dinamic parsing variables to array_map


I have this function

    public function saveVariantValue($id, $data, $langs, $field, $client)
    {
        $transformation = [];
        $parts1 = explode(';', $data[$field][1]);
        $parts2 = explode(';', $data[$field][2]);

        $tmp_arr = array_map(null, $parts1, $parts2);
        $transformation[] = array_map(
            function($v) { return array_combine(["1","2"], $v); }, 
            $tmp_arr
        );
        //here i use the transformation array//
    }

the function transform an input array from this,

[
  {
      "1": "tag_es1;tag_es2;tag_es3",
      "2": "tag_en1;tag_en2;tag_en3"
  }
]

to this

[
  {
    {
      "1" : "tag_es1",
      "2" : "tag_en1"
    },
    {
      "1" : "tag_es2",
      "2" : "tag_en2"
    },
    {
      "1" : "tag_es3",
      "2" : "tag_en3"
    }
  }
]

each 1 and 2 represent a language id from the database, if you check the code, it will only work if i send both languages, but in some cases based on the users permission, the user will be able to send

[
  {
      "1": "tag_es1;tag_es2;tag_es3"
  }
]

i want to make the code mode dynamically so instead of declaring parts1 and parts2 i could create a string of values and then eval the string like this

$str  = '';
$eval = '';
foreach($langs as $l) {
    //i create the string "variables", the values of the "parts" must be an array, that's why the explode
    $str  .= 'parts'.$l->id.'='.serialize(explode(";", $variant[$l->id])).';&';
    //i need to pass the "parts" to array_map so i create a string to pass it as the function parameters;
    $eval .= '$parts'.$l->id.', ';
}
$str  = rtrim($str, '&');
$eval = rtrim($eval, ', ');
parse_str($str);
//if i var_dump $parts1 or $parts2 i get the correct values
$tmp_arr = array_map(null, eval($eval));//here i fail

so far when i call array_map it fails throwing syntax error, unexpected end of file

so here goes the question, how can i make this code work? and it is ok to do this? is there a better way of doing it?


Solution

  • Read about variadic arguments and use this:

    $a = '1,2,3';
    $b = '4,5,6';
    $c = '7,8,9';
    $r = [1 => $a, 2 => $b, 3 => $c];
    
    $parts = array_map(function($v) { return explode(',', $v); }, $r);
    
    print_r(array_map(null, ...$parts));