php how to implode two dimensional array as arguments of array_intersect - php

I have an array of arrays like below
$array = [
0 => [10, 20, 50],
1 => [20, 30, 50],
2 => [10, 20, 60],
]
and I want to give them as arguments of array_intersect like below
array_intersct($array[0], $array[1], $array[2])
but this is not dynamic do you have better suggestions?

You can use call_user_func_array like this
$intersect = call_user_func_array('array_intersect',$array);

Another variant would be to use the "splat" operator in PHP.
array_intersct(...$array);
It would be more cleared than using call_user_func_array() soltion provided above.

Related

PHP pack with multiple values with different lengths

I'd like to pack a string consisting of a 64 bits, 32 bits and 32 bits ints.
I don't have much experience with the pack function (or bits altogether) so I'm trying to do something like this:
pack('JNN', 1, 2, 3);
// and
unpack('JNN');
But that does not yield the result I'm after.
The problem is that when I run that code I receive the following:
array [
"NN" => 1
]
But I expected this:
array [
1,
2,
3
]
Any idea how to approach this?
Thanks in advance,
pack creates a 16-character string.
$str = pack('JNN', 1, 2, 3);
//string(16) "\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03"
unpack requires keys for the format elements. Example:
$arr = unpack('Jint64/N2int32_',$str);
/*
array (
'int64' => 1,
'int32_1' => 2,
'int32_2' => 3,
)
*/
For more examples, see Unpack in the PHP manual.
If purely numeric keys are required, the array_values function can be used.

Sort mutidimensional array by values total

I am trying to sort multidimensional array by values total in php.
[
c => [1=>22, 2=> 14, 3=> 55],
a => [7=> 33, 2=> 19, 51=> 43, 14=> 27],
...
]
since values total of a subarray are higher than in c, it should be first in this example.
I would appreciate very much help regarding this problem.
You can use array_multisort with array_map
array_multisort(array_map(function($v){return array_sum($v);},$a), SORT_DESC, $a);
First argument with array_map is the sum of the all sub-array. You can change the order of the result array by modifying the 2nd argument SORT_ASC, SORT_DESC
Working example : https://3v4l.org/4fpXh
As #splash58 comment (just to make formal answer):
Use uasort() and array_sum as:
uasort($arr, function($a, $b) { return array_sum($b) - array_sum($a);});
Live example: 3v4l

PHP using array_multisort to sort multiple arrays by date

I have around five arrays, where one of them is containing dates. I would like to sort these after the array containing dates. I know how to sort the date array using "usort". I also know how to sort multiple arrays using "array_multisort", but do not know how to combine these. Is there a way doing this? I hope you can help, and tell me if you need more information to solve my problem :)
Edit:
Here are my arrays:
$fid=array(1, 2, 3, 4, 5, 6, 7, 8);
$ftitle=array("Title1", "Title2", "Title3", "Title4", "Title1", "Title2", "Title3", "Title4");
$fbeskrivelse=array("Beskrivelse1", "Beskrivelse2", "Beskrivelse3", "Beskrivelse4", "Beskrivelse1", "Beskrivelse2", "Beskrivelse3", "Beskrivelse4");
$fstøtter=array(2, 15, 7, 10, 3, 4, 5, 6);
$fstartdato=array('11-01-2012', '01-01-2014', '01-01-2015', '09-02-2013', '01-01-2013', '11-01-2017', '01-01-2018', '01-01-2019');
So this is the kind of result I want (after the sorting):
$fid=array(1, 3, 4, 2, 5, 6, 7, 8);
$ftitle=array("Title1", "Title3", "Title4", "Title2", "Title1", "Title2", "Title3", "Title4");
$fbeskrivelse=array("Beskrivelse1", "Beskrivelse3", "Beskrivelse4", "Beskrivelse2", "Beskrivelse1", "Beskrivelse2", "Beskrivelse3", "Beskrivelse4");
$fstøtter=array(2, 7, 10, 15, 3, 4, 5, 6);
$fstartdato=array('11-01-2012', '01-01-2013', '09-02-2013', '01-01-2014', '01-01-2015', '11-01-2017', '01-01-2018', '01-01-2019');
If I'm understanding correctly, you have (lets say 3) arrays that you want to sort, one of which contains dates, and you want to sort by the dates, and since multisort does not support a callback or sort on dates, you aren't sure what to do?
e.g.
$arr1 = array('2019-05-15', '2019-05-17', '2019-05-13')
$arr2 = array('Wed','Fri','Mon');
$arr3 = array('Pork','Steak','Chicken');
If it were me, I'd probably just parse the dates into unix_time in a new array and use that new array as my sorting "key" to sort the rest, since multisort can do numbers.
(Not tested, just my theory)
$key = array_map('strtotime', $arr1);
array_multisort($key, SORT_ASC, SORT_NUMERIC, $arr1, $arr2, $arr3);

Using Laravel's pluck method without an associative array

Let's say I've got an Articles collection, and I want to fetch the popular articles.
So first, I make a scopePopular() method in the Article method. Very simple.
Now I want to fetch their ids, so I'll do this:
Article::popular->pluck('id');
The result will be an associative array:
[
0 => 1,
1 => 34,
2 => 17
...
];
I want to get a normal array, without the keys, Like:
[1, 34, 17]
I know I can just do something like this:
array_values(Article::popular->pluck('id'));
But I believe Laravel has much more cleaner way to do this. Any ideas?
All arrays have indexes.
[
0 => 1,
1 => 34,
2 => 17
];
equals to
[1, 34, 17]
In other words:
$a1 = [0 => 1, 1 => 34, 2 => 17];
$a2 = [1, 34, 17];
$a1 === $a2;
// returns True
You can use values() method which is wrapper for array_values():
Article::popular->pluck('id')->values();
Its exactly what you need and what you get, By default php has the incremental key from 0.
You want to see something like a JSON array I assume.
Just do a return the array and you will see the JSOn array in browser, but internally its like this only.
Please confirm and let me know.Thanks
$store_type = \App\Websites::find($request->store_id)->first()->store_type;
// Outputs - just a string like "live" // there was option like a demo, live, staging ...
It might be useful for someone else.

PHP - merge only keys of an array?

Let's say I have a PHP array:
$array1 = array(
'protein' => array('PROT', 100, 150),
'fat' => array('FAT', 100, 250),
'carbs' => arary('CARBS', 10, 20)
);
$array2 = array(
'vitaminA' => array('vitA', 1, 2),
'vitaminB' => array('vitB', 1, 2),
'vitaminC' => arary('vitC', 1, 2)
);
Now I want a combined array of those nutrients (something like array_merge()), but I only need the keys, not the values themselves.
So either this:
$combined = array(
'protein' => NULL,
'fat' => NULL,
'carbs' => NULL,
'vitaminA'=> NULL,
'vitaminB'=> NULL,
'vitaminC'=> NULL
);
OR
$combined = array('protein', 'fat', 'carbs', 'vitaminA', 'vitaminB', 'vitaminC');
I can do this manually with a foreach loop, but I was hoping there's a function which would make this process fast and optimized.
Wouldn't this do the trick?
$combined = array_merge(array_keys($array1), array_keys($array2));
This would result in your option #2.
I haven't done any benchmarks but I know that isset() is faster than in_array() in many cases; something tells me that it will be the same for isset() versus array_key_exists(). If it matters that much, you could try to use this:
$combined = array_flip(array_merge(array_keys($array1), array_keys($array2)));
Which would result in something like this:
$combined = array(
'protein' => 1,
'fat' => 2,
'carbs' => 3,
'vitaminA'=> 4,
'vitaminB'=> 5,
'vitaminC'=> 6
);
That would allow you to use isset(), e.g. option #1.
#edit I did some research on the performance of all three mentioned functions and most, if not all, case studies show that isset() is the fastest of all (1, 2); mainly because it is not actually a function but a language construct.
However do note that we now use array_flip() to be able to use isset(), so we lose quite a few microseconds to flip the array; therefore the total execution time is only decreasing if (and only if) you use isset() quite often.
This:
function array_merge_keys($arr1, $arr2) {
return array_merge(array_keys($arr1), array_keys($arr2));
}
should do the trick.

Categories