Multiply two array - php

I have an two array like this:
$wij = array(0.25, 0.30, 0.25. 0.15, 0.5);
$nij = array(
array(3, 3, 2, 1, 2),
array(2, 2, 3, 2, 1),
array(1, 3, 2, 2, 1));
$rij = array();
I want to multiplying a value from wij array-variable into each nij array and join the result into rij array-variable, because $nij array always have more than 3 array than from the example. I dont have any clue just using for-loops in 1 looping. Please give me an example

if you want just add the values to $rij array use the code below:
$wij = array(0.25, 0.30, 0.25, 0.15, 0.5);
$nij = array( array(3, 3, 2, 1, 2), array(2, 2, 3, 2, 1), array(1, 3, 2, 2, 1));
$rij = array();
foreach($nij as $arr) {
foreach($arr as $val) {
foreach($wij as $multiplier) {
$rij[] = $val * $multiplier;
}
}
}
print_r($rij);

Related

Group Array By Range Value

I have this array [1,1,2,2,2,3,4,4,5,6,6,6,7], may I group the array according to the range value, so get the final result:
'1-3' = [1,1,2,2,2,3]; // Count is 6
'4-5' = [4,4,5]; // Count is 3
'6-7' = [6,6,6,7]; // Count is 4
What you need I believe is:
function array_get_range($array, $min, $max) {
return array_filter($array, function($element) use ($min, $max) {
return $element >= $min && $element <= $max;
});
}
$array = [1,1,2,2,2,3,4,4,5,6,6,6,7];
$range13 = array_get_range($array, 1, 3); // [1, 1, 2, 2, 2, 3]
$range45 = array_get_range($array, 4, 5); // [4, 4, 5]
$range67 = array_get_range($array, 6, 7); // [6, 6, 6, 7]
Create a new array with your ranges, then iterate through the values and through the ranges inside. If the current value is inside the range, add the record to the current range:
<?php
$numbers = [1,1,2,2,2,3,4,4,5,6,6,6,7];
$counts = [];
$counts[] = ["values"=> [1, 3], "records" => []]; // first value in "values" is min, second is max
$counts[] = ["values"=> [4, 5], "records" => []];
$counts[] = ["values"=> [6, 7], "records" => []];
foreach ($numbers as $number) {
foreach ($counts as $key => &$value) {
if ($number >= $value["values"][0] && $number <= $value["values"][1]) { // if between the range, add it to the records
$value["records"][] = $number;
}
}
}
echo "<pre>";
foreach ($counts as $count) {
echo $count["values"][0]." - ".$count["values"][1]." = ".count($count["records"])." elements<br>";
}
Demo
I think array_intersect() and range() with sizeof()/count() does a cleaner job for this task. It eliminates the double-conditional.
Code: (Demo)
function count_range($array, $min, $max) {
return sizeof(array_intersect($array, range($min, $max)));
}
$array = [1, 1, 2, 2, 2, 3, 4, 4, 5, 6, 6, 6, 7];
echo count_range($array, 1, 3); // 6 from [1, 1, 2, 2, 2, 3]
echo count_range($array, 4, 4); // 2 from [4, 4]
echo count_range($array, 2, 7); // 11 from [2, 2, 2, 3, 4, 4, 5, 6, 6, 6, 7]
range() creates a whitelist array of one or more consecutive integers.
array_intersect() compares the input array and the whitelist array all at once.
sizeof() is just an alias of count() to determine how many elements were retained.

PHP compare arrays

I have two arrays. $arrayOld and $arrayNew and I want to compare these arrays and only select the values that are not in $arrayNew.
I don't want the values that are in $arrayNew only. So I don't think array_diff() is gonna help me.
$arrayOld = [1, 2, 3, 4, 5]
$arrayNew = [1, 4, 5, 6, 7]
So it only needs to return 2 and 3 and not 6 or 7.
use array_diff, to accomplish this. As you need to difference between the array and need data from Old array so you need to use the old array as the first parameter of the array_diff.
Note: Array diff only returns from the first array which is not in second array.
$arrayOld = [1, 2, 3, 4, 5];
$arrayNew = [1, 4, 5, 6, 7];
$n = array_diff($arrayOld, $arrayNew);
print_r($n);
Result: Online Check
Array
(
[1] => 2
[2] => 3
)
If you need a new keys for the output array just use array_values. The new key start from 0.
$arr = array_values($n);
Use below code
$arrayOld = [1, 2, 3, 4, 5];
$arrayNew = [1, 4, 5, 6, 7];
print "<pre>";
print_r(array_diff($arrayOld, $arrayNew));
OUTPUT:
Array
(
[1] => 2
[2] => 3
)
use this code.
$arrayOld = array(1, 2, 3, 4, 5);
$arrayNew = array(1, 4, 5, 6, 7);
print_r(array_diff($arrayOld, $arrayNew));
$compare = array();
$i=1;
foreach($arrayOld as $k=>$v){
if(!in_array($v, $arrayNew)){
$compare[$i] = $v;
$i++;
}
}
$i=$i;
foreach($arrayNew as $k=>$v){
if(!in_array($v, $arrayOld)){
$compare[$i] = $v;
$i++;
}
}
use array_diff function
$arrayOld = array(1, 2, 3, 4, 5);
$arrayNew = array(1, 4, 5, 6, 7);
print_r(array_diff($arrayOld, $arrayNew));

Multiplying from two array variable in PHP

Let say.. I have data like this
wij = [0.5, 0.30, 0.25, 0.15, 0.25]
and
As you see.. each data in wij is represent from C1 until C5 from table above
C1=0.5, C2=0.30, C3=0.25, C4=0.15, C5=0.25
So.. i create the wij into array-variable like this:
$wij = array(0.5, 0.30, 0.25, 0.15, 0.25);
and A1 until A5 rows into array-variable:
$nij = array(
array(150, 15, 2, 2, 3);
array(500, 200, 2, 3, 2);
array(200, 10, 3, 1, 3);
array(350, 100, 3, 1, 2);
);
I want to multiplying each data from wij with data from A1 until A5 rows, so it will be look like this:
A1 = (0.5*150)+(0.30*15)+(0.25*2)+(0.15*2)+(0.25*3)
A2 = (0.5*500)+(0.30*200)+(0.25*2)+(0.15*3)+(0.25*2)
A3 = (0.5*200)+(0.30*10)+(0.25*3)+(0.15*1)+(0.25*3)
A4 = (0.5*350)+(0.30*100)+(0.25*3)+(0.15*1)+(0.25*2)
I don't have any clue how to do it using for-loops or foreach-loops. Because each rows in table is not always have 4 data like from table above, it can always 5 rows or more, so I guess it will be work if using for-loops.
<?php
$wij = array(0.5, 0.30, 0.25, 0.15, 0.25);
$array1 = array(150, 15, 2, 2, 3);
$array2 = array(500, 200, 2, 3, 2);
$array3 = array(200, 10, 3, 1, 3);
$array4 = array(350, 100, 3, 1, 2);
$arrays = array($array1,$array2,$array3,$array4);
$as = array(0,0,0,0);
for($i = 0;$i<4;$i++)
{
for($t = 0;$t<5;$t++)
{
$as[$i] += ($wij[$t]*$arrays[$i][$t]);
}
echo "</br>".$as[$i];
}
?>
I can explain for-loop, if you want.

remove elements in php array that occur more than twice

I am looking for a function in php,where the function must delete values in the array that shows up three times or more? For example, if you give the function array(2, 4, 6, 3, 7, 7, 7, 4, 2, 0) the funciton will return array(2, 4, 6, 3, 4, 2, 0)
You can use array_count_values() to get frequencies. Then use a foreach to get values that has frequency less than 3...
$array = array(2, 4, 6, 3, 7, 7, 7, 4, 2, 0);
$frq = array_count_values($array);
$result = array();
foreach ($frq as $key=>$value){
if ($value < 3){
$result[] = $key;
}
}
function FilterMyArray(array &$array){
$array_count=array_count_values($array);
foreach($array_count as $key => $value){
if($value > 2){
foreach (array_keys($array, $key, true) as $unsetKey) {
unset($array[$unsetKey]);
}
}
}
}
$array=array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9);
FilterMyArray($array);
print_r($array);
Output
Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 2 [7] => 3 [8] => 1 [9] => 9 )
`
This will remove all duplicates, but you'd have to add to it to count the number of each of the values.
$a = array(2, 4, 6, 3, 7, 7, 7, 4, 2, 0);
$b = array();
for ($a as $key=>$value) {
if (!in_array($value, $b)) {
$b[] = $value;
}
}
// array $b has all the values with no duplicates.

Foreach loop + recursion

I am trying to put this code in a more flexible manner so it can work whatever the size of $sets array is. I suppose it can be done with recursion but cannot find the correct php syntax.
$sets = array(
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3)
);
$combinations = array();
foreach($sets[0] as $s1)
foreach($sets[1] as $s2)
foreach($sets[2] as $s3)
foreach($sets[3] as $s4)
$combinations[] = array($s1, $s2, $s3, $s4);
print_r($combinations);
You can do it in recursion like this. The output is identical to your loops
<?php
$sets = array(
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3),
array(0, 1, 2, 3)
);
function get_combinations($sets, &$combinations = array(), &$row = array()) {
if (count($sets) == 0) {
$combinations[] = $row;
return $combinations;
}
foreach ($sets[0] as $s) {
$row[] = $s;
get_combinations(array_slice($sets, 1), $combinations, $row);
array_pop($row);
}
return $combinations;
}
$combinations = get_combinations($sets);
print_r($combinations);

Categories