How to randomly get min value from Array? - php

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.

Related

Array_combine error: puts out the wrong values and gives only the unique values php

hi all i have 2 arrays
[0] => CD Alaves
[1] => Granada CF
[2] => Getafe
[3] => CD Leganes
[4] => Barcelona
[5] => Getafe
[6] => Atletico Madrid
[7] => Getafe
[8] => Sevilla
[9] => Athletic Bilbao
[10] => CD Leganes
and their corresponding values
[0] => 11
[1] => 11
[2] => 11
[3] => 11
[4] => 11
[5] => 10
[6] => 10
[7] => 10
[8] => 10
[9] => 10
[10] => 9
I am currently using
$teamdata=array_combine($clubs,$stats);
echo "<pre>";
print_r($teamdata);
echo "</pre>";
however it outputs as
[CD Alaves] => 1
[Granada CF] => 1
[Getafe] => 1
[CD Leganes] => 1
[Barcelona] => -
[Atletico Madrid] => 1
[Sevilla] => 1
[Athletic Bilbao] => -
[Real Betis] => 1
[Espanyol] => 1
[Osasuna] => 1
[Villarreal] => 1
[Real Madrid] => 1
[Levante] => 1
giving unique values on the left and 1s on the right
it should read
[CD Alves]=>11
[Granada CF]=>11
....
Thanks a lot to anyone who sees this and takes the time to respond.
As long as your two originating arrays are valid, then array_combine works just fine:
<?php
$arr1 = array(
0 => "CD Alaves",
1 => "Granada CF",
2 => "Getafe",
3 => "CD Leganes",
4 => "Barcelona",
5 => "Getafe",
6 => "Atletico Madrid",
7 => "Getafe",
8 => "Sevilla",
9 => "Athletic Bilbao",
10 => "CD Leganes"
);
$arr2 = array(
0 => 11,
1 => 11,
2 => 11,
3 => 11,
4 => 11,
5 => 10,
6 => 10,
7 => 10,
8 => 10,
9 => 10,
10 => 9
);
$res = array_combine($arr1, $arr2);
print_r($res);
// Gives:
Array
(
[CD Alaves] => 11
[Granada CF] => 11
[Getafe] => 10
[CD Leganes] => 9
[Barcelona] => 11
[Atletico Madrid] => 10
[Sevilla] => 10
[Athletic Bilbao] => 10
)
A working example: http://sandbox.onlinephpfunctions.com/code/a6da80745eff9ff9b598fe4a2d7fbd8d71d3fb08
Even if your arrays are single values without explicit keys, it will work (php adds numeric indexes anyway)
<?php
$arr1 = array(
"CD Alaves",
"Granada CF",
"Getafe",
"CD Leganes",
"Barcelona",
"Getafe",
"Atletico Madrid",
"Getafe",
"Sevilla",
"Athletic Bilbao",
"CD Leganes"
);
$arr2 = array(
11,
11,
11,
11,
11,
10,
10,
10,
10,
10,
9
);
$res = array_combine($arr1, $arr2);
print_r($res);
// Prints the same output as above.
I had the exact same problem and this worked for me. Please mark this answer as accepted if it works for you.
Code from #axiac on https://stackoverflow.com/a/46671863/19402660
// The input arrays
$Array1 = ['bus', 'bus', 'int'];
$Array2 = [2, 18, 10];
// Build the result here
$Array3 = [];
// There is no validation, the code assumes that $Array2 contains
// the same number of items as $Array1 or more
foreach ($Array1 as $index => $key) {
// If the key $key was not encountered yet then add it to the result
if (! array_key_exists($key, $Array3)) {
$Array3[$key] = 0;
}
// Add the value associate with $key to the sum in the results array
$Array3[$key] += $Array2[$index];
}
print_r($Array3);
Its output:
Array
(
[bus] => 20
[int] => 10
)

PHP array with dynamic index

I tried to make table from PHP array. But here all key are dynamic and have also child array
Here is the array structure:
array (
1371618448317 =>
array (
0 =>
array (
0 => '23.77311734',
1 => '90.396355125',
2 => '23.77313316',
3 => '90.396411867187',
4 => '23.77309048',
5 => '90.396419484375',
6 => '23.77307348',
7 => '90.3963645',
),
1 => 20911,
2 =>
array (
1371618713208 =>
array (
0 => 1,
1 => 'BRAC Delivery Centre',
2 => '371/A Shahinbag',
3 => 25,
4 => 91,
5 => 221,
6 => 1,
7 => 11,
8 => 1,
9 => 99,
10 => 1,
11 => 99,
12 => 99,
13 => 99,
14 => 99,
15 => 99,
16 => 1,
),
),
),
1371619410448 =>
array (
0 =>
array (
0 => '23.77894566',
1 => '90.39968559375',
2 => '23.77916362',
3 => '90.400307765625',
4 => '23.77889887',
5 => '90.400401515625',
6 => '23.77870083',
7 => '90.399780515625',
),
1 => 24612,
2 =>
array (
1371619950162 =>
array (
0 => 1,
1 => 'EPI Centre (Govt.)',
2 => ' Mohakhali Road Mohakhali',
3 => 20,
4 => 91,
5 => 11,
6 => 1,
7 => 12,
8 => 1,
9 => 99,
10 => 1,
11 => 99,
12 => 99,
13 => 99,
14 => 99,
15 => 99,
16 => 0,
),
),
),
1371621080807 =>
array (
0 =>
array (
0 => '23.77746206',
1 => '90.399232078125',
2 => '23.77744917',
3 => '90.399623390625',
4 => '23.77712934',
5 => '90.39958940625',
6 => '23.77714809',
7 => '90.399205125',
),
1 => 24566,
2 =>
array (
1371621897771 =>
array (
0 => 1,
1 => 'Society for Assistance to Hearing Impaired Children (SAHIC)',
2 => 'N/A Sattola Road Mohakhali',
3 => 20,
4 => 91,
5 => 222,
6 => 1,
7 => 7,
8 => 1,
9 => 1,
10 => 1,
11 => 1,
12 => 99,
13 => 99,
14 => 99,
15 => 1,
16 => 0,
),
),
),
1371622305777 =>
array (
0 =>
array (
0 => '23.77357261',
1 => '90.36189965625',
2 => '23.77359605',
3 => '90.36197925',
4 => '23.77344028',
5 => '90.36201675',
6 => '23.77341802',
7 => '90.361931296875',
),
1 => 1325,
2 =>
array (
1371622497359 =>
array (
0 => 1,
1 => 'Natoinal Health Care Network',
2 => '3/Ka Pisiculture Housing Society Shamoly',
3 => 29,
4 => 91,
5 => 222,
6 => 1,
7 => 7,
8 => 1,
9 => 99,
10 => 99,
11 => 1,
12 => 99,
13 => 99,
14 => 1,
15 => 1,
16 => 0,
),
),
)
My Desire tale will be look like:
BRAC Delivery Centre 371/A Shahinbag 23.77311734 90.396355125
EPI Centre (Govt.) Mohakhali Road 23.77746206 90.399232078125
from lat long array I always try to collect first lat long.
Can any one give me solution ?
Here is your solution
Input
$array = array(
1371618448317 => array (
array ('23.77311734','90.396355125','23.77313316','90.396411867187','23.77309048','90.396419484375','23.77307348','90.3963645'),
20911,
array ('1371618713208' => array (1,'BRAC Delivery Centre','371/A Shahinbag',25,91,221,1,11,1,99,1,99,99,99,99,99,1))
),
1371619410448 => array (
array ('23.77894566','90.39968559375','23.77916362','90.400307765625','23.77889887','90.400401515625','23.77870083','90.399780515625',),
24612,
array ('1371619950162' => array (1,'EPI Centre (Govt.)',' Mohakhali Road Mohakhali',20,91,11,1,12,1,99,1,99,99,99,99,99,0))
),
1371621080807 => array(
array ('23.77746206','90.399232078125','23.77744917','90.399623390625','23.77712934','90.39958940625','23.77714809','90.399205125'),
24566,
array ('1371621897771' => array (1,
'Society for Assistance to Hearing Impaired Children (SAHIC)','N/A Sattola Road Mohakhali',20,91,222,1,7,1,1,1,1,99,99,99,1,0)
)
),
1371622305777 => array (
array ('23.77357261','90.36189965625','23.77359605','90.36197925','23.77344028','90.36201675','23.77341802','90.361931296875'),
1325,
array ('1371622497359' => array (1,
'Natoinal Health Care Network','3/Ka Pisiculture Housing Society Shamoly',29,91,222,1,7,1,99,99,1,99,99,1,1,0)
)
)
);
Solution
Coordinates are in 1st array.
Name and address is in 3rd.
Coordinates picks easily by $row[0][0].','.$row[0][1]; in given loop below.
For address and name you need to pick key of 3rd array, for this here I used $row[2][key($row[2]);
Now check the code below.
$new_array = array();
$i=0;
foreach($array as $key => $row){
//echo "<pre>";print_r(key($row[2]));
$new[$i]['coordinates'] = $row[0][0].','.$row[0][1];
$new[$i]['name'] = $row[2][key($row[2])][1];
$new[$i]['address'] = $row[2][key($row[2])][2];
$i++;
}
echo "<pre>";print_r($new);
Output
Array
(
[0] => Array
(
[coordinates] => 23.77311734,90.396355125
[name] => BRAC Delivery Centre
[address] => 371/A Shahinbag
)
[1] => Array
(
[coordinates] => 23.77894566,90.39968559375
[name] => EPI Centre (Govt.)
[address] => Mohakhali Road Mohakhali
)
[2] => Array
(
[coordinates] => 23.77746206,90.399232078125
[name] => Society for Assistance to Hearing Impaired Children (SAHIC)
[address] => N/A Sattola Road Mohakhali
)
[3] => Array
(
[coordinates] => 23.77357261,90.36189965625
[name] => Natoinal Health Care Network
[address] => 3/Ka Pisiculture Housing Society Shamoly
)
)
solved myself:
$assoc = true;
$result = json_decode ($json, $assoc);
echo "<table border='1'>";
echo "<tr><td>Hospital Name</td><td>Address</td><td>Lat1</td><td>Long1</td><td>Lat2</td><td>Long1</td><td>Lat3</td><td>Long3</td><td>Lat4</td><td>Long4</td></tr>";
foreach($result as $single_res){
$lat1 = $single_res[0][0];
$long1 = $single_res[0][1];
$lat2 = $single_res[0][2];
$long2 = $single_res[0][3];
$lat3 = $single_res[0][4];
$long3 = $single_res[0][5];
$lat4 = $single_res[0][6];
$long4 = $single_res[0][7];
$key = $single_res[2];
$key_val = array_keys($key);
$key_val = $key_val[0];
$name = $key[$key_val][1];
$address = $key[$key_val][2];

How to merge multidimentional array into single array?

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..

I want to add one array key and value with another array values

I want to add one array key and value with another array's value.
If I have arrays as follow:
Array ( [6] => 12 [8] => 9 [10] => 11 )
Array ( [6] => 70 [8] => 10 [9] => 35 [10] => 25 [11] => 25 [12] => 2 )
For example (6 => 12 when processed result in 70 + 2 )
This is the expected output with the two arrays above:
array ([0] => 72 [1] => 45 [2] => 50 )
Thanks In Advance
Use the following simple solution:
$arr1 = [6 => 12, 8 => 9 , 10 => 11];
$arr2 = [6 => 70, 8 => 10, 9 => 35, 10 => 25, 11 => 25, 12 => 2];
$result = [];
foreach ($arr1 as $k => $v) {
if (isset($arr2[$k]) && isset($arr2[$v])) {
$result[] = $arr2[$k] + $arr2[$v];
}
}
print_r($result);
The output:
Array
(
[0] => 72
[1] => 45
[2] => 50
)
$array1 = Array ( 6 => 12, 8 => 9, 10 => 11 );
$array2 = Array ( 6 => 70, 8 => 10, 9 => 35, 10 => 25, 11 => 25, 12 => 2 );
$newarray = array();
foreach ($array1 as $key => $item) {
$newarray[] = $array2[$item] + $array2[$key];
}
var_dump($newarray);

Array matches with values on keys

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
}

Categories