Convert data from complex array, with dictionary that is also a complex array


$example["matches"][0]["matchId"] = 3024554258;
$example["matches"][0]["champion"] = 120;
$example["matches"][0]["role"] = "none";

$example["matches"][1]["matchId"] = 3024554259;
$example["matches"][1]["champion"] = 120;
$example["matches"][1]["role"] = "none";

$example["matches"][2]["matchId"] = 3024554260; 
$example["matches"][2]["champion"] = 122; 
$example["matches"][2]["role"] = "none"; 

$dict[3];    // they do exist 
$dict[4];    // they do exist 
$dict[5];    // they do exist 
$dict[6];    // they do exist 

$dict[7]["id"] = 120; 
$dict[7]["name"] = "Namecian"; 
$dict[7]["title"] = "examplar"; 

$dict[8]["id"] = 121; 
$dict[8]["name"] = "Cianame"; 
$dict[8]["title"] = "nothingtoseehere"; 

$dict[9]["id"] = 122; 
$dict[9]["name"] = "Shakespear"; 
$dict[9]["title"] = "bookclub"; 

$dict[10];    // they do exist 
$dict[11];    // they do exist 

$i = 0; 

foreach($example["matches"] as $match) { 

    $result[$i]["champion"] = $match["champion"]; 
    // It will be int(120), then int(120), then int(122).

    /* Some magic here, I don't know what to execute. */

    echo "Match: " .$result[$i]["matchId"]. ", player with ID: " .$result[$i]["champion"]. ", is now named: " .$newName. ", and his title shall be: " .$newTitle; 
    // Expected:
    // Match: 3024554258, player with ID 120, is now named: Namecian, and his title shall be: examplar
    // Match: 3024554259, player with ID 120, is now named: Namecian, and his title shall be: examplar
    // Match: 3024554260, player with ID 122, is now named: Shakespear, and his title shall be: bookclub


I have extensive array, while I need to change values, using equally large dictionary. The script is part of way larger application. But this snippet alone represents the problem clearly. And solving this little thing, will solve application’s problem.

Of course I could run double foreach, but this would make couple ten-thousands comparisons and it will take seconds before user will receive basic response, which shouldn’t happen.

Aren’t there couple functions that can be just thrown in just like that and to have it work?

Where do you get your arrays from?

$example comes from remote API, I receive it through JSON.
$dict also comes from remote API, but I transform it and store in MySQL, and upon each request of $example I need to replace value according to the dictionary that I just cached.

Can you use array_search inside the loop, and will it be quick enough?

$x = array_search($result[$i]['champion'], array_column($dict, 'id'));
$newName = $dict[$x]['name'];
$newTitle = $dict[$x]['title'];

Or, could you arrange $dict[] so that instead of the structure you are using, you have it indexed on the ID column, like

$dict[121]["name"] = "Cianame";
$dict[121]["title"] = "nothingtoseehere";
$dict[122]["name"] = "Shakespeare";
$dict[122]["title"] = "bookclub";

// and then use it directly

$ch = $result[$i]['champion'];
$newName = $dict[$ch].["name"];
$newTitle = $dict[$ch].["title"];
1 Like

Oh it’s perfect. Not a single lag. It might as well be most optimal solution ever wouldn’t notice a difference. Thanks!

Good that it seems to work. Obviously you’ll need to have some code to deal with the id not appearing in the $dict array.

1 Like

What would $x return if ID doesn’t exist, false?

Correct, it would return FALSE. I think the second option might work more quickly, though that might just be in my head. It ‘seems’ as if just using the id as the array index would give quicker access, but I guess that internally PHP might just be doing its own search, the same as array_search() does. Perhaps it only matters when you have tens of thousands of entries in the dictionary.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.