php - new array be joining 2 arrays at random

459

Dear stackoverflow members, I want to join 2 arrays in random as mentioned below, i tried different methods but i was not able to produce the result that i exactly wanted.

I have 2 arrays which looks like this. Lets call this names.

Array
(
    [bill] => Array
        (
        )

    [steve] => Array
        (
        )

    [Ritchie] => Array
        (
        )

)

now these names are generated from another function, it's output looks something like this.

Array
(
    [1] => Array
        (
            [email] => [email protected]
            [name] => bill
            [web] => http://bill.com
        )

    [2] => Array
        (
            [email] => [email protected]
            [name] => steve
            [web] => http://steve.com
        )

    [3] => Array
        (
            [email] => [email protected]
            [name] => Ritchie
            [web] => http://linux.com
        )

    [4] => Array
        (
            [email] => [email protected]
            [name] => Ritchie
            [web] => http://linux.com
        )

)

and the 2nd array, lets call it countries.

Array
(
    [0] => USA
    [1] => UK
    [2] => Netherlands
    [3] => Australia
    [4] => Germany
)

Now here is the exact problem. i want the 1st array names and the 2nd array countries to be joined and form another associative array in random order. But please note that the function which returns the name array returns the key : Ritchie twice, it has 2 occurrences. So the final array should be something like this.

Array
(

    [Ritchie] => Array
        (
            [0] => USA,
            [1] => Germany
        )

    [bill] => Array
        (
            [0] => Netherlands
        )

    [steve] => Array
        (
            [0] => UK
        )

)

Since the key Ritchie has 2 occurrences, 2 unique values from the country array should be added. The number of keys or occurrences in names and the keys in countries will always be the same. If their is anything unclear, let me know i'll clear it out.


I did heavy research on the internet and stackoverflow and i was only able to come up with this. The current solution i have is this. Please be kind to help me to improve the current lines of code to suit my need or may be this might not be elegant, if so, please be kind to suggest a better solution.

$jumble = array();
  foreach ($names as $name) {
  $jumble[$name] = $countries[array_rand($countries)];
}

Thank you.

940

Answer

Solution:

Try this code:

shuffle($countries);
$n = count($countries); $i = 0;
$jumble = array();
foreach ($names as $name) {
    if (!isset($jumble[$name])) $jumble[$name] = array();
    $jumble[$name][] = $countries[$i++ % $n];
}

Demo

People are also looking for solutions to the problem: Why this regex is not greedy in PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.