How to Merge Arrays with WHERE Clause? - php

How to add 2nd array into 1st array where [ myid ] matches.
1st Array
Array
(
[0] => Array
(
[myid] => 70
[realname] => Kishore
[full_name] => Kishore Chandra
[category] => professional
[firm_name] => Yes
[designation] => Mechanical
[address] => Dwarakanagar 5th lane
[city] => Vishakhapatnam
[email] => yesapps.india#gmail.com
)
[1] => Array
(
[myid] => 75
[realname] => Vinod kumar
[full_name] => Kishore Chandra
[category] => professional
[firm_name] =>
[designation] =>
[address] =>
[city] =>
[email] => vinod.k.alluri#gmail.com
)
)
2nd Array
Need these projects to be added into Array 1
Array
(
[0] => Array
(
[myid] => 70
[projects] => 20
)
[1] => Array
(
[myid] => 75
[projects] => 43
)
)
I have tried to Merge Array's but no use, i am getting 2 more objects in to this array
I hope my requirement is clear and readable, if not please mention in comment so i could explain it more.
The answer can be in codeigniter also i am using Codeigniter framework

assume that you want the result array to be merging the projects to your first array
you want the result set to be like this
Array
(
[0] => Array
(
[myid] => 70
[realname] => Kishore
[full_name] => Kishore Chandra
[category] => professional
[firm_name] => Yes
[designation] => Mechanical
[address] => Dwarakanagar 5th lane
[city] => Vishakhapatnam
[email] => yesapps.india#gmail.com
[projects] => 20
)
[1] => Array
(
[myid] => 75
[realname] => Vinod kumar
[full_name] => Kishore Chandra
[category] => professional
[firm_name] =>
[designation] =>
[address] =>
[city] =>
[email] => vinod.k.alluri#gmail.com
[projects] => 43
)
)
so try this code
foreach($b as $key => $val){
if(isset($a[$key]) && $a[$key]->myid == $val->myid){
$a[$key]->projects = $val->projects;
}
}
if converted the stdArray to Array
json_decode($a,TRUE)
and json_decode($b, TRUE)
$a = Array
(
0 => Array
(
'myid' => 70,
'realname' => 'Kishore',
'full_name' => 'Kishore Chandra',
'category' => 'professional',
'firm_name' => 'Yes',
'designation' => 'Mechanical',
'address' => 'Dwarakanagar 5th lane',
'city' => 'Vishakhapatnam',
'email' => 'yesapps.india#gmail.com',
),
1 => Array
(
'myid' => 75,
'realname' => 'Vinod kumar',
'full_name' => 'Kishore Chandra',
'category' => 'professional',
'firm_name' => '',
'designation' => '',
'address' => '',
'city' => '',
'email' => 'vinod.k.alluri#gmail.com'
)
);
$b = Array ( 0 => Array(
'myid' => 70,
'projects' => 20
),
1 => Array(
'myid' => 75,
'projects' => 43
)
);
foreach($b as $key => $val){
if(isset($a[$key]) && $a[$key]['myid'] == $val['myid']){
$a[$key]['projects'] = $val['projects'];
}
}
print_r($a);
NOTE:
$b is the 2nd array
$a is the 1st array

foreach($array1 as $key => $array){
foreach($array as $key2 => $value){
$results[$key]['myid'] = $value;
}
}
foreach($array2 as $key => $array){
foreach($array as $key3 => $value){
$results[$key3]['projects'] = $value;
}
}
print_r($results);

Related

merge two array with loop and key name

i have two separate arrays $first_one:
Array
(
[0] => Array
(
[_date] => 2019-10-16
[_number] => 1
[_order] => 1
[name] => jack
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array
(
[_date] => 2020-10-11
[_number] => 2
[_order] => 2
[name] => joey
[other_ids] => Array
(
[b_id] => 1433
)
)
)
and the $second_array:
Array
(
[0] => Array
(
[date] => 2019-10-16
[number] => 1
[order] => 1
[name] => jack
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array
(
[date] => 2020-10-11
[number] => 2
[order] => 2
[name] => joey
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1433
)
)
[2] => Array
(
[date] => 2020-10-28
[number] => 3
[order] => 3
[name] => tom
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1593
)
)
)
they are very similar but they came from different api's and they are different in numbers and also some key names.
so what i'm trying to do is to count the $first_one arrays and if for that many loop through the $second_one (in this example is 2) and if the [_number] [_order] from $first_one was equal (==) to [number] [number] $second_one then take some info (for example the last name) from it and put in a new array.
so is this possible to do?
$arr1 = [ [ '_date' => '2019-10-16','_number' => 1,'_order' => 1,
'name' => 'jack','other_ids' => ['b_id' => 1253]
],
['_date' => 2020-10-11,'_number' => 2,'_order' => 2,
'name' => 'joey','other_ids' => ['b_id' => 1433]
]
];
$arr2 = [ [ 'date' => '2019-10-16','number' => '1','order' => '1',
'name' => 'jack','last_name' => 'foobar','other_ids' => ['b_id' => 1253]
],
[ 'date' => '2019-10-11','number' => '2','order' => '2',
'name' => 'joey','last_name' => 'foobar','other_ids' => ['b_id' => 1433]
],
[ 'date' => '2019-10-28', 'number' => '3', 'order' => '3',
'name' => 'tom', 'last_name' => 'foobar', 'other_ids' => ['b_id' => 1593]
],
];
// first make second array more directly searchable
// make new array with the `number` as the key
foreach( $arr2 as $a){
$arr2new[$a['number']] = $a;
}
foreach ($arr1 as $a) {
if ( array_key_exists($a['_number'], $arr2new) &&
$a['_order'] == $arr2new[$a['_order']]['order'] )
{
$merged[] = ['name'=>$a['name'], 'other_ids' => $a['other_ids']];
}
}
print_r($merged);
RESULT
Array
(
[0] => Array (
[name] => jack
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array (
[name] => joey
[other_ids] => Array
(
[b_id] => 1433
)
)
)

Merge array by id and sum amount

I'm trying to merge and calc only by choosen key. For example I have the following array:
$array = array();
$array['Fruit'][] = ['id' => 17, 'name' => 'seasonal fruit', 'type' => 'Fruit', 'measurement' => '', 'amount' => 2];
$array['Fruit'][] = ['id' => 17, 'name' => 'seasonal fruit', 'type' => 'Fruit', 'measurement' => '', 'amount' => 1];
$array['protein'][] = ['id' => 16, 'name' => 'soy yogurt', 'type' => 'protein', 'measurement' => '', 'amount' => 2];
$array['protein'][] = ['id' => 18, 'name' => 'oilseed', 'type' => 'protein', 'measurement' => 'grip', 'amount' => 2];
$array['protein'][] = ['id' => 18, 'name' => 'oilseed', 'type' => 'protein', 'measurement' => 'grip', 'amount' => 1];
$array['lipid'][] = ['id' => 19, 'name' => 'coconut powder', 'type' => 'lipid', 'measurement' => 'Tablespoon', 'amount' => 2];
$array['lipid'][] = ['id' => 20, 'name' => 'chocolate powder', 'type' => 'lipid', 'measurement' => 'Tablespoon', 'amount' => 2];
$array['lipid'][] = ['id' => 38, 'name' => 'chocolate square', 'type' => 'lipid', 'measurement' => '', 'amount' => 1];
Final output should look like this :
Array (
[0] => Array (
[id] => 17
[name] => seasonal fruit
[type] => Fruit
[measurement] =>
[amount] => 3
)
[1] => Array (
[id] => 16
[name] => soy yogurt
[type] => protein
[measurement] =>
[amount] => 2
)
[2] => Array (
[id] => 18
[name] => oilseed
[type] => protein
[measurement] => grip
[amount] => 3
)
[3] => Array (
[id] => 19
[name] => coconut powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[4] => Array (
[id] => 20
[name] => chocolate powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[5] => Array (
[id] => 38
[name] => chocolate square
[type] => lipid
[measurement] =>
[amount] => 1
)
)
I tried the following :
$courses = [];
foreach ($array as $key => $item) {
if (!key_exists($key, $courses)) {
$courses[$key] = [];
}
foreach ($item as $row) {
$id = $row['id'];
if (!key_exists($id, $courses[$key])) {
$courses[$key][$id] = $row;
continue;
}
$r = &$courses[$key][$id];
$r['amount'] += $row['amount'];
}
}
Output:
Array (
[Fruit] => Array (
[17] => Array (
[id] => 17
[name] => seasonal fruit
[type] => Fruit
[measurement] =>
[amount] => 3
)
)
[protein] => Array (
[16] => Array (
[id] => 16
[name] => soy yogurt
[type] => protein
[measurement] =>
[amount] => 2
)
[18] => Array (
[id] => 18
[name] => oilseed
[type] => protein
[measurement] => grip
[amount] => 3
)
)
[lipid] => Array (
[19] => Array (
[id] => 19
[name] => coconut powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[20] => Array (
[id] => 20
[name] => chocolate powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[38] => Array (
[id] => 38
[name] => chocolate square
[type] => lipid
[measurement] =>
[amount] => 1
)
)
)
You were not far off, but everything you are interested in ins inside the inner foreach, that way you dont get the Fruit and Protien etc involved
$courses = [];
foreach( $array as $sub){
foreach ($sub as $item) {
if ( array_key_exists($item['id'], $courses) ) {
// dup, so just add to the amount
$courses[$item['id']]['amount'] += $item['amount'];
} else {
//New entry
$courses[$item['id']] = $item;
}
}
}
// if its important to have indexes starting at 0 for the result
$courses = array_values($courses);

Remove parent array including the children if the children value found empty

I have this array structure.
I'm trying to remove those whole array if I found the [empevalpptwo] empty or null, so if the [empevalpptwo] is empty the whole [4] => Array should be remove.
Right now i'm just trying to get the parent index so I can just remove it by index
Is there any good solution like filtering recursively?
$kra = array_column($ppform_plan->toArray(), 'kra');
$emp_eval_pptwo = array_search('', array_column($kra, 'empevalpptwo'));
//should return 4
Array
(
[4] => Array
(
[id] => 50
[user_id] => 6282
[specific_user_id] => 6281
[eval_cat_id] => 2
[format_cat] => 1
[title] => This istesting
[desc] =>
[weight] => 50
[bsc_weight_group] =>
[bsc_rating] =>
[sequence] =>
[created_at] => 2019-05-22 10:55:23
[updated_at] => 2019-05-22 10:55:23
[kra] => Array
(
[0] => Array
(
[id] => 77
[user_id] => 6282
[bsc_id] => 50
[index] => 0
[kra_title] => ttes lang muna
[kra_desc] =>
[kra_weight] => 25
[sat] => 521
[at] => 4
[ot] => 535
[rating_per_kra] =>
[rating_per_kra_cat] =>
[net_weighting] => 5
[rank] => 1
[remarks] =>
[indicator_text] =>
[created_at] =>
[updated_at] =>
[empevalpptwo] =>
)
[1] => Array
(
[id] => 78
[user_id] => 6282
[bsc_id] => 50
[index] => 1
[kra_title] => talga e2 pa o
[kra_desc] =>
[kra_weight] => 25
[sat] => 5
[at] => 2
[ot] => 4
[rating_per_kra] =>
[rating_per_kra_cat] =>
[net_weighting] => 4
[rank] => 2
[remarks] =>
[indicator_text] =>
[created_at] =>
[updated_at] =>
[empevalpptwo] =>
)
)
)
)
Something akin to:
<?php
$items =
[
1 =>
[
'foo'=>
[
[
'bar' => '',
],
[
'bar' => '',
]
]
],
2 =>
[
'foo'=>
[
[
'bar' => 'baz'
]
]
]
];
foreach ($items as $k => $item)
if(empty(array_filter(array_column($item['foo'], 'bar'))))
unset($items[$k]);
var_export($items);
Output:
array (
2 =>
array (
'foo' =>
array (
0 =>
array (
'bar' => 'baz',
),
),
),
)
You should try this :
$data = array_map('array_filter', $data);
$data = array_filter($data);
UPDATED
and for checking inner values you can use this :
$array = array_filter($array, function($v) { return !empty($v['empevalpptwo']); });
Orginal
Array
(
[con] => Array
(
[address] => Array
(
[city] => ahmedabad
)
[personalinfo] =>
)
)
Result :
Array
(
[con] => Array
(
[address] => Array
(
[city] => ahmedabad
)
)
)

how to reorder array php?

i have array and i need to reorder by another array:
here is my array:
Array
(
[0] => Array
(
[name] => Haroldas
[number] => 444
[address] => g.
[city] => eee
[country] => f
[lastname] => r
)
[1] => Array
(
[name] => Lukas
[number] => 999
[address] => rrr
[city] => tttt
[country] => 3
[lastname] => r
)
)
This is another array with columns number which show me how to order number (columns order can to be another):
Array
(
[lastname] => 4
[name] => 1
[number] => 5
[address] => 3
[city] => 0
[country] => 2
)
i need result like this:
Array
(
[0] => Array
(
[city] => eee
[name] => Haroldas
[country] => f
[address] => g.
[lastname] => r
[number] => 444
)
...
)
This should do the job:
<?php
$order = [
'city' => 0,
'name' => 1,
'country' => 2,
'address' => 3,
'lastname' => 4,
'number' => 5,
];
$data = [
[
'name' => 'Haroldas',
'number' => '444',
'address' => 'g.',
'city' => 'eee',
'country' => 'f',
'lastname' => 'r',
],
[
'name' => 'Lukas',
'number' => '999',
'address' => 'rrr',
'city' => 'ttt',
'country' => 3,
'lastname' => 'r',
],
];
foreach ($data as &$entry) {
uksort($entry, function ($a, $b) use ($order) {
return strcmp($order[$a], $order[$b]);
});
}
print_r($data);
The output is:
Array
(
[0] => Array
(
[city] => eee
[name] => Haroldas
[country] => f
[address] => g.
[lastname] => r
[number] => 444
)
[1] => Array
(
[city] => ttt
[name] => Lukas
[country] => 3
[address] => rrr
[lastname] => r
[number] => 999
)
)
If you want to reorder that use foreach then create a new array.
$arrReorder = [];
foreach($yourArr as $key => $val){
$arrReorder[] = ['city' => $val['city'], 'name' => $val['name'], 'country' => $val['country'], 'address' => $val['address'], 'lastname' => $val['lastname'], 'number' => $val['number'] ];
}
print_r($arrReorder);

Merging Data of MultiDimensional Array

I have this kind of array
Array ( [0] => Array ( [id] => 1
[name] => PeopleOne
[address] => AddressOneOfPeopleOne
[1] => Array ( [id] => 2
[name] => PeopleTwo
[address] => AddressOneOfPeopleTwo
[2] => Array ( [id] => 3
[name] => PeopleThree
[address] => AddressOneOfPeopleThree
[3] => Array ( [id] => 4
[name] => PeopleOne
[address] => AddressTwoOfPeopleOne
and I want this kind of format
Array ( [0] => Array ( [id] => 1
[name] => PeopleOne
[address] => Array(
[0] => AddressOneOfPeopleOne
[1] => AddressTwoOfPeopleOne
)
[1] => Array ( [id] => 2
[name] => PeopleTwo
[address] => AddressOneOfPeopleTwo
[2] => Array ( [id] => 3
[name] => PeopleThree
[address] => AddressOneOfPeopleThree
I don't know how to do?
Could anyone please solve this?
I have a feeling this isn't exactly what you're looking for, but I'm not entirely sure what needs to be inside the final array. How is this, at least for a start?
<?php
$array = array(
array('id' => 1, 'name' => 'PeopleOne', 'address' => 'Address1'),
array('id' => 2, 'name' => 'PeopleTwo', 'address' => 'Address2'),
array('id' => 3, 'name' => 'PeopleOne', 'address' => 'Address3')
);
foreach ($array as $k => $v) {
$newarray[$v['name']][] = $v['address'];
}
echo '<pre>'.print_r($array,1).'</pre>';
echo '<pre>'.print_r($newarray,1).'</pre>';
?>
I'll assume that your array is called $array.
foreach ($array as $subArray) {
if (!is_array($subArray['address']) {
$subArray['address'] = array($subArray['adress']);
}
$subArray['address'][] = 'AddressTwoOfPeopleOne';
}

Categories