how to merge array? array_merge not working - php

hello everybody im trying to merge a a few array in an array.
currently with my code:
$matchs = DiraChatLog::where('status','=','Match')->whereBetween('date_access', [$request->from, $request->to])->get();
foreach ($matchs as $key => $match) {
$array[] = [
$match->date_access => $match->status,
];
}
dd($array);
so with this i get the ouput when i dd(); like:
so what i want to do now is to merge all that array to become one in array:16>
itself.
how can i do that? i have tried array merge and it isnt working either

For your case you should have a unique $match->date_access so you can use it as a key of your array, like this :
$matchs = DiraChatLog::where('status','=','Match')
->whereBetween('date_access', [$request->from, $request->to])
->get();
foreach ($matchs as $key => $match) {
$array[$match->date_access] = $match->status;
}
if you have a more complex data you can use array_collapse helper to collapses an array of arrays into a single array,
here is an example :
$array = array_collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]

Related

How to remove unique elements within multidimensional array?

How to remove dulicate keys from multidimensional array?
My array is as follows:
$array = [[0, 1, 2, 3, 4], [1, 2, 3, 4, 6]];
My desired array should be:
$array = [[0, 1, 2, 3, 4], [6]];
Here's a quick and dirty solution for you:
Walk through every element of the array recursively and if you've not seen an element set it to null (unsetting it doesn't work for some reason). Then filter the resulting sub-arrays.
$array = [[0, 1, 2, 3, 4], [1, 2, 3, 4, 6]];
$seen = [];
array_walk_recursive($array, function (&$v) use (&$seen) {
if (!array_key_exists($v, $seen) {
$seen[$v] = true;
} else {
$v = null;
}
});
$final = array_map('array_filter', $array);
If the functions array_diff() and array_values​​() are used, the solution can be delivered in one line of code:
$array = [[0, 1, 2, 3, 4], [1, 2, 3, 4, 6]];
$array[1] = array_values(array_diff($array[1],$array[0]));
var_export($array);
Output:
array (
0 =>
array (
0 => 0,
1 => 1,
2 => 2,
3 => 3,
4 => 4,
),
1 =>
array (
0 => 6,
),
)
$serialize=array_map('serialize',$array);
$unique=array_unique($serialize);
$result=array_map('unserialize',$uniue);
print_r($result);

How to get max element at each index while comparing two arrays?

I have two indexed arrays of identical length:
$first_array = [1,3,4,5,6];
$second_array = [5,2,1,7,9];
I need to generate a new array that consists of the higher value between the two elements at each index.
The output should be:
$output_array[5, 3, 4, 7, 9];
Super easy one-liner:
Pass both arrays to array_map(), as it synchronously loops through both sets of data, call max() on the two elements.
Code: (Demo)
$first_array = [1, 3, 4, 5, 6];
$second_array = [5, 2, 1, 7, 9];
var_export(array_map('max', $first_array, $second_array));
Output:
array (
0 => 5,
1 => 3,
2 => 4,
3 => 7,
4 => 9,
)
Try this way. demo
<?php
$first_array = array(1,3,4,5,6);
$second_array = array(5,2,1,7,9);
$return = array();
foreach($first_array as $key => $value){
if($first_array[$key] > $second_array[$key]){
$return[] = $first_array[$key];
}else{
$return[] = $second_array[$key];
}
}
print_r($return);

Add value from one element to other in php

This is probably a duplicate, but I couldn't find the answer I needed, maybe my wording is wrong.
Anyway, I have a two-dimensional array with hundreds of values, what I need is to insert a value from second element to the first element.
Example;
I have four elements in an array:
[0] = 1, [1] = 9, [2] = 9, [3] =5
I need to put these into a single element, so that it would turn
into this: [0] = 1995.
I have a feeling there might be something I could do with foreach, if so, maybe someone can explain to me, in detail, how that would exactly work?
OR
Maybe there's a function I'm not aware of.
You can use the following, using implode:
$arr = [1, 9, 9, 5];
$val = implode($arr);
unset($arr);
$arr[0] = $val;
demo: https://ideone.com/VTb7vO
To use the solution using implode on the whole multidimensional array, you can use the following:
$arr = [[1, 9, 9, 5], [1, 9, 9, 6], [1, 9, 9, 7], [1, 9, 9, 8]];
foreach ($arr as $key => $value) {
$val = implode($value);
$arr[$key] = $val;
}
demo: https://ideone.com/X6vueO
another, much shorter solution could be the following:
$arr = [[1, 9, 9, 5], [1, 9, 9, 6], [1, 9, 9, 7], [1, 9, 9, 8]];
$arr = array_map('implode', $arr);
demo: https://ideone.com/0ju8he
To concatenate each inner array of numbers you can use implode on each of it.
$newArray = array_map('implode', $array);
If it is executed on the array [[1, 2, 3], [1, 3], [1, 1, 1]] it will create the array ['123', '13', '111']. demo
$arr = [2,3,4];
$var = implode($arr, '');
var_dump( $var );
Two dimensional:
$cars = array
(
array(22,18),
array(15,13),
array(5,2),
array(17,15)
);
foreach ($cars as $val) {
$var1 = implode($val, '');
var_dump( $var1 );
}
$var= "";
foreach($arr as $element) {
$var .= $element;
}

Check if array contains values of another array in the same order

I have two arrays, the first being:
[4, 6, 2, 7, 5, 1]
and the second being:
[2, 7, 5]
How can I determine if the first array contains the SAME values (and in the SAME order) of the second array. In this case the function would return TRUE because the sequence 2, 7, 5 in fact is present in the first array. It would return FALSE for 2, 5, 7. The values exist in the first array, but not in that exact order.
Is there an existing PHP function for this? If no, how should I achieve this?
For common case of array contents:
$ar1 = [2, 4, 6, 2, 4, 7, 5, 1,];
$ar2 = [2, 7, 5];
// Find point where sub-array can start
$keys = array_keys($ar1, $ar2[0]);
foreach($keys as $k)
// Check that sub-array is the same as the second array
if(array_slice($ar1, $k, count($ar2)) == $ar2)
{ echo "Wow!"; break; }
If you array is just numbers, i would suggest a trick:
$a1 = [4, 6, 2, 7, 5, 1];
$a2 = [2, 7, 5];
// convert both arrays to strings,
// add `,` in the beginning and end, see #splash58 comment
$a1_str = ',' . implode(',', $a1) . ',';
$a2_str = ',' . implode(',', $a2) . ',';
// check with strpos:
echo strpos($a1_str, $a2_str) !== false? 'Eq' : 'Not eq';

PHP compare arrays

I have two arrays. $arrayOld and $arrayNew and I want to compare these arrays and only select the values that are not in $arrayNew.
I don't want the values that are in $arrayNew only. So I don't think array_diff() is gonna help me.
$arrayOld = [1, 2, 3, 4, 5]
$arrayNew = [1, 4, 5, 6, 7]
So it only needs to return 2 and 3 and not 6 or 7.
use array_diff, to accomplish this. As you need to difference between the array and need data from Old array so you need to use the old array as the first parameter of the array_diff.
Note: Array diff only returns from the first array which is not in second array.
$arrayOld = [1, 2, 3, 4, 5];
$arrayNew = [1, 4, 5, 6, 7];
$n = array_diff($arrayOld, $arrayNew);
print_r($n);
Result: Online Check
Array
(
[1] => 2
[2] => 3
)
If you need a new keys for the output array just use array_values. The new key start from 0.
$arr = array_values($n);
Use below code
$arrayOld = [1, 2, 3, 4, 5];
$arrayNew = [1, 4, 5, 6, 7];
print "<pre>";
print_r(array_diff($arrayOld, $arrayNew));
OUTPUT:
Array
(
[1] => 2
[2] => 3
)
use this code.
$arrayOld = array(1, 2, 3, 4, 5);
$arrayNew = array(1, 4, 5, 6, 7);
print_r(array_diff($arrayOld, $arrayNew));
$compare = array();
$i=1;
foreach($arrayOld as $k=>$v){
if(!in_array($v, $arrayNew)){
$compare[$i] = $v;
$i++;
}
}
$i=$i;
foreach($arrayNew as $k=>$v){
if(!in_array($v, $arrayOld)){
$compare[$i] = $v;
$i++;
}
}
use array_diff function
$arrayOld = array(1, 2, 3, 4, 5);
$arrayNew = array(1, 4, 5, 6, 7);
print_r(array_diff($arrayOld, $arrayNew));

Categories