There are 2 arrays, the reason for 2 arrays is because of 2 drop down from form post.
array1
1 => 878
2 => 983
3 => 717
array2
1 => 10
2 => 15
3 => 12
I have to loop 2 arrays and get where the keys matched and combine them into an array.
878 => 10
983 => 15
717 => 12
Thanks!
using the array_combine. try it here
$combined = array_combine(array_values($array1),array_values($array2));
Related
I have an array of id's and I want to filter those id's to last 5 and unique ids.
$recently_viewed_ids
array:16 [▼
0 => 1
1 => 2
2 => 1
3 => 2
4 => 8
5 => 7
6 => 6
7 => 6
8 => 6
9 => 5
10 => 8
11 => 4
12 => 1
13 => 1
14 => 1
15 => 1
]
Here is my code and it's messing up because I'm getting 85672
$items = array_slice(array_unique(array_reverse($recently_viewed_ids)), -5);
Output I am expecting
14856
You need to use next combination of array functions:
array_slice( // get first 5 values
array_unique( // get only unique values
array_reverse($arr) //reverse array for get last values
)
,0,5);
Code example here: PHPize.online
In Laravel, I believe you can rewrite the correct answer to:
return collect($arr)
->reverse()
->unique()
->slice(0, 5)
->all();
try this :
$items = array_slice(array_unique(array_reverse($recently_viewed_ids)), 5);
I have one big array circa 50 values like this (for example I used smaller array):
Array
(
[Adam Małysz] => 1
[Justyna Kowalczyk] => 2
[Janne Ahonen] => 3
[Stefan Hula] => 4
[Ole Einar Bjoerdalen] => 5
[Jakub Janda] => 6
[Mariusz Pudzianowski] => 7
[Harry Potter] => 8
[Vladimir Zografski] => 9
[Pavel Karelin] => 10
[Eddie Edwards] => 11
[Apoloniusz Tajner] => 12
)
and I have n smaller constant arrays with different values:
Array
(
[Adam Małysz] => 1
[Janne Ahonen] => 2
[Stefan Hula] => 3
[Ole Einar Bjoerdalen] => 4
[Vladimir Zografski] => 5
[Pavel Karelin] => 6
[Apoloniusz Tajner] => 7
[Mariusz Pudzianowski] => 8
[Jakub Janda] => 9
[Harry Potter] => 10
)
Array
(
[Justyna Kowalczyk] => 1
[Apoloniusz Tajner] => 2
[Harry Potter] => 3
[Janne Ahonen] => 4
[Mariusz Pudzianowski] => 5
[Adam Małysz] => 6
[Jakub Janda] => 7
[Ole Einar Bjoerdalen] => 8
[Vladimir Zografski] => 9
[Pavel Karelin] => 10
)
Array
(
[Adam Małysz] => 1
[Janne Ahonen] => 2
[Jakub Janda] => 3
[Stefan Hula] => 4
[Ole Einar Bjoerdalen] => 5
[Justyna Kowalczyk] => 6
[Harry Potter] => 7
[Mariusz Pudzianowski] => 8
[Vladimir Zografski] => 9
[Apoloniusz Tajner] => 10
)
So I want to subtract array values where is the same key like:
Big Array: [Harry Potter] => 8
[Mariusz Pudzianowski] => 7
Small Array #1: [Harry Potter] => 10
[Mariusz Pudzianowski] => 8
Small Array #2: [Harry Potter] => 3
[Mariusz Pudzianowski] => 5
Output: Difference between Small Array #1 and Big Array for key [Harry
Potter] = 2
Difference between Small Array #1 and Big Array for key [Mariusz Pudzianowski] = 1
Output: Difference between Small Array #2
and Big Array for key [Harry Potter] = 5
Difference between Small Array #2 and Big Array for key [Mariusz Pudzianowski] = 2
Basically I'm stuck.
Please help.
lets name your big array as $bigArray and small array as $smallArray both having key-value pair. Now the code should be:
foreach($bigArray as $bigKey=>$bigValue){
foreach($smallArray as $smallKey=>$smallValue){
if($smallKey==$bigKey){
$output=$bigValue-$smallValue;
echo 'Difference between Small Array #1 and Big Array for $bigKey is '.$output;
}
}
}
This way for every element of your big Array , you will loop small array and check if the keys are matching and if they do then you subtract their values and print them. You can do this for other small arrays as well.
You could use array_intersect_key to find all the key that are common to all arrays.
$commonKeys = array_intersect_key($bigArray, $smallArray1, $smallArray2, ...)
From there you just have to iterate over $commonKeys and make the appropriate substractions. Something like :
foreach ($commonKeys as $key) {
$result = abs($bigArray[$key] - $smallArray1[$key]);
echo " Difference between Small Array #1 and Big Array for key [$key] = $result";
//you do that for each Small Array
}
I am trying to extract only array items that match on specific fields and then I need to merge the two into one array only containing the data I need.
I have two arrays, and their structure is like below. Only product_count s where CATEGORY_ID of Array #1 match the Key of Array #2 should be the output.
Array #1: - Many of Array #1 CATEGORY_IDs will not exist in Array #2 key field.
0 => Array (4)
0 => 3
CATEGORY_ID => 10
1 => 1
product_count => 8
1 => Array (4)
0 => 4
CATEGORY_ID => 111
1 => 6
product_count => 109
...
Array #2:
10 => Category Name 1
110 => Category Name 2
8 => Category Name 3
109 => Category Name 4
111 => Category Name 5
3 => Category Name 6
132 => Category Name 7
...
Final Output should look something like: and I might be going about this all wrong, so I am open to any suggestions..
10 => [0] => Category Name 1
[1] => 8 // product_count
111 => [0] => Category Name 5
[1] => 109 // product_count
...
I am running a foreach() to extract product counts per category. As you can see my two arrays reflect this by the data.
You'd have to do a foreach loop, I believe:
$new_array = array();
foreach($array1 as $part) {
$new_array[$part["CATEGORY_ID"]] = array( $array2[$part["CATEGORY_ID"]], $part["product_count"] );
}
I think I got the gist of what you want. I don't think there is an actual PHP function to do what you want, either.
Array
(
[university] => 57
[iit] => 57
[jee] => 44
[application] => 28
[study] => 26
[college] => 23
[exam] => 19
[colleges] => 19
[view] => 19
[amp] => 18
)
How can I get an array with the first 5 elements?
Use array_slice() function:
$newArray = array_slice($originalArray, 0, 5, true);
If you need the first 5 elements
by order of keys: use ksort(),
by order of values: use sort()
before the array_slice(). Insert a letter 'r' to the second place for reverse order: krsort(), arsort().
I'm trying to get the difference between two arrays, but with array_diff, array_diff_assoc, or array_diff_key I can't get what I want..
Array 1 :
0 => 424012,
1 => 423000,
2 => 425010,
3 => 431447,
4 => 421001,
5 => 421002,
Array 2 :
0 => 424012,
1 => 423000,
2 => 425010,
3 => 431447,
4 => 431447,
5 => 421001,
6 => 421002,
array_diff = array ()
// empty
jarray_diff_assoc = array (
4 => 431447,
5 => 421001,
6 => 421002,
)
// OK but too much :)
array_diff_key = array(
6 => 421002
)
// nope i don't want that :(
I want 431447, cause it's only one time in the first array and twice in the second.
Regards, Tony
Is that exactly what you want? Only those that occur one time in the first, and two times in the second?
You can basically write your own function for that. Search through the second array, get a list of values that occur two times (or more than once, depending on what it is that you actually want), and then search for those in the first one (this you can do using a built-in PHP function array_intersect).