PHP comparison 22-dimensional array Ask the great God to teach

Source: Internet
Author: User
Such as:

$grade = Array(    [0] => Array        (            [course_name] => 大学英语(综合)-1            [course_strid] => College English (Comprehensive)-1            [credit] => 2            [course_nature] => 必修            [grade] => 70.0            [year] => 2013-2014            [term] => 1            [user_id] => 1034        ))$temp = Array(    [0] => Array        (            [user_id] => 1034            [year] => 2013-2014            [term] => 1            [course_name] => 大学英语(综合)-1            [course_nature] => 必修            [credit] => 3            [grade] => 1            [course_strid] => 1        ))

$gradeif two arrays are returned as long as there is a difference $grade

Reply content:

Such as:

$grade = Array(    [0] => Array        (            [course_name] => 大学英语(综合)-1            [course_strid] => College English (Comprehensive)-1            [credit] => 2            [course_nature] => 必修            [grade] => 70.0            [year] => 2013-2014            [term] => 1            [user_id] => 1034        ))$temp = Array(    [0] => Array        (            [user_id] => 1034            [year] => 2013-2014            [term] => 1            [course_name] => 大学英语(综合)-1            [course_nature] => 必修            [credit] => 3            [grade] => 1            [course_strid] => 1        ))

$gradeif two arrays are returned as long as there is a difference $grade

Looked down, it seems that this function can meet the requirements:
Http://www.w3school.com.cn/php/func_array_diff_assoc.asp

If the landlord gives the array structure and the problem is exactly the same. Then you can use the following methods:

function compare($grade,$temp){    $compare = array_diff_assoc($grade[0],$temp[0]);    if ($compare != null) {        return $grade;    }}

If $grade below actually contains many sub-arrays, more than $grade[0] this one, then combine the methods upstairs:

function compare($grade,$temp){    foreach($grade as $key=>$value){        $compare = array_diff_assoc($value,$temp[$key]);        if ($compare != null) {            return $grade;        }    }}

If you solve your problem, remember to adopt:)

phpforeach($grade as $key=>$item){    if($item != $temp[$key]){        return false;    }}return true;

foreach simple, master to an elegant way!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.