3

I’m working on a system that fetches info from an external source and then processes the data a bit. I’ve gotten it to do my wished functionality for the client when processing the data in JavaScript. What’s next is that I want to store the info in a database. But here I want to process it solely in PHP (or in some other server side way of doing it). What’s currently confusing me is some array replacement stuff.

What I have now stored in an array is players ID, Times, Name and Bikes. The data I'm fetching is dividing the bikes into 2 classes (2 and 4stroke) so I have to fetch each set of data individually. The array "rec450" contains the relevant information from the 4stroke class. What I want to do here is compare the 2stroke class for a given user and see if the time is faster on the 2stroke. If it is, I want to replace the data in the "rec450" array with the new info from the 2stroke array. 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 I’m 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 try 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) I’m 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
    )
)

This is the array that contains the 2stroke data in it's base form, before any columns are filtered out.

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 what I want is some way to replace the 4stroke times with the 2stroke times if a Player was faster on the 2stroke. If it's unnecessary to do the filtering out of unnecessary data and losing the association of the array when I am doing the rr450f to rec450, I could skip that.

Would really appreciate any info of how I can replace the values in the 1st array with the ones from the 2nd one!

1 Answer 1

1

You may be looking at this wrong.

Essentially what you are trying to achieve is to sort a multidimensional array by the key of 'time'.

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

Try the following code after you created your array

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

What's happening here is that we're creating an array out of all the times and then sorting the main array according to that new array.

I must add some credit behind this, from this original post, and accepted answer.

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

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

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.