Jump to content

What is the best way, of replacing array values?


Recommended Posts

Hi,

I have 2 arrays, named $old_teams1 and $old_teams2.

The arrays have from 6 values upwards. If $old_teams1 looks like this:

Array ( 
[0] => 3 
[1] => 4
[2] => 3 
[3] => 4 

[4] => 1 
[5] => 2 
)

and $old_teams2 is the has the same mixture of values from 1-4.....

.. How do I create a loop that will change all values of $old_teams1 and $old_teams2 to new values such as, 1->3, 2->4, 3->1, 4->2.... (The new values have been randomly generated).

 

 

 

 

function convertArrayValues($value)
{
    switch($value)
    {
        case 1:
            return 3;
        case 2:
            return 4;
        case 3:
            return 1;
        case 4:
            return 2;
    }
}

$new_teams1 = array_map($old_teams1, 'convertArrayValues')

Ok, now I'm trying to apply this, from the random array that I have generated...

 

so...  for example... 

$non_rand_arr  is an array of...

[0] => 1 
[1] => 2
[2] => 3 
[3] => 4

etc... (increasing with count())

and...

$rand_arr could be

[0] => 2 
[1] => 4
[2] => 1 
[3] => 3

 

The arrays of numbers to be changed are $old_teams1 and $old_teams2....

which can be any one of the numbers in the range of the $non_rand_arr... in this case 1-4, although it could be 1-9++...

 

so, would I use a for loop to convert each of the $old_teams1 and $old_teams2 to its new values, with the switch()... as demonstrated?

Sorry, I've tried to understand what you are wanting. But, apparently there is a communication failure here (and it is not my failure). You previously stated that the requirements were to change the values as follows:

1 changes to 3

2 changes to 4

3 changes to 1

4 changes to 2

 

Now, you are saying that the numbers can be in any range. So, what would a 5 change to, or a 6, etc. If you cannot explain what you need I cannot help. You provided requirements based upon the range of numbers being fixed from 1 - 4. If the range of number is not fixed you must provide an explanation of how the transformations will be decided based upon the range used.

 

I'm really trying to understand you, but you are not making it easy and I'm tempted to bail on this thread. I'm sorry if you find this rude, but I come here to help people and when I feel the people asking for help are not taking the time to at least provide clear/concise information I feel disrespected.

Hi.

I made a poor job of explaining my last post, which caused some frustration, so this time I will try to explain a bit better - and hopefully get a better result... Here goes...

 

I have 2 arrays, called $old_teams1 and $old_teams2.... $old_teams1 represents side 1 of the fixtures, and $old_teams2 represents side 2 of the fixtures... Both these arrays may vary in size depending on how many fixtures/teams there are.... This table represents the 2 arrays:

Fixture$old_team1$old_team2

134

241

332

442

513

621

With less or more teams, there would be more or less fixtures.. In the above table, there are 4 teams... So the 2 end colomns, represent the arrays...

 

The numbers (with 4 teams) as above, vary from obviously 1-4...  and with more teams, a bigger range. However these numbers are not the actual team ID numbers, as defined in the MySQL database.  If the team ID numbers defined in the database, are stored in the array, called $teams_arr for example... The actual numbers of the teams, in the database may vary depending.. but for now... lets say $teams_arr is the following:

$teams_arr
= Array ( 
[0] => 3 
[1] => 5 
[2] => 6
[3] => 8 ) 

 

How do I go about substituting all instances, of 1's for 3's, 2's for 5's, 3's for 6's and 4's for 8's, .... I need to convert the existing values as stored in $old_teams1 and $old_teams2, into the new $teams_arr values...

 

I hope you understand what I'm getting at...

$non_rand_arr = array(1, 2, 3, 4);
$rand_arr = $non_rand_arr;
shuffle($rand_arr);

$team1 = array (3, 4, 3, 4, 1, 2);

foreach ($team1 as $key => $value) {
$newKey = array_search($value, $non_rand_arr);
if ($newKey !== false) $team1[$key] = $rand_arr[$newKey];
}

 

This looks at each value in the $team1 array; searches for it in the NON Random array and gets it's key from there; then sets the $team1 element to the value from the RANDOM array with the same key. If a value in $team1 is NOT in the non random array, its value does not change.

 

Although, if the values in the non-random array are always consecutive, you could just shuffle that array, and use the value returned by array_search (plus one) as the new value for the team array.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.