0

I have array like this:

[0] => MFG Part Number: M402TM
[1] => SKU: dd32
[2] => Weight: 2.99lb/1.36kg
[3] => Warranty: 2

I want to have array like this:

[MFG Part Number] => M402TM
[SKU] => dd32
[Weight] => 2.99lb/1.36kg
[Warranty] => 2

And I did it using this code:

$new_array = array_reduce($normal_array, function ($c, $v){ 
    preg_match('/^([^:]+):\s+(.*)$/', $v, $m); 
    if(!empty($m[1])){
        return array_merge($c, array($m[1] => $m[2]));}
    else{
        return array();
    }
},[]);

This works great locally, but when I open my script on my server, there is a problem, maybe because of different version of PHP or something, I've got error like this

PHP Parse error: syntax error, unexpected '[' in /location.php on line 322

That is this part of code

},[]);

Any help? Maybe code that I'm using isn't good? Is there any replecment code for it?

YakovL
  • 7,557
  • 12
  • 62
  • 102

1 Answers1

0

If you cant upgrade php version, then use the old array() style:

$new_array = array_reduce($normal_array, function ($c, $v){ 
    preg_match('/^([^:]+):\s+(.*)$/', $v, $m); 
    if(!empty($m[1])){
        return array_merge($c, array($m[1] => $m[2]));}
    else{
        return array();
    }
},array());
Steffen Mächtel
  • 981
  • 8
  • 13