0

I've a muti dimensional array. I want to concatenate 2 strings separately for 2 array values and the 2 strings should not be concatenated for a single value. I want CM and PM concatenated 2 times any where in the array. I've tried looping the array and generating array_rand but i generates only once. Any help is much appreciated. Below is one example of what am achieving.

Thing am trying to achieve

  • Concatenate "PM" and "CM" string in one set of array and same value can't be CM and PM
  • Every array should have PM and CM concatenated
  • 1 "Name" value should have minimum 1 CM and PM and Maximum 2 CM and PM

For example: I've the below multi dimensional array.

Array
(
    [0] => Array
        (
            [0] => Name-A
            [1] => Name-B
            [2] => Name-C
            [3] => Name-4
            [4] => Name-5
        )

    [1] => Array
        (
            [0] => Name-A
            [1] => Name-B
            [2] => Name-C
            [3] => Name-4
            [4] => Name-5
        )

    [2] => Array
        (
            [0] => Name-A
            [1] => Name-B
            [2] => Name-C
            [3] => Name-4
            [4] => Name-5
        )

    [3] => Array
        (
            [0] => Name-A
            [1] => Name-B
            [2] => Name-C
            [3] => Name-4
            [4] => Name-5
        )

    [4] => Array
        (
            [0] => Name-A
            [1] => Name-B
            [2] => Name-C
            [3] => Name-4
            [4] => Name-5
        )
)

After concatenating

Array
(
    [0] => Array
        (
            [0] => Name-A
            [1] => Name-B
            [2] => Name-C["PM"]
            [3] => Name-4["CM"]
            [4] => Name-5
        )

    [1] => Array
        (
            [0] => Name-A
            [1] => Name-B["PM"]
            [2] => Name-C
            [3] => Name-4["CM"]
            [4] => Name-5
        )

    [2] => Array
        (
            [0] => Name-A["PM"]
            [1] => Name-B
            [2] => Name-C
            [3] => Name-4
            [4] => Name-5["CM"]
        )

    [3] => Array
        (
            [0] => Name-A["PM"]
            [1] => Name-B["CM"]
            [2] => Name-C
            [3] => Name-4
            [4] => Name-5
        )

    [4] => Array
        (
            [0] => Name-A
            [1] => Name-B
            [2] => Name-C["CM"]
            [3] => Name-4
            [4] => Name-5["PM"]
        )
)

3 Answers 3

2

Sorry I didn't understand at first. Given $your_array:

// Generate an array where the names are the key, and assign a zero value to PM (0) and CM (1) in a sub array
$ar = array_fill_keys($your_array[0], array (0 => 0, 1 => 0));

//$array the sub_value, $add the case PM ou CM, $exclusion is the key used the first time
function rand_in_array($array, $add, $ar, $exclusion)
{   
    // Select a random key
    $arr_key = array_rand($array, 1);

    
    if($ar[$array[$arr_key]][$add] < 2 && ($arr_key !=  $exclusion))
    {
        return $arr_key;
    } 
    return rand_in_array($array, $add, $ar, $exclusion);    
}

for($i=0; $i<count($your_array);$i++) 
{
    
    $arr_key_pm = rand_in_array($your_array[$i], 0, $ar, 99);
    $ar[$your_array[$i][$arr_key_pm]][0]++;     
    $arr_key_cm = rand_in_array($your_array[$i], 1, $ar, $arr_key_pm);
    $ar[$your_array[$i][$arr_key_cm]][1]++;     

    $your_array[$i][$arr_key_pm] .= "PM";
    $your_array[$i][$arr_key_cm] .= "CM";
}

It is ugly but it works :) Somewhere should existe someone able to make more aesthetic..

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this code. This code working fine. Just need one clarification.. any scope of adding a condtion to make sure that every name as minimum 1 CM and PM. Because Some name are shown as 0 for either CM/PM.
You could just replace 2 by 1 in the function. If you change the array for a larger one it could end in an infinite loop where the function will never find the case where no one has ever been unassigned. <b>EDIT: It does, don't use I make a new one!</b>
But if we want to change the min value to 2 instead of 1 then some of name counts has only 1 in it. any solution for that
If everyone has been once PM and once CM, there is non room for someone being selected twice with the array you provided.
0
for($i=0; $i<count($your_array);$i++) 
{
    $arr_keys = array_rand($your_array[$i], 2);
    $your_array[$i][$arr_keys[0]] .= "PM";
    $your_array[$i][$arr_keys[1]] .= "CM";
}

Should do the work.

1 Comment

This doesn't work.. Multiple times PM CM comes for one name and a person becomes PM CM many times where as max they can become CM and PM is 2.
0

I assume your inner array index is starting from zero. So generate the random index between 0 to your ( inner_array_size - 1). Then assign the value in the array's reference variable withing the loop.

foreach ($arr as &$value) {
    $randomIndex = array_rand(range(0, (count($value) -1) ), 2);
    $value[$randomIndex[0]] .= ' ["CM"]'; 
    $value[$randomIndex[1]] .= ' ["PM"]'; 
} 

Comments

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.