Replacing PHP array value (or alternative)

I'm working on a system that pulls information from an external source and then processes the data a bit. Ive got it to perform the desired functionality for the client when processing data in JavaScript. Next, I want to store the information in the database. But here I want to handle it exclusively in PHP (or some other server). What is currently confusing me is the array replacement stuff.

What I have now stored in an array are the Player IDs, Times, Name and Bikes. The data I am getting divides the bikes into 2 classes (rows 2 and 4), so I have to get each dataset separately. The "rec450" array contains the corresponding information from the 4stroke class. Here I want to compare the 2stroke class for a given user and see if the time is faster at 2-level. If so, I want to replace the data in array "rec450" with new information from array 2stroke. So I would like to have this functionality:

if(class1[β€˜time’] < class2[β€˜time’]){   
  replace class1 info with the class2 info;    
}

      

Here is the PHP code that I am currently using:

//Pushing the data I want from the fetched array to a new array. This array is no longer associative.
foreach ($rr450f as $key => $value) {
    $tmpArr = array(
        "name" => $value['name'],
        "time" => $value['time'],
        "bike" => $value['bike'],
        "uid" => $value['uid']
    );
    array_push($rec450, $tmpArr);
}

      

This is how I am trying to change the values ​​for the rec450 array:

foreach($rr250 as $key => $value){

    foreach ($rec450 as $k => $v) {
        if($value['uid'] == $v['uid']){

            $k['time'] = $value['time'];
            $k['bike'] = $value['bike'];
            $k['name'] = $value['name'];
            $k['uid'] = $value['uid'];

        }
    }
}

      

Here is the print_r of the rec450 array (structure) Im using:

Array(
[0] => Array
    (
        [name] => Player One
        [time] => 17068
        [bike] => rmz450(2013)
        [uid] => 90970
    )

[1] => Array
    (
        [name] => Player Two
        [time] => 8959
        [bike] => 450sxf(2016)
        [uid] => 76800
    )

[2] => Array
    (
        [name] => Player Three
        [time] => 8300
        [bike] => yz450f(2016)
        [uid] => 90380
    )

[3] => Array
    (
        [name] => Player Four
        [time] => 8791
        [bike] => 450sxf(2016)
        [uid] => 89240
    )

[4] => Array
    (
        [name] => Player Five
        [time] => 19640
        [bike] => 450sxf(2016)
        [uid] => 98809
    )
)

      

It is an array containing 2-point data in its basic form before filtering out any columns.

Array
(
    [90970] => Array
        (
        [time] => 11929
        [bike] => rm250(2008)
        [number] => 123
        [name] => Player One
        [uid] => 90970
        [url] => /path/to/player/info
    )

[9248] => Array
    (
        [time] => 9922
        [bike] => cr250(2007)
        [number] => 124
        [name] => Player Twelve
        [uid] => 60030
        [url] => /path/to/player/info
    )
)

      

At this stage, I want to somehow replace the time of 4-cycle seconds by 2 times, if the player was faster at 2-cycle. If there is no need to filter out unnecessary data and lose connection of the array when I do rr450f in rec450, I could skip this.

Truly appreciate any information on how I can replace the values ​​in the 1st array with the ones in the second!

+3


source to share


1 answer


You may be looking at this the wrong way.

Basically, you are trying to sort a multidimensional array using the time key.

http://docs.php.net/manual/en/function.array-multisort.php

Try following code after creating array



$list = array();
foreach ($rec450 as $key => $row)
  {
    $list[$key] = $row['time'];
  }
array_multisort($list, SORT_ASC, $rec450);

      

What's going on here is that we create an array from all times and then sort the main array according to this new array.

I must add a little bit of thanks for this, from this original post and the accepted answer.

How to sort an array of associative arrays by the value of a given key in PHP?

+1


source







All Articles