I have problem with matrix operation in php.
I have two multidimensional array
array1=
Array([0] => Array([0] => 5000, [1] => 6, [2] => 325, [3] => 3, [4] => 3, [5] => 517000000)
[1] => Array([0] => 20000, [1] => 5, [2] => 217, [3] => 5, [4] => 3, [5] => 1692000000)
[2] => Array([0] => 12150, [1] => 3, [2] => 254, [3] => 4, [4] => 4, [5] => 1370520000)
[3] => Array([0] => 4200, [1] => 4, [2] => 351, [3] => 3, [4] => 2, [5] => 394800000)
[4] => Array([0] => 24700, [1] => 7, [2] => 237, [3] => 3, [4] => 4, [5] => 2089620000)
[5] => Array([0] => 18500, [1] => 5, [2] => 314, [3] => 5, [4] => 4, [5] => 1739000000)
[6] => Array([0] => 12150, [1] => 5, [2] => 247, [3] => 3, [4] => 3, [5] => 1142100000)
[7] => Array([0] => 15000, [1] => 5, [2] => 307, [3] => 4, [4] => 3, [5] => 1410000000)
[8] => Array([0] => 10000, [1] => 4, [2] => 231, [3] => 4, [4] => 4, [5] => 940000000)
[9] => Array([0] => 27500, [1] => 6, [2] => 347, [3] => 5, [4] => 4, [5] => 2147483647))
the table display is
5000 6 325 3 3 517000000
20000 5 217 5 3 1692000000
12150 3 254 4 4 1370520000
4200 4 351 3 2 394800000
24700 7 237 3 4 2089620000
18500 5 314 5 4 1739000000
12150 5 247 3 3 1142100000
15000 5 307 4 3 1410000000
10000 4 231 4 4 940000000
27500 6 347 5 4 2147483647
and the second array is
array2=
Array(
[0] => Array
(
[0] => 27500
[1] => 7
[2] => 351
[3] => 3
[4] => 4
[5] => 394800000
))
then, I want to do mathematical operation for each column.
for column 0,1,2,4 the operation is array1[][]/array2[][].
and for column 3 and 5 the operation is array2[][]/array1[][]
how do I do that ?
I have tried for this function :
function perkalian_matriks($array1, $array2) {
$result= array();
for ($i=0; $i<sizeof($array1); $i++) {
for ($j=0; $j<sizeof($array2[0]); $j++) {
$temp = 0;
$temp1 = 0;
$temp2 = 0;
for ($k=0; $k<sizeof($array2); $k++) {
if(isset($array1[$i][3]) || isset($array1[$i][5])){
$temp1 += ($array2[0][$j]/$array1[$i][$j])/2;
}else{
$temp2 += $array1[$i][$j]/$array2[0][$j];
}
$temp += $array1[$i][$j]/$array2[0][$j];
}
$result[$i][$j] = $temp;
$result1[$i][$j] = $temp1;
$result2[$i][$j] = $temp2;
}
}
return $result;
return $temp;
return $result1;
return $result2;
}
But the final result that I expected is
0.181818 0.857143 0.925926 1 0.75 0.763636
0.727273 0.714286 0.618234 0.6 0.75 0.233333
0.441818 0.428571 0.723647 0.75 1 0.288066
0.152727 0.571429 1 1 0.5 1
0.898182 1 0.675214 1 1 0.188934
0.672727 0.714286 0.894587 0.6 1 0.227027
0.441818 0.714286 0.703704 1 0.75 0.345679
0.545455 0.714286 0.874644 0.75 0.75 0.28
0.363636 0.571429 0.65812 0.75 1 0.42
1 0.857143 0.988604 0.6 1 0.183843
Thank you.
first of all doing this:
return $result;
return $temp;
return $result1;
return $result2;
will execute every time just return $result; so that really ve no meaning.
If called from within a function, the return statement immediately
ends execution of the current function, and returns its argument as
the value of the function call. ( from php doc )
The second array don't need to be a matrix.
anyway you can achieve what you need using 2 nested loop:
$array1=
array(array(5000,6, 325, 3, 3, 517000000),
array( 20000, 5, 217, 5, 3, 1692000000),
array( 12150, 3, 254, 4, 4, 1370520000),
array( 4200, 4, 351, 3, 2, 394800000),
array( 24700, 7, 237, 3, 4, 2089620000),
array( 18500, 5, 314, 5, 4, 1739000000),
array( 12150, 5, 247, 3, 3, 1142100000),
array( 15000, 5, 307, 4, 3, 1410000000),
array( 10000, 4, 231, 4, 4, 940000000),
array( 27500, 6, 347, 5, 4, 2147483647));
$array2=array(27500,
7,
351,
3,
4,
394800000);
$action1 = array(0,1,2,4);
$action2 = array(3,5);
$result = array();
foreach($array1 as $rowNum => $row){
$result[] = array();
foreach($row as $colNum => $col){
if(in_array($colNum,$action1))
$result[$rowNum][] = $col/$array2[$colNum];
else if(in_array($colNum,$action2)){
$result[$rowNum][] = $array2[$colNum]/$col;
}
}
}
echo var_dump($result);
Working Example
Related
I have an array with multiple similar minimum value.
May I know how to randomly get one of the minimum value?
Here is my sample code:-
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$b = array_keys($aryNo, min($aryNo)); //Here only can get 1 value.
$intNo = $b[0];
May I know how to get min value list (3 => 12, 4 => 12,5 => 12,10 => 12) and randomly pick one of them so that I can set in $intNo?
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$b = array_keys($aryNo, min($aryNo)); //Here only can get 1 value.
// Taking a random KEY from $b
$key = array_rand($b);
// Taking a KEY from $aryNo which is under `$key`
echo $b[$key];
// Taking a VALUE from `$aryNo` which is under `$b[$key]`
echo $aryNo[$b[$key]];
The fiddle.
Try something like this:
$aryNo = [34,34,34,51,12,12,12,56,876,453,43,12];
foreach($aryNo as $a) {
$finalArray[$a][] = $a;
}
print("<pre>".print_r($finalArray,true)."</pre>");
$minKey = min(array_keys($finalArray));
print("<pre>".print_r($finalArray[$minKey],true)."</pre>");
$randIndex = array_rand($finalArray[$minKey]);
print_r("Key: ".$randIndex.", ".$finalArray[$minKey][$randIndex]);
First print prints:
Array
(
[34] => Array
(
[0] => 34
[1] => 34
[2] => 34
)
[51] => Array
(
[0] => 51
)
[12] => Array
(
[0] => 12
[1] => 12
[2] => 12
[3] => 12
)
[56] => Array
(
[0] => 56
)
[876] => Array
(
[0] => 876
)
[453] => Array
(
[0] => 453
)
[43] => Array
(
[0] => 43
)
)
Than you select min key, and that prints this:
Array
(
[0] => 12
[1] => 12
[2] => 12
[3] => 12
)
At the end you pick random key from this array and print the value:
Key: 2, Value: 12
`<?php
$sortArr = array();
$aryNo = array(0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,5 =>
12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,10 => 12);
asort($aryNo);
$aryNo = array_values($aryNo);
print_r($aryNo);
echo $aryNo[0];
?>`
I found that if I using shuffle(); also work for me.
Here is my example:-
$aryNo = array(
0 => 34, 1 => 34, 2 => 51, 3 => 12, 4 => 12,
5 => 12, 6 => 56, 7 => 876, 8 => 453, 9 => 43,
10 => 12
);
$aryNo2 = array_keys($aryNo, min($aryNo));
shuffle($aryNo2); //
$intWinNo = $aryNo2[0];
Thanks #u_mulder suggestion & answer.
I have this array which contains 2 keys and values..
array (
23 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
),
1 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 14,
8 => 12,
9 => 1,
10 => 7,
11 => 10,
12 => 8,
13 => 3,
)
)
So How can I convert this into single query with distinct values like this
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
20 => 14,
)
That means it should be first merge and then create distinct values array without using more foreach/for loop.
This is the code I have tried http://codepad.org/x881cBt1
You can use array_walk_recursive to flatten your multidimensional array:
$flatten = [];
array_walk_recursive($array, function ($value) use (&$flatten) {
$flatten[] = $value;
});
$flatten = array_unique($flatten); //Taking Unique for the flattened array
print_r($flatten);
This should give:
Array
(
[0] => 9
[1] => 13
[2] => 2
[3] => 11
[4] => 4
[5] => 5
[6] => 6
[7] => 12
[8] => 1
[9] => 7
[10] => 10
[11] => 8
[12] => 3
[20] => 14
)
Check EVAL
<?php
$data; //Your array
$data_set = array_values($data);
$required_data = [];
for ($i=0; $i< count($data_set); $i++) {
$required_data = array_merge($required_data, $data_set[$i]);
unset($data_set[$i]);
}
var_dump($required_data);
This will solve the issue
$arr;//Your array
$final_arr=array();
foreach($arr as $r){
$r=array_unique($r);
$final_arr=array_merge($final_arr,$r);
}
$final_arr=array_unique($final_arr);
print_r($final_arr);
see it live ideone
//Here 1st you have to Merge array & then remove duplicate entry
<?php
$main_array=array("0"=>array(1,2,3,4),"1"=>array(2,6,4),"2"=>array(2,8,5));
$temp_array=array();
for($i=0;$i<count($main_array);$i++)
{
$temp_array=array_merge($temp_array,$main_array[$i]);
}
//var_dump($temp_array);
$final_array=array_unique($temp_array);
echo "array:";
var_dump($final_array);
?>
you can use this code as you said this code is without any loop
$myArray = array (
23 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
),
1 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 14,
8 => 12,
9 => 1,
10 => 7,
11 => 10,
12 => 8,
13 => 3,
)
);
$objTmp = (object) array('array' => array());
array_walk_recursive($myArray, create_function('&$v, $k, &$t', '$t->array[] = $v;'), $objTmp);
print_r (array_unique($objTmp->array));
/* output
Array
(
[0] => 9
[1] => 13
[2] => 2
[3] => 11
[4] => 4
[5] => 5
[6] => 6
[7] => 12
[8] => 1
[9] => 7
[10] => 10
[11] => 8
[12] => 3
[20] => 14
)
*/
Thank you..
[a, 1, 3, 9, 0, 13]
[b, 5, 6, 0, 0, 11]
[j, 0, 6, 2, 1, 9]
[c, 1, 0, 8, 5, 14]
[d, 0, 0, 0, 17, 17]
[e, 0, 5, 0, 0, 5]
[h, 0, 0, 3, 3, 6]
The array needs to be sorted on
Ascending order of number of zeroes.
Ascending order of last element value.
So the above array after sorting should look like,
[j, 0, 6, 2, 1, 9]
[a, 1, 3, 9, 0, 13]
[c, 1, 0, 8, 5, 14]
[h, 0, 0, 3, 3, 6]
[b, 5, 6, 0, 0, 11]
[d, 0, 5, 0, 0, 5]
[d, 0, 0, 0, 17, 17]
I am sorting the multidimensional array normally against the last value via this code
function multiarraysorter($arr, $index) {
$b = array();
$c = array();
foreach ($arr as $key => $value) {
$b[$key] = $value[$index];
}
asort($b);
foreach ($b as $key => $value) {
$c[] = $arr[$key];
}
return $c;
}
Any ideas how to accomplish the first sort that is based on number of zeroes of the values?
You can use usort for tasks like this:
$arr=[['a', 1, 3, 9, 0, 13],['b', 5, 6, 0, 0, 11],['j', 0, 6, 2, 1, 9],['c', 1, 0, 8, 5, 14],['d', 0, 0, 0, 17, 17],['e', 0, 5, 0, 0, 5],['h', 0, 0, 3, 3, 6]];
usort($arr,function($a,$b){
$infoa=array_count_values($a);
$infob=array_count_values($b);
if(empty($infoa[0])) $infoa[0]=0;
if(empty($infob[0])) $infob[0]=0;
if($infoa[0]==$infob[0])
{
return end($a)-end($b);
}
else
{
return $infoa[0]-$infob[0];
}
});
print_r($arr);
3v4l.org demo
The generated output is:
Array
(
[0] => Array
(
[0] => j
[1] => 0
[2] => 6
[3] => 2
[4] => 1
[5] => 9
)
[1] => Array
(
[0] => a
[1] => 1
[2] => 3
[3] => 9
[4] => 0
[5] => 13
)
[2] => Array
(
[0] => c
[1] => 1
[2] => 0
[3] => 8
[4] => 5
[5] => 14
)
[3] => Array
(
[0] => h
[1] => 0
[2] => 0
[3] => 3
[4] => 3
[5] => 6
)
[4] => Array
(
[0] => b
[1] => 5
[2] => 6
[3] => 0
[4] => 0
[5] => 11
)
[5] => Array
(
[0] => e
[1] => 0
[2] => 5
[3] => 0
[4] => 0
[5] => 5
)
[6] => Array
(
[0] => d
[1] => 0
[2] => 0
[3] => 0
[4] => 17
[5] => 17
)
)
Try This its working :
<?php
$res_ary = array();
$res_ary = sortArray($arr,'key_value');
?>
in sortArray function you pass two parameters first one is array you want to short and second one is key value on which you have to do sorting.
<?php
function sortArray($arrData, $p_sort_field, $p_sort_type = false )
{
if(!empty($arrData))
{
foreach($arrData as $data)
{
$newData [] = $data;
}
for($i=0; $i<count($newData); $i++)
{
$ar_sort_field[$i]=$newData[$i][$p_sort_field];
}
array_multisort($ar_sort_field, ($p_sort_type ? SORT_DESC : SORT_ASC), $newData);
return $newData;
}
}
?>
i got an array like this with unsorted outer index.
$a = array(
(1) => array(1, 2, 3, 0, 5, 4),
(0) => array(2, 1, 5, 0, 3, 4)
);
echo "<br/>Before Sorting: ";
print_r($a);
foreach($a as $b)
array_multisort($b, SORT_ASC, SORT_NUMERIC);
echo "<br/>After Sorting: ";
print_r($a);
which gives me output as below
Before Sorting:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => 5
[5] => 4
)
[0] => Array
(
[0] => 2
[1] => 1
[2] => 5
[3] => 0
[4] => 3
[5] => 4
)
)
After Sorting:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => 5
[5] => 4
)
[0] => Array
(
[0] => 2
[1] => 1
[2] => 5
[3] => 0
[4] => 3
[5] => 4
)
)
AND WHAT I WANT IS
$a = array(
(0) => array(2, 1, 5, 0, 3, 4),
(1) => array(1, 2, 3, 0, 5, 4)
);
please tell me how to deal with.........
How about just using ksort (as you need to reorder your array by key)?
$a = array(
1 => array(1, 2, 3, 0, 5, 4),
0 => array(2, 1, 5, 0, 3, 4)
);
echo "<br/>Before Sorting: ";
print_r($a);
ksort($a);
echo "<br/>After Sorting: ";
print_r($a);
Since you're only wanting to sort by the top level indices, you don't need to use multisort. You don't even need a loop.
Try this:
$a = array(
(1) => array(1, 2, 3, 0, 5, 4),
(0) => array(2, 1, 5, 0, 3, 4)
);
ksort($a);
print_r($a);
Should give you what you want.
See http://www.php.net/manual/en/function.ksort.php for more info.
It feels I am going way over my head while discovering the ultimate usage of arrays.
I have two arrays, where the first has main keys, and the value is a count of files attached to that key.
The goal is to match the keys of this first array to the values in a second array, but still mainting (and show) the (value)count of Array-1 -- but for only the values in the second array.
Seems somewhat hazy perhaps, but here are the arrays. The second one has the values that should match the keys in the first.
(My problem is that I keep losing the values of array 1 with every attempt I make.)
Hope you can help me out with this one.
(working matches are keys like: 125, 2051 & 2214)
Array 1:
Array (
[6960] => 3
[2214] => 4
[2051] => 4
[6944] => 2
[6938] => 4
[1823] => 1
[766] => 6
[3993] => 4
[5896] => 6
[6927] => 2
[4220] => 3
[77] => 3
[83] => 1
[125] => 2
[6618] => 2
[196] => 1
[4072] => 12
[3718] => 1
[5918] => 1
[3388] => 10
[4500] => 13
[5968] => 2
[3000] => 2
[942] => 1
[4246] => 8
[5868] => 2
[6394] => 3
[1168] => 1
[2163] => 1
[1827] => 2
[2071] => 8
[4597] => 1
[1702] => 7
)
Array 2:
Array (
[0] => 1024
[1] => 1076
[2] => 111
[3] => 124
[4] => 125
[5] => 1301
[6] => 1409
[7] => 2051
[8] => 2214
[9] => 2636
[10] => 3246
[11] => 4838
[12] => 6946
[13] => 6955
[14] => 6961
[15] => 73
[16] => 74
[17] => 8
)
What about doing this:
<?php
$arr1 = array(1 => 1000, 500 => 1111, 1000 => 5000, 5000 => 5555);
$arr2 = array(1, 5000);
print_r(array_intersect_key($arr1, array_flip($arr2)));
OUTPUT:
(
[1] => 1000
[5000] => 5555
)
Or, using your data:
<?php
$arr1 = array(6960 => 3, 2214 => 4, 2051 => 4, 6944 => 2, 6938 => 4, 1823 => 1, 766 => 6, 3993 => 4, 5896 => 6, 6927 => 2, 4220 => 3, 77 => 3, 83 => 1, 125 => 2, 6618 => 2, 196 => 1, 4072 => 12, 3718 => 1, 5918 => 1, 3388 => 10, 4500 => 13, 5968 => 2, 3000 => 2, 942 => 1, 4246 => 8, 5868 => 2, 6394 => 3, 1168 => 1, 2163 => 1, 1827 => 2, 2071 => 8, 4597 => 1, 1702 => 7);
$arr2 = array(1024, 1076, 111, 124, 125, 1301, 1409, 2051, 2214, 2636, 3246, 4838, 6946, 6955, 6961, 73, 74, 8);
print_r(array_intersect_key($arr1, array_flip($arr2)));
OUTPUT:
Array
(
[2214] => 4
[2051] => 4
[125] => 2
)
array_interset_keys will find the intersection of arrays by keys, not values. Since your second array is an index based array (not an associative array) we need to first flip the keys and values using array_flip. Then the keys can be intersected.
Your question is somewhat unclear, but I think this is what you're looking for:
foreach( $array2 as $key)
{
$count = ( isset( $array1[ $key ]) ? $array1[ $key ] : 0);
echo $key . ' has ' . $count . ' files.';
}
Uhhmm.. i cant seem to understand what you want to imply.. but from the way i see it.. if you want to have the keys of array 1 as value to array 2.. just do this code..
foreach($array1 as $key=>$val) {
$array2[] = $key;
}
This should grab the KEYS of array1 and insert it to your array2[].
Hope this helps you.. Cheers :)
This should print out what you need:
foreach($array2 as $key=>$val) {
echo $val;
foreach($array1 as $key2 => $val2){
if($key == $val2){
echo $val2;
}
}
echo '\n'; // new line
}