0

I am trying to compare two arrays and update specific array values based on conditions.

Getting attendance array of absent students:

$array1 = 
    array(
    array("student_id" => "2",
        "date" => "2016-04-24"),
    array("student_id" => "6",
        "date" => "2016-04-24"));
$attendance = json_decode(json_encode($array1));

Getting student list array of all students:

$array2 = 
    array(
    array("student_id" => "1",
        "Reason" => "",
        "date" => "2016-04-24"),
    array("student_id" => "2",
        "Reason" => "",
        "date" => "2016-04-24"),
    array("student_id" => "3",
        "Reason" => "",
        "date" => "2016-04-24"),
    array("student_id" => "6",
        "Reason" => "1",
        "date" => "2016-04-24"));
$students = json_decode(json_encode($array2));

Taking out only student IDs of absent students:

foreach($attendance as $att) 
{   $atts[] = $att->student_id;}

Here I am trying to find out if any of the students ID in student array matches with ID in absent array. If ID is present then I will update the Reason as "1". Else will make the reason as 0

for ($i = 1; $i <= count($students); $i++) {
    if(in_array($atts[$i],$students))
    {
        $students->Reason='1';
    }
    else
    {
        $students->Reason='0';
    }
} 
echo '<pre>',print_r($students,1),'</pre>';

Here I am unable to update student array with "reason" values.

8
  • 6
    json_decode(json_encode($array1));.. whut? Commented Apr 28, 2016 at 10:09
  • 1
    Hey Jon I am new to php and and was trying to convert multi dimensional array to array of std objects Commented Apr 28, 2016 at 10:11
  • Okay, first off, just use the arrays. You have no benefit in this use-case of converting to objects. 2) $students is an array, not an object, so doing $students->Reason doesn't even make sense as you're actually wanting to update $students[$i]? 3) $students is an array of either more arrays, or currently in your case, objects. Your in_array is currently trying to compare a scalar value (an ID) against an array or object, which isn't going to match. Commented Apr 28, 2016 at 10:13
  • OK. Please suggest a way out Commented Apr 28, 2016 at 10:22
  • 1
    I usually wouldn't do this, but I've done a little cleaning up and refactoring to what I believe works the way you want (see 3v4l.org/b5fce). It's only an example using your code as a basis. Please have a read and understand what the differences and changes are before using :). Commented Apr 28, 2016 at 10:24

1 Answer 1

1

If you just want to compare student_id from array1 with student_id from array2, and set Reason in array2 if they correspond to each other, use this :

foreach ($array1 as $key1 => $value1) {
    foreach ($array2 as $key2 => $value2) {
        if ($value1['student_id'] == $value2['student_id']) {
            $array2[$key2]['Reason'] = 1;
        } else if ($array2[$key2]['Reason'] != 1) {
            $array2[$key2]['Reason'] = 0;
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

when I am increasing array elements in both arrays, the code doesnt work
Did you increase with another array ? Show me what you have now
I have made changes in my original question. Added arrays in both array1 and array2
My bad, I modified my post.

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.