Check if a specific value exists in the array, if not check next value and make sure no previous values are encountered
Ok, so I have an array called $ arraymaster, which is basically a bunch of educational codes (highest to lowest):
$arrayedumaster=array("P H D", "Post Graduate", "Professional", "Graduate", "Technical", "ITI", "Under Graduate", "H S C Passed", "S S C Passed", "Upto Ninth", "Upto Fifth", "No Schooling");
There is now a second array that lists the individual qualifications I got from the database:
$arrayedu=explode(',', $edudetail);
Some examples of $ arrayedu (a person can have multiple qualifications):
- Graduate, HSC Passed, Graduate, Professional, Professional
- Professional, SSC passed
- Postgraduate, professional, technical
Etc. Now I have to set individual highest qualification (based on $ arraymaster) -> and then do some actions based on that from $ arrayedu. I am currently doing it like this:
if(in_array("P H D", $arrayedu){
$educode=1;
}
elseif(in_array("Post Graduate", $arrayedu) && !in_array("P H D",$arrayedu)){
$educode=2;
}
elseif(in_array("Professional", $arrayedu) && !in_array("P H D", $arrayedu)
&& !in_array("Post Graduate", $arrayedu)){
$educode=3;
}
elseif(in_array("Graduate", $arrayedu) && !in_array("Professional",
$arrayedu) && !in_array("P H D", $arrayedu) && !in_array("Post Graduate",
$arrayedu)){
$educode=4;
}
Etc. So basically I am trying to get the highest qualification from $ arrayedu, is there an easier way to do this?
source to share
Solution using functions array_intersect
and current
:
$arrayedumaster = [
"P H D", "Post Graduate", "Professional", "Graduate", "Technical", "ITI", "Under Graduate",
"H S C Passed", "S S C Passed", "Upto Ninth", "Upto Fifth", "No Schooling"
];
$arrayedu = ["Graduate", "HSC Passed", "Graduate", "Professional", "Professional"];
$highest_edu = current(array_intersect($arrayedumaster, $arrayedu));
print_r($highest_edu);
Output:
Professional
source to share
A perfect one-line answer has already been provided by RomanPrekhrest, but perhaps this approach could be inspired:
$arrayedumaster = [
"P H D", "Post Graduate", "Professional", "Graduate", "Technical", "ITI",
"Under Graduate", "H S C Passed", "S S C Passed", "Upto Ninth",
"Upto Fifth", "No Schooling"
];
$arrayedu = [
"Graduate", "HSC Passed", "Graduate", "Professional", "Professional"
];
$highestedu = count($arrayedumaster);
foreach ( $arrayedu as $value ) {
if ( ($key = array_search($value, $arrayedumaster)) !== false ) {
$highestedu = min($key, $highestedu);
}
}
print_r($arrayedumaster[$highestedu]);
source to share
Try this for sorting:
$data = array(
"Graduate", "H S C Passed", "Graduate", "Professional", "Professional"
);
$sortingArr = array("P H D", "Post Graduate", "Professional", "Graduate", "Technical", "ITI", "Under Graduate", "H S C Passed", "S S C Passed", "Upto Ninth", "Upto Fifth", "No Schooling", "ddd");
$result = array(); // result array
foreach($sortingArr as $val){ // loop
$key = array_search($val, $data);
if(in_array($val, $data)) {
$result[$key] = $val; // adding values
}
}
print_r($result); // print results
source to share