Loop a array and create new array by combine two array - php

$array1=Array("2016-11-02","2016-11-03","2016-11-04","2016-11-05","2016-11-06","2016-11-07","2016-11-08");
$array2 = Array([0] => Array("count" => 2 ,"created_at"=> 2016-11-04));
//desired result will
$array3 = Array(
[0] => Array("count" => 0 ,"created_at" => 2016-11-02),
[1] => Array("count" =>0 "created_at" => 2016-11-03),
[2] => Array("count" => 2 ,"created_at" => 2016-11-04),
[3] => Array("count" => 0 ,"created_at" => 2016-11-05),
[4] => Array("count" =>0 ,"created_at" => 2016-11-06),
[5] => Array("count" => 0 ,"created_at" => 2016-11-07),
[6] => Array("count" => 0 ,"created_at" => 2016-11-08)
);

Try this -
foreach ($array1 as $date) {
//if value resides in $array2 - use that count
if (in_array($date, $array2[0])) {
$array3[]['count'] = $array2[0]['count'];
}
else {
$array3[]['count'] = 0;
}
$array3[]['created_at'] = $date;
}
print_r($array3); //should be your required array.

$array1=array("2016-11-02","2016-11-03","2016-11-04","2016-11-05","2016-11-06","2016-11-07","2016-11-08");
$array4 = array(0 => array("count" =>'2' ,"created_at"=>'2016-11-04'),1 => array("count" =>'2' ,"created_at"=>'2016-11-05'));
$array3 = array();
for($i =0;$i<count($array1); $i++) {
$array3[$i]['count'] = 0;
$array3[$i]['created_at'] = $array1[$i];
}
$arr = array_merge($array3,$array4);
foreach($arr as $k => $v) {
foreach($arr as $key => $value) {
if($k != $key && $v['created_at'] == $value['created_at']) {
unset($arr[$k]);
}
}
}
$arr = array_values($arr);
function date_compare($a, $b) {
$t1 = strtotime($a['created_at']);
$t2 = strtotime($b['created_at']);
return $t1 - $t2;
}
usort($arr, 'date_compare');
print_r($arr);

Related

array_merge & in_array not what I need

I have an array like this;
$array = array(
array('id' => 1, 'amount' => 104,
array('id' => 46, 'amount' => 200,
array('id' => 1004, 'amount' => 200
);
and another array like this;
$array2 = array(
array('id' => 206, 'amount' => 50,
array('id' => 1, 'amount' => 96,
array('id' => 88, 'amount' => 2
);
How to I add check if the 'id' in array2 exists in $array. If it does at the 'amount' of $array2 to $array, if it doesn't exist append the id and amount onto the end of $array. I want the output result to look like this:
array {
[0] id = 1 amount = 200
[1] id = 46 amount = 200
[2] id = 1004 amount = 200
[3] id = 206 amount = 50
[4] id = 88 amount = 2
}
I have tried using array_merge and in_array but can't seem to get my head around it working.
You could try somthing like this :
foreach ($array as $key => $value) {
$found = false ;
foreach ($array2 as $key2 => $value2) {
if ($value2['id'] == $value['id']) {
$array2[$key2]['amount'] += $value['amount'];
$found = true;
break;
}
}
if (!$found) {
$array2[] = $value ;
}
}
print_r($array2);
outputs :
Array
(
[0] => Array
(
[id] => 206
[amount] => 50
)
[1] => Array
(
[id] => 1
[amount] => 200
)
[2] => Array
(
[id] => 88
[amount] => 2
)
[3] => Array
(
[id] => 46
[amount] => 200
)
[4] => Array
(
[id] => 1004
[amount] => 200
)
)
EDIT After comments, updated code with a final check to remove duplicated rows :
foreach ($array as $key => $value) {
$found = false ;
foreach ($array2 as $key2 => $value2) {
if ($value2['id'] == $value['id']) {
$array2[$key2]['amount'] += $value['amount'];
$found = true;
break;
}
}
if (!$found) {
$array2[] = $value ;
}
}
$final = [];
foreach ($array2 as $key1 => $value) {
$found = false ;
foreach ($final as $key2 => $value2) {
if ($value2['id'] == $value['id']) {
$final[$key2]['amount'] += $value['amount'];
$found = true;
break;
}
}
if (!$found) {
$final[] = $value ;
}
}
var_dump($final) ;

PHP - Create multiple arrays based on value

I have an array like this:
$r = array();
$r[] = ['name' => 'Test', 'supplierId' => 34];
$r[] = ['name' => 'Test2', 'supplierId' => 31];
$r[] = ['name' => 'Test3', 'supplierId' => 32];
$r[] = ['name' => 'Test4', 'supplierId' => 34];
$r[] = ['name' => 'Test5', 'supplierId' => 30];
$r[] = ['name' => 'Test6', 'supplierId' => 32];
Now I want to take $r and get multiple arrays back, differenced by supplierId. So I am looking for this result:
$r30 = ['name' => 'Test5', 'supplierId' => 30];
$r32 = [
['name' => 'Test3', 'supplierId' => 32],
['name' => 'Test6', 'supplierId' => 32]
];
I tried it with, but here I do not have access to $sup in array_filter.
$supplier = array(30, 31, 32, 34);
$finalArray = [];
foreach ($supplier as $sup) {
$finalArray[] = array_filter($r, function($value, $sup) {
echo $sup;
if ($value['supplierId'] == $sup) {
return true;
}
});
}//foreach
Any idea how I can solve it? Is there no native function that accomplishes this - something like create_array_based_on('supplierId');?
Thanks
You can pass $sup to your anonymous function:
foreach ($supplier as $sup) {
$finalArray[] = array_filter($r, function($value) use ($sup) {
^^^^^^^^^^ you need to pass
it like this
echo $sup;
if ($value['supplierId'] == $sup) {
return true;
}
});
}//foreach
But personally I would probably just loop over the original array and use the supplier ID as a key.
Something like:
$results = [];
foreach ($r as $value) {
$results[$value['supplierId']][] = $value;
}
Try this out
$r = array();
$r[] = ['name' => 'Test', 'supplierId' => 34];
$r[] = ['name' => 'Test2', 'supplierId' => 31];
$r[] = ['name' => 'Test3', 'supplierId' => 32];
$r[] = ['name' => 'Test4', 'supplierId' => 34];
$r[] = ['name' => 'Test5', 'supplierId' => 30];
$r[] = ['name' => 'Test6', 'supplierId' => 32];
$supplier = array(30, 31, 32, 34);
$finalArray = [];
$i=0;
foreach ($supplier as $sup) {
$value =$r[$i]['supplierId'];
if($value==$sup)
{
$finalArray[] = $value;
}
$i++;
}//foreach
$finalArray is a new array with all values you need
You can combine array_filter and in_array as following:
$finalArray[] = array_filter($r, function($value) use ($supplier) {
return in_array($value['supplierId'], $supplier);
});
Without foreach loop.
It would be much easier, instead of wanting new variables for each of them, like $r32, $r35, etc... if you just used the array keys to store the supplierid, e.g.
$r = array();
$r[] = ['name' => 'Test', 'supplierId' => 34];
$r[] = ['name' => 'Test2', 'supplierId' => 31];
$r[] = ['name' => 'Test3', 'supplierId' => 32];
$r[] = ['name' => 'Test4', 'supplierId' => 34];
$r[] = ['name' => 'Test5', 'supplierId' => 30];
$r[] = ['name' => 'Test6', 'supplierId' => 32];
$newArray = array();
foreach($r as $key => $element)
{
if (!isset($newArray[$element['supplierId']])){
$newArray[$element['supplierId']] = array();
}
$newArray[$element['supplierId']][] = $element;
}
That would give you an output of:
<pre>Array
(
[34] => Array
(
[0] => Array
(
[name] => Test
[supplierId] => 34
)
[1] => Array
(
[name] => Test4
[supplierId] => 34
)
)
[31] => Array
(
[0] => Array
(
[name] => Test2
[supplierId] => 31
)
)
[32] => Array
(
[0] => Array
(
[name] => Test3
[supplierId] => 32
)
[1] => Array
(
[name] => Test6
[supplierId] => 32
)
)
[30] => Array
(
[0] => Array
(
[name] => Test5
[supplierId] => 30
)
)
)
</pre>
It's about simple grouping nested arrays by some key value:
$finalArr = [];
foreach ($r as $arr) {
$finalArr[$arr['supplierId']][] = $arr;
}
print_r($finalArr);
DEMO link

how to filter array using php?

print_r($menus); gives the following output:
Array
(
[0] => Array
(
[name] => Politics
[action] => politics
)
[sub_menu2] => Array
(
[0] => Array
(
[name] => submenu2
[action] => politics
)
[1] => Array
(
[name] => submenu3
[action] => sport
)
)
)
My expected filtered array is:
Array
(
[0] => Array
(
[name] => Politics
[action] => politics
[sub_menu2] => Array
(
[0] => Array
(
[name] => submenu2
[action] => politics
)
[1] => Array
(
[name] => submenu3
[action] => sport
)
)
)
)
My code is:
$filteredMenu = array();
$unique = array();
$index = 0;
$index2 = 0;
foreach ($menus as $key => $menu) {
$pm = $menu['Menu']['name'];
if (isset($unique[$pm])) {
if (!empty($menu['sub_menus']['name'])) {
$temp = array('name' => $menu['sub_menus']['name'], 'action' => $menu['sub_menus']['action']);
$filteredMenu[$index]['sub_menu'][] = $temp;
}
if(!empty($menu['sub_sub_menus']['name'])){
$temp = array('name' => $menu['sub_sub_menus']['name'], 'action' => $menu['sub_menus']['action']);
$filteredMenu[$index]['sub_menu']['sub_menu2'][] = $temp;
}
} else {
if ($key != 0)
$index++;
$unique[$pm] = 'set';
$temp = array('name' => $pm, 'action' => $menu['Menu']['action']);
$filteredMenu[$index]['menu'] = $temp;
if (!empty($menu['sub_menus']['name'])) {
$temp = array('name' => $menu['sub_menus']['name'], 'action' => $menu['sub_menus']['action']);
$filteredMenu[$index]['sub_menu'][] = $temp;
}
if(!empty($menu['sub_sub_menus']['name'])){
$temp = array('name' => $menu['sub_sub_menus']['name'], 'action' => $menu['sub_menus']['action']);
$filteredMenu[$index]['sub_menu']['sub_menu2'][] = $temp;
}
}
}
I can filter to wrap all sum menus under menu. But I can't wrap all second level sub menu under sub menu. I spent lot of time to solve but failed.
Your php code is different from your array. From where you find
$pm = $menu['Menu']['name'];
this will display error Undefined index: Menu
I think you have to apply more condition.
I don't sure apply below code. I think this will solve your problem.
$filteredMenu = array();
$unique = array();
$index = 0;
$index2 = 0;
foreach ($menus as $key => $menu) {
$pm = $menu['Menu']['name'];
$pm1 = $menu['sub_menus']['name'];
if (isset($unique[$pm])) {
if (!empty($menu['sub_menus']['name'])) {
$temp = array('name' => $menu['sub_menus']['name'], 'action' => $menu['sub_menus']['action']);
$filteredMenu[$index]['menu']['sub_menu'][] = $temp;
}
if(!empty($menu['sub_sub_menus']['name'])){
$temp = array('name' => $menu['sub_sub_menus']['name'], 'action' => $menu['sub_menus']['action']);
$filteredMenu[$index]['menu']['sub_menu']['sub_menu2'][] = $temp;
}
} else {
if ($key != 0)
$index++;
$unique[$pm] = 'set';
$temp = array('name' => $pm, 'action' => $menu['Menu']['action']);
$filteredMenu[$index]['menu'] = $temp;
if (!empty($menu['sub_menus']['name'])) {
$temp = array('name' => $menu['sub_menus']['name'], 'action' => $menu['sub_menus']['action']);
$filteredMenu[$index]['menu']['sub_menu'][] = $temp;
}
if(!empty($menu['sub_sub_menus']['name'])){
$temp = array('name' => $menu['sub_sub_menus']['name'], 'action' => $menu['sub_menus']['action']);
$filteredMenu[$index]['menu']['sub_menu']['sub_menu2'][] = $temp;
}
}
}

How to concatenate two or more arrays in PHP without loosing values if it is same key and different values

I have two multi-dimensional arrays. I need to concatenate the two without loosing any values which have the same key and different values. Here is the scenario:
Array1
(
[0] => 11
[2] => 12
[3] => 13
[4] => (
[0] => 100
[1] => 200
)
[5] => 2
[6] => 3
)
Array2
(
[0] => 11
[2] => 12
[3] => 13
[4] => (
[0] => 400
[1] => 500
)
[5] => 2
[6] => 3
)
The result should be
Result
(
[0] => 11
[2] => 12
[3] => 13
[4] => (
[0] => (
[0] => 100
[1] => 400
)
[1] => (
[0] => 200
[1] => 500
)
)
[5] => 2
[6] => 3
)
Here is one solution:
<?php
$arrayA = array(0 => 11, 2 => 12, 3 => 13, 4 => array(0 => 100, 1 => array(0 => 222),), 5 => 2, 6 => 3);
$arrayB = array(
0 => 11,
2 => 12,
3 => 13,
4 => array(
0 => 100,
1 => array(0 => array(0 => 'test1', 1 => 'test2'), 1 => array(0 => 'test1', 1 => 'test2'),),
),
5 => 2,
6 => 3
);
/**
* #param $a
* #param $b
* #return array
*/
function array_merge_graceful($a, $b)
{
$c = [];
if (is_array($a) && is_array($b)) {
foreach (array_merge(array_keys($a),array_keys($b)) as $i) {
if (!array_key_exists($i, $a)) {
$c[$i] = $b[$i];
} elseif (!array_key_exists($i, $b)) {
$c[$i] = $a[$i];
} else {
$c[$i] = array_merge_graceful($a[$i], $b[$i]);
}
}
} else {
if ($a <> $b) {
$c = [$a, $b];
} else {
$c = $a;
}
}
return $c;
}
var_dump(array_merge_graceful($arrayA, $arrayB));
?>
Try something like this
<?php
$array1 = array(11, 12, 13, array(100, 200), 2, 3);
$array2 = array(11, 12, 13, array(400, 500), 2, 3);
echo "<pre>";
print_r($array1);
print_r($array2);
$combine = array();
foreach ($array1 as $key => $value) {
if (array_key_exists($key, $combine)) {
//if is array
if (is_array($combine[$key])) {
$combine[$key] = array($combine[$key], $value);
}
} else {
$combine[$key] = $value;
}
}
foreach ($array2 as $key => $value) {
if (array_key_exists($key, $combine)) {
//if is array
if (is_array($combine[$key])) {
$combine[$key] = array($combine[$key], $value);
}
} else {
$combine[$key] = $value;
}
}
echo "<hr>";
print_r($combine);
use array_merge_recursive() function for merging arrays.For more details please refer http://php.net/manual/en/function.array-merge-recursive.php

Merging and combining arrays in PHP

I have multiple associative arrays, similar to the following:
$arr1 = array(0 => 12, 5 => 10, 19 => 48);
$arr2 = array(0 => 14, 7 => 9, 12 => 11, 19 => 30);
I would like to merge these arrays so that I have a single set of keys (0, 5, 7, 12, 19) and each points to an array with the values from the original arrays, and null if the value doesn't exist in the original array:
$merge = array(
0 => array(12, 14),
5 => array(10, null),
7 => array(null, 9),
12 => array(null, 11),
19 => array(48, 30)
);
I need to be able to do this for an arbitrary number of arrays. I'm not sure where to start with this.
I could, I suppose, iterate through each array, append it's value to the result - but I'd have to check to see if I have the requisite number of elements in each resulting array before appending the current value to that index - which isn't terribly efficient.
Ideas or pointers?
$arr1 = array(0 => 12, 5 => 10, 19 => 48);
$arr2 = array(0 => 14, 7 => 9, 12 => 11, 19 => 30);
$keys = array_merge(array_keys($arr1), array_keys($arr2));
$merged = array();
foreach ($keys as $key) {
$merged[$key] = array();
$merged[$key][] = isset($arr1[$key]) ? $arr1[$key] : null;
$merged[$key][] = isset($arr2[$key]) ? $arr2[$key] : null;
}
ksort($merged);
echo '<pre>', var_dump($merged), '</pre>';
modified for an arbitrary number of arrays
$arrays = array(
array(0 => 12, 5 => 10, 19 => 48),
array(0 => 14, 7 => 9, 12 => 11, 19 => 30),
// ... more arrays
);
$keys = array();
foreach ($arrays as $arr) {
$keys = array_merge($keys, array_keys($arr));
}
$merged = array();
foreach ($keys as $key) {
$merged[$key] = array();
foreach ($arrays as $arr) {
$merged[$key][] = isset($arr[$key]) ? $arr[$key] : null;
}
}
ksort($merged);
echo '<pre>', var_dump($merged), '</pre>';
EDIT
<?php
$arr1 = array(0 => 12, 5 => 10, 19 => 48);
$arr2 = array(0 => 14, 7 => 9, 12 => 11, 19 => 30);
foreach($arr1 as $k => $v){
if(array_key_exists($k, $arr2)){
$newarr[$k][] = $v;
$newarr[$k][] = $arr2[$k];
}else{
$newarr[$k][] = $v;
$newarr[$k][] = 'NULL';
}
}
foreach($arr2 as $k => $v){
if(!array_key_exists($k, $arr1)){
$newarr[$k][] = 'NULL';
$newarr[$k][] = $v;
}
}
ksort($newarr);
echo '<pre>';
print_r($newarr);
?>
Output:
Array
(
[0] => Array
(
[0] => 12
[1] => 14
)
[5] => Array
(
[0] => 10
[1] => NULL
)
[7] => Array
(
[0] => NULL
[1] => 9
)
[12] => Array
(
[0] => NULL
[1] => 11
)
[19] => Array
(
[0] => 48
[1] => 30
)
)
I think this is what you're looking for.
/*Merge function*/
function merge($a1, $a2)
{
foreach($a1 as $key => $val)
$a1[$key] = Array($a1[$key], null);
foreach($a2 as $key => $val)
$a1[$key] = Array((isset($a1[$key]))? $a1[$key][0]: null, $val);
return ksort($a1);
}
/*Test*/
$arr1 = array(0 => 12, 5 => 10, 19 => 48);
$arr2 = array(0 => 14, 7 => 9, 12 => 11, 19 => 30);
foreach(merge($arr1, $arr2) as $key => $val){
echo "<br />$key --";
print_r($val);
}
/*output*/
0 --Array ( [0] => 12 [1] => 14 )
5 --Array ( [0] => 10 [1] => )
7 --Array ( [0] => [1] => 9 )
12 --Array ( [0] => [1] => 11 )
19 --Array ( [0] => 48 [1] => 30 )
Try this function:
function multimerge ($array1, $array2) {
if (is_array($array2) && count($array2)) {
foreach ($array2 as $k => $v) {
if (is_array($v) && count($v)) {
$array1[$k] = multimerge($array1[$k], $v);
} else {
$array1[$k] = $v;
}
}
} else {
$array1 = $array2;
}
return $array1;
}
http://php.net/manual/en/function.array-merge.php

Categories