$replacedString = str_replace(array_keys($array1), $array1, $externalVariableString);

I have an entire array above that called $championToIdConversionArray, it has about 650 switches, it looks something like this

    $array1 = array(
        "1" => "Banana",
        "12" => "Monkey",
        "2" => "Apple",
        "121" => "Human"
    );

Now if I let script parse numbers "121", it won't say "Human", but "BananaAppleBanana". Could someone help with this? How could I force the small one-liner above, the swap only entire matches, not just parts of it?

This isn't actual example, as I said, I made PHP script which generated gigantic array saving me lots of time, it swaps ID's for names and it has around 700 positions. So instead of "Peter" I'm seeing "JackJoshJohn". Because Peter is let's say 123, and Jack, Josh and John are first in the array, before Peter.

Recommended Answers

All 2 Replies

Order them longest to shortest (descending order).

There's isn't a shorter way? If I'll sort them manually it will take ages, and... welp, it seems I'll need to rewrite the problem, but sort them first.

@edit:

$numbers = array(THE AWESOME CONTENT HERE);
sort($numbers);
var_dump($numbers);

I don't know what it did, but it did nothing, it didn't get sorted.

Still Googling on how to sort these arrays. I think PHP sorted them alphabetically, which they already were.

@edit2: ksort(); does sort by key and it worked, just need to find a way, to get it in descending order.

@edit3: They made entirely new function for that, krsort();, I don't get why wouldn't they add option to previous function. But well, it does it's job. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.