I have two array Array1 and Array2 and now i want to combine or you can say merge these array into one according to product_option_id in each array means if product_option_id is same then push the second array data into one.
my First array is :
Array
(
[0] => Array
(
[DispensaryInventory] => Array
(
[id] => 21
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 1
)
)
[1] => Array
(
[DispensaryInventory] => Array
(
[id] => 22
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 2
)
)
[2] => Array
(
[DispensaryInventory] => Array
(
[id] => 23
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 3
)
)
[3] => Array
(
[DispensaryInventory] => Array
(
[id] => 24
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 4
)
)
[4] => Array
(
[DispensaryInventory] => Array
(
[id] => 25
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 5
)
)
)
and second array is
Array
(
[0] => Array
(
[DriverInventory] => Array
(
[id] => 21
[driver_id] => 10
[dispensary_inventory_id] => 12
[product_id] => 7
[product_option_id] => 2
)
[Mydata] => Array
(
[price]= 2545
)
)
[1] => Array
(
[DriverInventory] => Array
(
[id] => 22
[driver_id] => 10
[dispensary_inventory_id] => 13
[product_id] => 7
[product_option_id] => 3
[quantity] => 16
)
[Mydata] => Array
(
[price]= 15987
)
)
[2] => Array
(
[DriverInventory] => Array
(
[id] => 23
[driver_id] => 10
[dispensary_inventory_id] => 14
[product_id] => 7
[product_option_id] => 4
[quantity] => 20
)
[Mydata] => Array
(
[price]= 96744
)
)
[3] => Array
(
[DriverInventory] => Array
(
[id] => 24
[driver_id] => 10
[dispensary_inventory_id] => 15
[product_id] => 7
[product_option_id] => 5
[quantity] => 45
)
[Mydata] => Array
(
[price]= 97455
)
)
)
finally i want to be a result as
Array
(
[0] => Array
(
[DispensaryInventory] => Array
(
[id] => 21
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 1
)
)
[1] => Array
(
[DispensaryInventory] => Array
(
[id] => 22
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 2
)
[Mydata] => Array
(
[price]= 2545
)
)
[2] => Array
(
[DispensaryInventory] => Array
(
[id] => 23
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 3
)
[Mydata] => Array
(
[price]= 15987
)
)
[3] => Array
(
[DispensaryInventory] => Array
(
[id] => 24
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 4
)
[Mydata] => Array
(
[price]= 96744
)
)
[4] => Array
(
[DispensaryInventory] => Array
(
[id] => 25
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 5
)
[Mydata] => Array
(
[price]= 97455
)
)
)
Try this.
foreach ($array1 as $key => $value)
{
foreach ($array2 as $key1 => $value1)
{
if($value['DispensaryInventory']['product_option_id']==$value1['DriverInventory']['product_option_id'])
{
$price_data[$key]['Mydata'] = $value1['Mydata'];
}
}
}
Related
I need to subtract the qt from two arrays based on id and type, keeping the array complete.
How do I do in php to be able to subtract these arrays?
I have 2 arrays:
=> this is the first array with multiple key "down"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => down
[qt] => 12
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => down
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => down
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => down
[qt] => 45
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => down
[qt] => 3
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
=> this is the second array with multiple key "up"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => up
[qt] => 5
)
[1] => Array
(
[id] => 86
[loc] => 3
[type] => up
[qt] => 27
)
[2] => Array
(
[id] => 23
[loc] => 9
[type] => up
[qt] => 3
)
)
=> I need cubtract "qt" (if "id" and "loc" are the same then subtract the "qt")
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => total
[qt] => 7
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => total
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => total
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => total
[qt] => 18
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => total
[qt] => 0
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
Try this out
function findMatch($array, $id, $loc)
{
foreach ($array as $key => $item) {
if ($item["id"] == $id and $item["loc"] == $loc) {
return $key;
}
}
return false;
}
$total = [];
foreach($down as $d) {
$matched = findMatch($up, $d["id"], $d["loc"]);
if($matched !== false){
$total[] = array_replace($d, [
"type" => "total",
"qt" => ($d["qt"] - $up[$matched]["qt"])
]);
} else {
$total[] = array_replace($d, ["type" => "total"]);
}
}
print_r($total);
I have a varying level of deepness for a multidimensional array. I want to find the elements where UserId isset (see example array below), then add a key/value pair ([show] => true) to the matched element and each parent array. There may be multiple matches that will need to modify the parents.
I have this:
Array
(
[0] => Array
(
[id] => 3
[parent_id] => 0
[ownerEntityId] => 2
[children] => Array
(
[0] => Array
(
[id] => 15
[parent_id] => 3
[ownerEntityId] => 14
[children] => Array
(
[0] => Array
(
[id] => 17
[parent_id] => 15
[ownerEntityId] =>
[userId] => 2
)
[1] => Array
(
[id] => 18
[parent_id] => 15
[ownerEntityId] =>
)
[2] => Array
(
[id] => 19
[parent_id] => 15
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 11
[parent_id] => 3
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 26
[parent_id] => 1
[ownerEntityId] =>
[children] => Array
(
[0] => Array
(
[id] => 23
[parent_id] => 26
[ownerEntityId] => 24
[1] => Array
(
[id] => 41
[parent_id] => 26
[ownerEntityId] =>
)
)
)
I want this:
Array
(
[0] => Array
(
[id] => 3
[parent_id] => 0
[ownerEntityId] => 2
[show] => true //***Added
[children] => Array
(
[0] => Array
(
[id] => 15
[parent_id] => 3
[ownerEntityId] => 14
[show] => true //***Added
[children] => Array
(
[0] => Array
(
[id] => 17
[parent_id] => 15
[ownerEntityId] =>
[show] => true //***Added
[userId] => 2
)
[1] => Array
(
[id] => 18
[parent_id] => 15
[ownerEntityId] =>
)
[2] => Array
(
[id] => 19
[parent_id] => 15
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 11
[parent_id] => 3
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 26
[parent_id] => 1
[ownerEntityId] =>
[children] => Array
(
[0] => Array
(
[id] => 23
[parent_id] => 26
[ownerEntityId] => 24
[1] => Array
(
[id] => 41
[parent_id] => 26
[ownerEntityId] =>
)
)
)
I have been messing with multiple recursion functions that have failed me miserably.
https://ideone.com/HoJPry
function hasUserId (&$el) {
if(isset($el['children'])) {
$ret = false;
foreach($el['children'] as &$child) {
if(hasUserId($child)) {
$ret=true;
}
}
if($ret) {
$el['show']=true;
return true;
}
} ;
if (isset($el['user_id'])) {
$el['show']=true;
return true;
}
}
foreach($top as $i => $t)
{
foreach($t['children'] as $n => $mid)
{
foreach($mid['children'] as $x => $bottom)
{
if($bottom['id'] !== $target_id)
continue;
$top[$i]['show'] = true;
$top[$i]['children'][$n]['show'] = true;
$top[$i]['children'][$n]['children'][$x]['show'] = true;
}
}
}
I have two array $array1 and $array2 which I get dynamically and look like
$array1 = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 2
[cnt] => 5
)
[1] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
$array2 = Array
(
[0] => Array
(
[id] => 1
[name] => Phone Calls
[readable] => 1
[status] => active
)
[1] => Array
(
[id] => 2
[name] => Meeting With Customer
[readable] => 1
[status] => active
)
[2] => Array
(
[id] => 3
[name] => Others Works
[readable] => 1
[status] => active
)
);
which i need to compare.
if $array2['id'] is not in $array1["activity"](i.e"activity_id") add array ['activity_id'=>$array2['id'],'cnt'=>0] to $array1['activity'].
My result must be like
$result = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 0
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 5
)
[2] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
What i have tried is
$finalArray = array();
foreach($array1 as $arr1) {
foreach($array2 as $arr2) {
if(!in_array($arr2['id'], $arr1['activity'])) {
$array = ['activity_id'=>$arr2['id'], 'cnt'=>0];
}
array_push($arr1['activity'], $array);
unset($array);
}
array_push($finalArray, $result);
}
print_r($finalArray);
in_array() function is not working as I excepted or I am trying to do it in the wrong way. Can someone helps me with this?
Sorry,finally i get what i did wrong.May be someone get helped.
everything is ok just change the line
if(!in_array($arr2['id'], $arr1['activity'])) {
into
if(!in_array( $readActivity['id'], array_column($result['activity'],'activity_id'))){
I have an array after an SQL query made with cake PHP which returns me a tree. I do not the number of dimension of my array.
I want transform it to use it with jstree. I'm fighting with a recursive function and I didn't success.
Can you help me.
My original array looks like this:
Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 815
[Name] => 1
[parent_id] =>
[lft] => 1
[rght] => 30
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 816
[Name] => 2
[parent_id] => 815
[lft] => 2
[rght] => 15
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 817
[parent_id] => 816
[lft] => 3
[rght] => 8
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 818
[Name] => 4
[parent_id] => 817
[lft] => 4
[rght] => 5
)
[children] => Array
(
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 819
[Name] => 5
[parent_id] => 817
[lft] => 6
[rght] => 7
)
[children] => Array
(
)
)
)
)
)
)
)
)
)
And I want have something like this :
Array
(
[0] => Array
(
[text] => 1
[id] => 815
[children] => Array
(
[0] => Array
(
[text] => 2
[id] => 816
[children] => Array
(
[0] => Array
(
[text] => 3
[id] => 817
[children] => Array
(
[0] => Array
(
[text] => 4
[id] => 818
)
)
)
[1] => Array
(
[text] => 5
[id] => 819
)
)
)
)
)
)
I have try with a recursive function like this but I don't success
private function buildTree(array $elements) {
$branch=array();
foreach ($elements as $element){
$branch[]=$element['Confsave']['Name'];
if(is_array($element['children'])){
$this->buildTree($element);
}
}
return $branch;
}
Edit :
After test and remarks my function is now
private function buildTree(array $elements) {
$branch=array();
foreach ($elements as $element){
$branch[]=$element['Confsave']['Name'];
if(is_array($element['children'])){
$this->buildTree($element['children']);
}
}
return $branch;
}
When I am debuging, I can see that I go to my function for each child (what I want). But I don't know how to make the new array()
this worked for me...
$array = buildTree($array);
print_r($array);
function buildTree(array $parent) {
$branch = array();
foreach ($parent as $index => $element){
$node = array();
$node['id'] = $element['Confsave']['id'];
$node['text'] = isset($element['Confsave']['Name']) ?
$element['Confsave']['Name'] : 'no name';
$node['children'] = buildTree($element['children']);
$branch[] = $node;
}
return $branch;
}
my initial test array....
Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 999
[Name] => 999
[parent_id] =>
[lft] => 1
[rght] => 30
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 777
[Name] => 777
[parent_id] => 999
[lft] => 1
[rght] => 30
)
[children] => Array
(
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 888
[Name] => 888
[parent_id] => 999
[lft] => 1
[rght] => 30
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 815
[Name] => 1
[parent_id] =>
[lft] => 1
[rght] => 30
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 816
[Name] => 2
[parent_id] => 815
[lft] => 2
[rght] => 15
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 817
[parent_id] => 816
[lft] => 3
[rght] => 8
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 818
[Name] => 4
[parent_id] => 817
[lft] => 4
[rght] => 5
)
[children] => Array
(
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 819
[Name] => 5
[parent_id] => 817
[lft] => 6
[rght] => 7
)
[children] => Array
(
)
)
)
)
)
)
)
)
)
my result array....
Array
(
[0] => Array
(
[id] => 999
[text] => 999
[children] => Array
(
[0] => Array
(
[id] => 777
[text] => 777
[children] => Array
(
)
)
[1] => Array
(
[id] => 888
[text] => 888
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 815
[text] => 1
[children] => Array
(
[0] => Array
(
[id] => 816
[text] => 2
[children] => Array
(
[0] => Array
(
[id] => 817
[text] => no name
[children] => Array
(
[0] => Array
(
[id] => 818
[text] => 4
[children] => Array
(
)
)
[1] => Array
(
[id] => 819
[text] => 5
[children] => Array
(
)
)
)
)
)
)
)
)
)
Ok so here is my first array:
(
[1] => Array
(
[27] => Array
(
[product_id] => 27
[type] => hardware
[step_number] => 1
)
[372] => Array
(
[product_id] => 372
[type] => hardware
[step_number] => 1
)
[92] => Array
(
[product_id] => 92
[type] => hardware
[step_number] => 1
)
)
[2] => Array
(
[335] => Array
(
[product_id] => 335
[type] => hardware
[step_number] => 2
)
[62] => Array
(
[product_id] => 62
[type] => hardware
[step_number] => 2
)
[356] => Array
(
[product_id] => 356
[type] => hardware
[step_number] => 2
)
)
and here is my second array
(
[1] => Array
(
[655] => Array
(
[product_id] => 655
[type] => optional
[step_number] => 1
)
[54] => Array
(
[product_id] => 54
[type] => optional
[step_number] => 1
)
[554] => Array
(
[product_id] => 554
[type] => optional
[step_number] => 1
)
)
[2] => Array
(
[33] => Array
(
[product_id] => 33
[type] => optional
[step_number] => 2
)
[612] => Array
(
[product_id] => 612
[type] => optional
[step_number] => 2
)
[5] => Array
(
[product_id] => 5
[type] => optional
[step_number] => 2
)
)
[3] => Array
(
[444] => Array
(
[product_id] => 444
[type] => optional
[step_number] => 3
)
[6] => Array
(
[product_id] => 6
[type] => optional
[step_number] => 3
)
[53] => Array
(
[product_id] => 53
[type] => optional
[step_number] => 3
)
)
Basically what i need is the second array appended to the end of the first array with the keys of the first array preserved the keys and changing the step_number to the new key so the final keys looks like
(
[1] => Array
(
[27] => Array
(
[step_number] => 1)
....
[2] => Array
(
[335] => Array
(
[step_number] => 2)
....
[3] => Array
(
[655] => Array
(
[step_number] => 3)
....
[4] => Array
(
[33] => Array
(
[step_number] => 4)
....
[5] => Array
(
[444] => Array
(
[step_number] => 5)
....
but when i do
$all = array_merge($first, $second);
the keys are
(
[0] => Array
[1] => Array
[2] => Array
[3] => Array
[4] => Array
Is that possible to do....
Try array_merge_recursive .. http://php.net/array_merge_recursive
This is also from the manual on array_splice if you're just looking to append your 2nd array onto the first (http://php.net/manual/en/function.array-push.php):
array_splice($first, count($first), 0, $second);
Iterating over the first set of keys, then using the Union array operator should do the trick.
//$first = array(...);
//$second = array(...);
foreach ($second as $key => $value)
{
if (!isset($first[$key]))
{
$first[$key] = array();
}
$first[$key] += $value;
}