I'm building support decision system using electre method
While i compile my code i got an error
the error is : Warning Division By zero
Here is the function that i created
function normalisasi(){
$array = ratarata();
$arr = spk_rel();
$nilai = array();
$data = array();
foreach ($array as $key => $value) {
$nilai[$key]=sqrt(array_sum($value));
}
foreach($arr as $key => $value){
foreach($value as $k => $v){
$data[$key][$k] = $v / $nilai[$k];
}
}
return $data;
}
The error is in
$data[$key][$k] = $v / $nilai[$k];
could you please help me ?
You will get a error when you divide any by 0.
You should check $nilai[$k] before make a divide by 0.
if (!empty($nilai[$k])) {
$data[$key][$k] = $v / $nilai[$k];
}
You didn't specified what the logic is about, but if you're expecting some values of $nilai to be 0 and you can just skip them, there 2 ways to achieve that:
Checking for value inside loop
foreach($arr as $key => $value){
foreach($value as $k => $v){
if ($nilai[$k]) {
$data[$key][$k] = $v / $nilai[$k];
}
}
}
This expression if ($nilai[$k]) will be true when $nilai[$k] is either positive or negative and will be false when it's 0.
You can remove 0 values from $nilai array before loop
foreach ($array as $key => $value) {
$nilai[$key]=sqrt(array_sum($value));
}
$nilay = array_filter($nilay);
foreach($arr as $key => $value){
...
}
In this case array_filter called without second argument (filter callback) will just remove all 0 values from array passed as first argument.
Related
I want to remove or hide duplicate values in loop , It's not array.
You can see in picture 1/3, & 2/2 is repeating so I want only once using loop. It's not return any array it's simple data so we can't use array_unique
$i=1;
$result = array();
foreach ($boxes as $key => $value) {
foreach ($result as $k => $val) {
echo $i."/".count($value).'</br>';
}
$i++;
}
Expected Output
REG-Pre-Cut Short 1/3
REG-Pre-Cut Long -
PREM-Pre-Cut Short -
PREM-Pre-Cut Short 2/2
PREM-Pre-Cut Long -
Try something like this.
First you create an array in the expected format.
Then use array_unique to remove duplicates.
Then you print out the array.
$i=1;
$result = array();
$outputResult = array();
foreach ($boxes as $key => $value) {
foreach ($result as $k => $val) {
$outputResult[] = $i."/".count($value).'</br>';
}
$i++;
}
$outputResult = array_unique($outputResult);
foreach ($outputResult as $result) {
echo $result;
}
I want to do the same actions thru some variables. So I created the variables of variables. But it is throwing me error - "Invalid argument supplied for foreach()" when I am looping thru $$a. I have check the type of the variable. It is array. Then what is the error ?
$edu_data = Json::decode($model->education);
$exp_data = Json::decode($model->experience);
$avail_data = Json::decode($model->availability);
$docs_data = Json::decode($model->documents);
$model_edu = new \admin\models\ApplicantEducation();
$model_exp = new \admin\models\ApplicantExperience();
$model_avail = new \admin\models\Availability();
$model_cre = new \admin\models\Credential();
$all = array('edu_data' => 'model_edu', 'exp_data' => 'model_exp', 'avail_data' => 'model_avail', 'docs_data' => 'model_cre');
foreach ($all as $a => $s)
{
$arr = $$a;
foreach ($arr as $v)
{
$$s->applicant_id = $applicant_id;
foreach ($arr[1] as $k1 => $v1)
{
$$s->$k1 = $v[$k1];
}
$$s->save();
}
}
Your array does not contain your variables (e.g. $model_edu), but only their respective names as string values ('model_edu'). Edit: My bad, I didn't notice this is intentional.
I suggest using a function:
function process_data($model, $data, $applicant_id) {
foreach ($data as $v) {
$model->applicant_id = $applicant_id;
foreach ($data[1] as $k1 => $v1)
{
$model->$k1 = $v[$k1];
}
$model->save();
}
}
process_data($model_edu, $edu_data);
process_data($model_exp, $exp_data);
process_data($model_avail, $avail_data);
process_data($model_docs, $docs_data);
Your code will be more easily comprehendable.
Apart from that, you can debug your code like this to find out exactly where and when the error happens:
foreach ($all as $a => $s)
{
$arr = $$a;
var_dump($arr);
foreach ($arr as $v)
{
$$s->applicant_id = $applicant_id;
var_dump($arr[1]);
foreach ($arr[1] as $k1 => $v1)
{
$$s->$k1 = $v[$k1];
}
$$s->save();
}
}
See if this is the expected value and proceed from there on.
Find out if the reason is an unexpected value in one of your variables or if it is an error in the code logic.
I found a way to search my multidimensional array and output the result and it works, however it only finds the first match and stops. If I have more than one match in the array I want to be able to show them all.
My array looks like this (the first layer of keys goes from 0, 1, 2 etc):
Array
(
[0] => Array
(
[mydevice] => blahblah
[ipadd] => 10.10.10.209
[portnum] => 16040
)
function searcharray($value, $key, $array) {
foreach ($array as $k => $val) {
if ($val[$key] == $value) {
return $k;
}
}
return null;
}
$myoutput = searcharray($ptn2, mydevice, $newresult);
I can then echo the results using something like $newresult[$myoutput][mydevice].
However if I have more than one entry in the array with a matching data in the 'mydevice' key it doesn't return them (just the first one).
That is because return breaks the function. You could use something like this:
function searcharray($value, $key, $array) {
$result = array();
foreach ($array as $k => $val) {
if ($val[$key] == $value) {
$result[] = $k;
}
}
return $result;
}
Now you will always get an array as result - empty if nothing was found. You can work with this like this e.g.
$mydevicekeys = searcharray($ptn2, "mydevice", $newresult);
foreach ($mydevicekeys as $mydevicekey) {
// work with $newresult[ $mydevicekey ]["mydevice"]
}
So add the results to an array :)
function searcharray($value, $key, $array) {
$res = array();
foreach ($array as $k => $val) {
if ($val[$key] == $value) {
$res[] = $key;
}
}
return $res;
}
ksort ($votes);
foreach ($votes as $total => $contestant){
$ordervotes[]= $contestant;
}
echo "<li> And the winner is: {$ordervotes[4]}</li>";
echo "<li> And the loser is: {$ordervotes[0]}</li>";
echo "<li> {$ordervotes[1]} came second last</li>";
This works fine when none of the '$total's are the same, if they are the same i get an error code. I realise I could use the 'max/min' to get the first and last elements of the array, but how do i go about finding the second last?
Thank you
Joe
Why don't you try:
echo $votes[count($votes)-2];
You also don't need to populate another array with the same values - you can keep them in $votes. You might also want to look into sorting your array by value instead of by key (which I assume you're trying to do).
If you're expecting duplicate keys, you need to remodel the way you're storing your data. Consider using a multidimensional array:
$votes = array(
array('name'=>'John','vote'=>10),
array('name'=>'James','vote'=>11),
array('name'=>'Jimmy','vote'=>13),
);
You will be able to sort this array using this function and code:
// This function will sort your array
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
// Sort the array by the 'vote' key
aasort($votes,"vote");
// Echo out the name of the second-last person
echo $votes[count($votes)-2]['name'];
Use this:
function secondMax($arr) {
$max = $second = 0;
$maxKey = $secondKey = null;
foreach($arr as $key => $value) {
if($value > $max) {
$second = $max;
$secondKey = $maxKey;
$max = $value;
$maxKey = $key;
} elseif($value > $secondMax) {
$second = $value;
$secondKey = $key;
}
}
return array($secondKey, $second);
}
Usage:
$second = secondMax($votes);
You can retrieve it by using the function count:
$ordervotes[ (count($ordervotes)-2) ]
// the array starts with index 0, so (count($ordervotes)-1) is the last element
I don't understand what is in your $votes variable ... How could you have multiple contestants with the same votes (and so, with the same key).
I think there is a mistake here.
You $votes should be like this :
$votes = array(
'contestant 1' => 8,
'contestant 2' => 12,
'contestant 3' => 3
);
Then order the array : sort($votes)
Finaly, get the second last : $votes[count($votes) - 2];
I want to create an new array with duplicated MAX value from an array
and put other duplicate value in an other array
$etudiant = array ('a'=>'2','b'=>'5', 'c'=>'6', 'd'=>'6', 'e'=>'2');
and i want this result
$MaxArray = array ('c'=>'6', 'd'=>'6');
$otherarray1 = array ('a'=>'2', 'e'=>'2');
Thank you !
First, find the maximum value:
$etudiant = array ('a'=>'2','b'=>'5', 'c'=>'6', 'd'=>'6', 'e'=>'2');
$maxValue = max($etudiant);
Second, find values that appear more than once:
$dups = array_diff_assoc($etudiant, array_unique($etudiant));
Lastly, check the original arrays for values matching either $maxValue or values that are listed in $dups:
$MaxArray = $OtherArray = $ElseArray = array();
foreach ($etudiant as $key => $value) {
if ($value == $maxValue) {
$MaxArray[$key] = $value;
} else if (in_array($value, $dups)) {
$OtherArray[$key] = $value;
} else {
$ElseArray[$key] = $value;
}
}
You'll get:
$MaxArray: Array
(
[c] => 6
[d] => 6
)
$OtherArray: Array
(
[a] => 2
[e] => 2
)
Note: I wasn't sure if you wanted the $MaxArray to contain the maximum value elements only if it appears more than once in the source array. If so, just change the max call to:
$maxValue = max($dups);
You can use array_values(array_intersect($array1, $array2)) to get duplicated values, and then make a loop to capture the keys which have those values and store them into another array.
$dups = array_values(array_intersect($array1, $array2))
$max = max($dups);
$result = array();
foreach ($array1 as $key => $value){
if (in_array($value, $dups)) {
$result[$key] = $value;
}
}
foreach ($array2 as $key => $value){
if (in_array($value, $dups)) {
$result[$key] = $value;
}
}
$maxArray = array();
foreach ($dups as $key => $value) {
if ($value == $max){
$maxArray[$key] = $value;
}
}
// results are in $dups and $maxArray
If you are looking to find elements with the min and max values from an array, the following will work.
// get min keys
$min_value = min($etudiant);
$min_keys = array_keys($etudiant, $min_value);
// get max keys
$max_value = max($etudiant);
$max_keys = array_keys($etudiant, $max_value);
You could then either rebuild your example arrays with these keys in a loop. Or access them directly, i.e. $etudiant[$min_keys].
Check out the documentation for array_keys, min, max