Php array question

Hello forums,

I have this array:

Array ( [Industrials] => 2 [Telecommunications] => 3 [Utilities] => 1 )

how do I make it to this:

Array (industrials,Industrials,Telecommunications,Telecommunications,Telecommunications,Utilities);

just repeat the keys inside an array based on their value.

Please help thanks

Nvm I did it via array_fill

Cheers

You would create a new array to hold the final result, and create a for loop to loop over your existing array. For each item in the existing array you would create another for loop based on the number, and push the key on to the new array.

Pseudo-code


existingArray = [...];
newArray = [];

foreach (existingArray as key => value) {
    for (i -> value) {
        newArray[] = key;
    }
}

So, what you need to do has been spelled out, and you have some fake pseudo-code that almost exactly replicates what you need to do to code up the solution.

You should be able to create the proper code from here.