Array matches with values on keys - php

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
}

Related

How can I combine 2 array in php or laravel

I would like to combine 2 arrays into 1 in PHP or laravel. I've searched this site for similar questions but can't seem to find an answer.
Can someone help me with this?
**array 1 -- $insertData **
Array
(
[0] => Array
(
[prid] => 4
[vendor_id] => 1
)
[1] => Array
(
[prid] => 5
[vendor_id] => 2
)
)
**Array - 2 $requestData **
Array
(
[vendor_id] => Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 2
)
[item] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
[qty] => Array
(
[0] => 12
[1] => 13
[2] => 14
[3] => 15
)
)
**Required Output ---- how can I do this array1 and array2 combine into a single array **
Array
(
[0] => Array
(
[prid] => 4
[vendor_id] => 1
[item] => 2
[qty] => 12
)
[1] => Array
(
[prid] => 4
[vendor_id] => 1
[item] => 3
[qty] => 13
)
[2] => Array
(
[prid] => 5
[vendor_id] => 2
[item] => 4
[qty] => 14
)
[3] => Array
(
[prid] => 5
[vendor_id] => 2
[item] => 5
[qty] => 15
)
)
My controller
public function prtmulti(Request $req)
{
$maxPrId = newpr::max('prid');
// print_r($maxPrId);
echo "<pre>";
$requestData = $req->all();
if (array_key_exists("vendor_name", $requestData)) {
$insertData = [];
$uniqueData = array_unique($requestData["vendor_name"]);
foreach ($uniqueData as $key => $value) {
$maxId = $maxPrId+1;
$insertData[] = ['prid' => $maxId, 'vendor_id' => $value];
$maxPrId = $maxPrId+1;
}
}
print_r($insertData);
print_r($requestData);
}
you can achieve this using the array_combine function in php, for example:
<?php
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_combine($fname,$age);
print_r($c);
?>
I'm pretty sure that Laravel doesn't offer anything out of the box to execute your desired merging technique (and I don't see why it would bother).
Assuming that the vendor_id values in the first array are unique, you will get best performance by creating a lookup array. array_column() can be used to declare an array with vendor_id values as keys and prid values as values.
Because your $requestData has rows with the number of columns desired in the output, loop over the $requestData['vendor_id'] data and manually generate the desired rows of data in the result array.
Code: (Demo)
$insertData = [
['prid' => 4, 'vendor_id' => 1],
['prid' => 5, 'vendor_id' => 2],
];
$requestData = [
'vendor_id' => [1, 1, 2, 2],
'item' => [2, 3, 4, 5],
'qty' => [12, 13, 14, 15]
];
$insertLookup = array_column($insertData, 'prid', 'vendor_id');
$result = [];
foreach ($requestData['vendor_id'] as $index => $vendorId) {
$result[] = [
'prid' => $insertLookup[$vendorId],
'vendor_id' => $vendorId,
'item' => $requestData['item'][$index],
'qty' => $requestData['qty'][$index],
];
}
var_export($result);
Output:
array (
0 =>
array (
'prid' => 4,
'vendor_id' => 1,
'item' => 2,
'qty' => 12,
),
1 =>
array (
'prid' => 4,
'vendor_id' => 1,
'item' => 3,
'qty' => 13,
),
2 =>
array (
'prid' => 5,
'vendor_id' => 2,
'item' => 4,
'qty' => 14,
),
3 =>
array (
'prid' => 5,
'vendor_id' => 2,
'item' => 5,
'qty' => 15,
),
)
You can use the array_merge() function to merge arrays.
array_merge
$merged_array = array_merge($insertData, $requestData);

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
)

How to randomly get min value from Array?

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.

PHP Multidimensional Array Sort & Trim

I have the array below. I do not know how I can sort secondary array so elements with highest value gets to be first and I want to trim if the array length is more than 20 elements. I want first dimension to be sorted by alphabetical, secondary dimension to be sorted by values and if there are more than 20 entries, I want only first 20 highest value.
(
[a] => Array
(
[option1] => 2
[option2] => 3
[option3] => 1
[option4] => 7
[option5] => 8
[option6] => 3
[option7] => 2
[option8] => 32
[option9] => 35
[option10] => 33
[option11] => 32
[option12] => 35
[option13] => 37
[option14] => 3
[option15] => 39
[option16] => 4
[option17] => 36
[option18] => 31
[option19] => 12
[option20] => 35
[option21] => 3
[option22] => 32
[option23] => 31
)
[b] => Array
(
[option16] => 4
[option17] => 36
[option18] => 31
[option19] => 12
)
Php has everything for you!
<?php
$array = array(
'b' => array(
'option1' => 33,
'option2' => 12,
'option3' => 17,
'option4' => 44,
'option5' => 543,
'option6' => 56,
'option7' => 8,
'option8' => 0,
'option9' => -10,
'option10' => 234,
'option11' => 67,
'option12' => 99,
'option13' => 3363,
'option14' => 912,
'option15' => 51,
'option16' => 42,
'option17' => 105,
'option18' => 80,
'option19' => 44,
'option20' => 0,
'option21' => 15,
),
'a' => array(
'option1' => 88,
'option2' => 0,
'option3' => -23,
'option4' => 16,
'option5' => 76,
),
);
// sorts array keys alphabetically
ksort($array, SORT_NATURAL);
// iterrate each key (a or b etc.) => value (value is assoc array of options) pair
foreach ($array as $k => &$v) {
// reverse sort (from max to min) options by its value
// with preserve keys
arsort($v);
// cut options array to max 20 element
$v = array_slice($v, 0, 20);
}
echo '<pre>' . print_r($array, 1) . '</pre>';
Online runnable.

Sort an array according to the value in php

Here is my array . I want to sort the array according the value of each key
Input array:-
Array
(
[location_classroom] => 209
[location_daily_pe] => 1
[location_hallways] => 3
[location_playground] => 93
[location_shade_area] => 26
[location_specialist] => 8
[location_toilet] => 3
[location_others] => 27
[location_others_info] => 0
)
Output array:-
Array
(
[location_others_info] => 0
[location_daily_pe] => 1
[location_hallways] => 3
[location_toilet] => 3
[location_specialist] => 8
[location_shade_area] => 26
[location_playground] => 93
[location_classroom] => 209
[location_others] => 27
)
Indeed you should use asort():
$arr = [
'location_classroom' => 209,
'location_daily_pe' => 1,
'location_hallways' => 3,
'location_playground' => 93,
'location_shade_area' => 26,
'location_specialist' => 8,
'location_toilet' => 3,
'location_others' => 27,
'location_others_info' => 0
];
asort($arr);
# but you can't print the output of the sorting - it'll give you nothing meaningful (boolean)
# you should print the sorted array itself
print_r($arr);
OUTPUT
Array
(
[location_others_info] => 0
[location_daily_pe] => 1
[location_toilet] => 3
[location_hallways] => 3
[location_specialist] => 8
[location_shade_area] => 26
[location_others] => 27
[location_playground] => 93
[location_classroom] => 209
)

Categories