0

I'm Trying to get 5 dates ($completion_date) into an array I have a list I'm looping through passing the $completion_date to the function below there are multiple instances of the same $completion_date in the list but I only want one of each in the array $completion_dates = []; so each time I search the array using array_search($completion_date, $completion_dates); and if the current $completion_date isn't there I want to add it to the array and if it is I want to modify $completions and $payouts at the same position in their respective arrays. My problem is array_push doesn't seem to push all the dates? only one?

And I've checked the if statement that array_push is in and it's running the else clause every time (as array_push isn't working to change that);

function sortResults($completion_date, $payout){
    global $completion_dates, $completions, $payouts;

    $completion_dates = [];
    $completions = [0,0,0,0,0]; // not in use
    $payouts = [0,0,0,0,0]; // not in use


    // check is $completion_date is in $completion_dates array and get position if so.
    $position = array_search($completion_date, $completion_dates);

    if ($position) {
        // update $payouts and $completions @ same $position.
    }else{
        // add $completion_date to $completion_dates array.
        array_push($completion_dates, $completion_date);
    }

}


var_dump($completion_dates);

outputs: array(1) { [0]=> string(10) "22/01/2017" }

But should output four other dates "18/01/2017", "19/01/2017", "20/01/2017", "21/01/2017" as well?

the data i'm looping through is escaping the dates like so {"completion_date":"18\/01\/2017","0":"18\/01\/2017","payout":"13.20","1":"13.20"} not sure if it matters, it really shouldn't?

1 Answer 1

1

Because you overwrite $completion_dates each time in your function.

function sortResults($completion_date, $payout)
{
    global $completion_dates, $completions, $payouts;

    $completion_dates = []; <----------------------- HERE
    $completions = [0, 0, 0, 0, 0]; // not in use
    $payouts = [0, 0, 0, 0, 0]; // not in use


    // check is $completion_date is in $completion_dates array and get position if so.
    $position = array_search($completion_date, $completion_dates);

    if ($position) {
        // update $payouts and $completions @ same $position.
    } else {
        // add $completion_date to $completion_dates array.
        array_push($completion_dates, $completion_date);
    }

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

3 Comments

Thats not gonna work either. It also sounds clearer to declare your globals at start outside functions to keep trace of them. global $completion_dates=array();
even with the array outside, the global doesn't seem to work? even with the comment's help above.
pastebin.com/raw/RFuxAKyU this doesn't work either? (when called)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.