0

i have problems with part of my code. In my local machine this code works, i have php7

$totals = array_values(
    array_count_values(
        array_map(function($x)
        {
            return explode('-', $x)[0];
        }, $arraySKU2)
    )
);

This is the error that i get on server that have php version 5.3.3.

Parse error: syntax error, unexpected '[' in

I tried to find solution on internet, but i didnt make it. Can someone help me please, i dont know how to write changes?

1 Answers1

2

This is not working:

 return explode('-', $x)[0];

Try assiging the result of explode to a variable and access the first element from that variable.

According to the release notes for PHP 5.4, accessing data this way has not been possible in versions older than 5.4.0

Finally, your code could look like this:

$totals = array_values(array_count_values(array_map(function ($x) {
    $explodedValues = explode('-', $x);
    return $explodedValues[0];
}, $arraySKU2)));
Nico Haase
  • 6,670
  • 34
  • 28
  • 48
  • I agree, though I don't think it would cause the error provided, though I guess that could be anywhere. – Jonnix Jan 22 '19 at 15:04
  • How i can do that? –  Jan 22 '19 at 15:06
  • Yes error is in this part [0] –  Jan 22 '19 at 15:06
  • @dokica I've added the proper code for that – Nico Haase Jan 22 '19 at 15:08
  • 1
    Thanks, its working! –  Jan 22 '19 at 15:11
  • Questions that are about basic PHP syntax errors should not be answered. They should be closed as a duplicate of [PHP Parse/Syntax Errors; and How to solve them?](//stackoverflow.com/questions/18050071) only. If you want to point out their mistake, just post a comment. See: [Should one advise on off topic questions?](//meta.stackoverflow.com/q/276572/1768232). Off-topic questions can be closed and deleted, which could nullify your contribution anyway. – John Conde Jan 24 '19 at 00:46