0

Here is an example of test code. I wonder how to optimize this code, knowing that in my development code, the original array comes from an API that I retrieve, then that I transform according to my needs and calculations in a class with functions methods (there is a lot of transformation in $rep with ternary operators). The example here is for simplicity:

When you call the function testArray() several hundred times, the calculation time begins to become long, too long. I wonder if there's a way to optimize this and code more elegantly.

 $data = array(
        "lemon" => 'test1',
        "tomato" => 'test2',
        "cofee" => 'test3',
        "tree" => 'test4'
    );
    
    
    function testArray($data)
    {
    
        $rep = array(
            'yellow' => $data['lemon'],
            'red' => $data['tomato'],
            'brown' => $data['cofee'],
            'green' => $data['tree']
        );
        return $rep;
    }
    
    echo testArray($data)['yellow'];    // test1
    echo testArray($data)['red'];       // test2
    echo testArray($data)['brown'];     // test3
    echo testArray($data)['green'];     // test4

thank you, I'm stuck a bit to find a more efficient way.

3
  • Well, if I understand your question correctly, you left out the code which could be optimised. Commented Feb 1, 2022 at 13:07
  • I'm not sure of your exact use case but perhaps a 'generator' could be of use to you? php.net/manual/en/language.generators.overview.php Commented Feb 1, 2022 at 13:12
  • I don't know the generators, I will find out. Do you have an idea if not how to use it with my example? Commented Feb 1, 2022 at 13:33

1 Answer 1

2

You don't need to call the function multiple time. Just store it to a variable and use it later.

$data = array(
        "lemon" => 'test1',
        "tomato" => 'test2',
        "cofee" => 'test3',
        "tree" => 'test4'
    );
    
    
    function testArray($data)
    {
    
        $rep = array(
            'yellow' => $data['lemon'],
            'red' => $data['tomato'],
            'brown' => $data['cofee'],
            'green' => $data['tree']
        );
        return $rep;
    }
    
    $transformedData = testArray($data);
    echo transformedData['yellow'];    // test1
    echo transformedData['red'];       // test2
    echo transformedData['brown'];     // test3
    echo transformedData['green'];     // test4
Sign up to request clarification or add additional context in comments.

6 Comments

Wow, you just saw the thing that no one sees, ....... :-O
so obvious now that you say it
I hope that this answer gets 1 million upvotes
The calculation time has been divided almost by 10. Thank you very much. I suspected it was something stupid like that, I just didn't see it, like the nose in the middle of the face ^^
And even divided by 20 or 30. What took 15 seconds to display, only takes half a second after corrections. Like what, small change, big effect...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.