Multiplying from two array variable in PHP - 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.

Related

Creating a list and indexing

Given a two list. Create a third list by picking an odd-index element from the first list and even index elements from the second.
For Example:
listOne = [3, 6, 9, 12, 15, 18, 21]
listTwo = [4, 8, 12, 16, 20, 24, 28]
Expected Output:
Printing Final third list
[6, 12, 18, 4, 12, 20, 28]
I think, it will be helpful for you.
<?php
$listOne = [3, 6, 9, 12, 15, 18, 21];
$listTwo = [4, 8, 12, 16, 20, 24, 28];
$NlistOne=[];
$NlistTwo=[];
//odd-index positions from list one [6, 12, 18]
foreach ($listOne as $key => $value) {
if($key%2==1){
array_push($NlistOne, $value);
}
}
//even-index positions from list two [4, 12, 20, 28]
foreach ($listTwo as $key => $value) {
if($key%2==0){
array_push($NlistTwo, $value);
}
}
//Printing Final third list [6, 12, 18, 4, 12, 20, 28]
print_r(array_merge($NlistOne,$NlistTwo));
?>
Output will be:
Array ( [0] => 6 [1] => 12 [2] => 18 [3] => 4 [4] => 12 [5] => 20 [6] => 28 )
//init result array
//loop over listOne, using for($i=1;$i<sizeof($listOne);$i=$i+2)
//and add to result for each iteration, $resultArr[] = $listOne[$i]
//do the same with listTwo, but for($i=*0*
You can merge both of arrays and then pick all odd elements
$listOne = [3, 6, 9, 12, 15, 18, 21];
$listTwo = [4, 8, 12, 16, 20, 24, 28];
$result = [];
foreach ( array_merge($listOne, $listTwo) as $value ){
if ( $key % 2 ) {
$result[] = $value;
}
}
If array length isn't fixed, say it could contain not 7 elements, then you need to check it before merging to make it have odd number of elements
$listOne = [3, 6, 9, 12, 15, 18, 21, 777];
$listTwo = [4, 8, 12, 16, 20, 24, 28];
$result = [];
if ( count($listOne) % 2 !== 1 ) {
$listOne[] = 0;
}
foreach( array_merge($listOne, $listTwo) as $value ){
if ( $key % 2 ) {
$result[] = $value;
}
}
you don't have to loop over whole array array_filter will do this for you, Constant ARRAY_FILTER_USE_KEY will check each key for odd or for even
<?php
$a1 = [3, 6, 9, 12, 15, 18, 21];
$a2 = [4, 8, 12, 16, 20, 24, 28];
function odd($var)
{
// returns whether the input integer is odd
return $var & 1;
}
function even($var)
{
// returns whether the input integer is even
return !($var & 1);
}
$result= (array_merge(array_filter($a1, 'odd', ARRAY_FILTER_USE_KEY),array_filter($a2, 'even', ARRAY_FILTER_USE_KEY)));
output you will get
Array (
[0] => 6
[1] => 12
[2] => 18
[3] => 4
[4] => 12
[5] => 20
[6] => 28 )
Iterate over first one and take only values of odds indices, and loop again through second one and take evens indices.
$listOne = [3, 6, 9, 12, 15, 18, 21];
$listTwo = [4, 8, 12, 16, 20, 24, 28];
$res = [];
for ($i=0; $i < count($listOne); $i++) {
if($i & 1) // $i is odd.
$res[] = $listOne[$i];
}
for ($j=0; $j < count($listTwo); $j++) {
if(n % 2 === 0) // $j is even.
$res[] = $listTwo[$j];
}
Result:
echo "List One :".json_encode($listOne)."<br>";
echo "List Two :".json_encode($listTwo)."<br>";
echo "Lists Merged:".json_encode($res);
Output:*
/*
List One :[3,6,9,12,15,18,21]
List Two :[4,8,12,16,20,24,28]
Lists Merged:[6,12,18,4,12,20,28]
*/
Iterate over arrays:
take odds in array starting with index One and increment it by two.
take evens in array by starting with index Zero and increment it by two.
$listOne = [3, 6, 9, 12, 15, 18, 21]; $listTwo = [4, 8, 12, 16, 20, 24, 28];
$res = [];
for ($i=1; $i < count($listOne); $i+=2) {
$res[] = $listOne[$i];
}
for ($j=0; $j < count($listTwo); $j+=2) {
$res[] = $listTwo[$j];
}
print(json_encode($res)); // [6,12,18,4,12,20,28]

PHP reorder an array at random

I need to reorder an array at random but I am not sure what the best/cleanest/fastest way is to do this.
So what I am trying to achieve is the following. Let's say I have an array that looks like this:
$array = array(4, 4, 4, 4, 6, 6, 6, 6, 8, 8, 10, 10, 20, 40, 60);
My goal is to get something like this but at random:
$array = array(6, 4, 4, 10, 4, 6, 4, 6, 60, 6, 8, 6, 10, 40, 8, 20);
Here's what I've been trying but it doesn't seem to be working as intended:
$array = array(4, 4, 4, 4, 6, 6, 6, 6, 8, 8, 10, 10, 20, 40, 60);
$newArray = array();
$randomNumber = rand(0 , 14);
for ($x = 0; $x <= 15; $x++) {
$newArray[$x] = $array[$randomNumber];
}
Many thanks in advance to anyone who can help me out :)
Use shuffle() function.
shuffle($array);
http://php.net/manual/en/function.shuffle.php
$array = array(4, 4, 4, 4, 6, 6, 6, 6, 8, 8, 10, 10, 20, 40, 60);
Check the output of previous array
echo "<pre>";
print_r($array);
echo "</pre>";
Shuffling previous array & check output again.
shuffle($array);
print_r($array);
Now run a foreach loop like
foreach($array as $item){
echo $item;
}
Note: You don't need to store shuffle data to new array.

Multiply two array

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);

PHP Find a maximum average for 10 subsequent numbers in a list of 50 random numbers

I'm working on this for quite a while, but I don't know how to fix this:
I have a list of 50 random numbers, and when 10 subsequent numbers from this this (numbers 11-20 for example, or numbers 24-33) reach an average of x, I want to get a notification.
The 50 numbers are in 1 row of a (HTML) table, each in a different column.
Anybody with an idea how to start? Thanks!
If you have your numbers in an array, you can loop through the array in chunks of 10, and then find the maximum average.
<?php
$numbers = array(1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1,
10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10, 11, 12, //large numbers here
1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1);
$number = 10; //numbers in a set
$max = 0;
$index = 0;
$size = sizeof($numbers) - $number;
for ($i = 0; $i < $size; $i++) {
$tmp = array_sum(array_slice($numbers, $i, $number)) / $number;
if ($tmp > $max) {
$max = $tmp;
$index = $i;
}
}
echo "Largest set of " . $number . " numbers is: " . implode(', ', array_slice($numbers, $index, $number)) . "\nAverage of: " . $max;
Output:
Largest set of 10 numbers is: 10, 11, 12, 13, 14, 15, 14, 13, 12, 11
Average of: 12.5
You can then compare the largest average to your threshold and notify yourself.

see if 3 or more numbers match an array

Hay i have an array consisting for 6 random numbers, here's an example
[4,8,12,22,23,43]
I also have 100 arrays containing 6 numbers, these are all random a few examples could be
[5,8,15,47,32,48]
[3,4,8,12,33,42]
[8,12,26,55,43,33]
[4,63,45,23,45,55] ...
I want to see how many times (out of the array of 100) these numbers match at least 3 from the top array. As you can guess this is a lottery experiment.
As you can see array number 3 matches 3 numbers from the top array.
Any ideas how do do this? With perhaps an option to see if 4 numbers matched.
$master_array = array(4, 8, 12, 22, 23, 43);
$arrays = array(array(5, 8, 15, 47, 32, 48),
array(3, 4, 8, 12, 33, 42),
array(8, 12, 26, 55, 43, 33),
array(4, 63, 45, 23, 45, 55));
foreach ($arrays as $arr)
{
$intersect = array_intersect($master_array, $arr);
if (count($intersect)==3) print 'Match: '.print_r($arr, true).PHP_EOL;
}
maybe smth like this:
$winner = [4,8,12,22,23,43];
$arrays = //all your 100 arrays
$i = 0; // number of matches
foreach ($arrays as $array)
{
$result = array_intersect($array, $winner);
if (count($result) >= 3) $i++;
}

Categories