merge two array with loop and key name - php

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
)
)
)

Related

Compare and Update PHP array

I have two arrays in PHP:
Array 1
Array
(
[0] => Array
(
[autoid] => t35wmkbg
[task] => Zaphod
[progress] => incomplete
)
[1] => Array
(
[autoid] => cwg2yc5q
[task] => Arthur
[progress] => incomplete
)
[2] => Array
(
[autoid] => 85q4bcmy
[task] => Ford
[progress] => incomplete
)
[3] => Array
(
[autoid] => yc5qcwg2
[task] => Trillian
[progress] => incomplete
)
)
Array 2
Array
(
[0] => Array
(
[autoid] => t35wmkbg
[task] => Zaphod
[progress] => complete
)
[1] => Array
(
[autoid] => 85q4bcmy
[task] => Ford
[progress] => incomplete
)
[2] => Array
(
[autoid] => cwg2yc5q
[task] => Arthur
[progress] => pending
)
)
I need to:
Step through Array 1, and check if the autoid exists in Array 2.
If it does exist, update the progress field to match.
So the ideal output when using the arrays above would be:
Array 3
Array
(
[0] => Array
(
[autoid] => t35wmkbg
[task] => Zaphod
[progress] => complete
)
[1] => Array
(
[autoid] => cwg2yc5q
[task] => Arthur
[progress] => pending
)
[2] => Array
(
[autoid] => 85q4bcmy
[task] => Ford
[progress] => incomplete
)
[3] => Array
(
[autoid] => yc5qcwg2
[task] => Trillian
[progress] => incomplete
)
)
I am not sure how best to go about this, if anyone has any thoughts or pointers that would be awesome.
First change the arr2 so its index is the autoid then you can use it easily to get the progress value.
Then just foreach over arr1 and apply changes to progress if they exist
$arr1 = [
['autoid' => 't35wmkbg','task' => 'Zaphod','progress' => 'incomplete'],
['autoid' => 'cwg2yc5q', 'task' => 'Arthur', 'progress' => 'incomplete'],
['autoid' => '85q4bcmy', 'task' => 'Ford', 'progress' => 'incomplete'],
['autoid' => 'yc5qcwg2', 'task' => 'Trillian', 'progress' => 'incomplete']
];
$arr2 = [
['autoid' => 't35wmkbg', 'task' => 'Zaphod', 'progress' => 'complete'],
['autoid' => '85q4bcmy', 'task' => 'Ford', 'progress' => 'incomplete'],
['autoid' => 'cwg2yc5q', 'task' => 'Arthur', 'progress' => 'pending']
];
#first make arr2 indexable on the autoid
foreach ($arr2 as $a) {
$arr2search[$a['autoid']] = $a;
}
foreach ($arr1 as $a){
// do we have an autoid in arr2
if ( isset($arr2search[$a['autoid']])){
$a['progress'] = $arr2search[$a['autoid']]['progress'];
}
$merged[] = $a;
}
print_r($merged);
RESULT
Array
(
[0] => Array
([autoid] => t35wmkbg, [task] => Zaphod, [progress] => complete)
[1] => Array
([autoid] => cwg2yc5q, [task] => Arthur, [progress] => pending )
[2] => Array
([autoid] => 85q4bcmy, [task] => Ford, [progress] => incomplete )
[3] => Array
([autoid] => yc5qcwg2, [task] => Trillian, [progress] => incomplete )
)

how to add new key and value to multidimensional in PHP without using the loops....?

Array ( [0] => Array (
[date] => 01-06-2018
[nav] => 30.65100 )
[1] => Array (
[date] => 31-05-2018
[nav] => 30.84900 )
[2] => Array (
[date] => 30-05-2018
[nav] => 30.73200 )
[3] => Array (
[date] => 29-05-2018
[nav] => 30.81500 )
The above code is the Multi-array, we have added a common id like id_code = 0089 to every array in it without using any loops in PHP. Can anyone helps me and it is possible or not .....?
If you mean not manually loop through the array then yes, it is possible using array_walk:
$array = [
0 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
1 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
2 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
3 => [
"date" =>"01-06-2018",
"nav" => "30.65100"]
];
array_walk($array, function(&$item1) {
$item1['id_code'] = "0089";
});
print_r($array);
Output:
Array
(
[0] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[1] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[2] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[3] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
)
Demo https://3v4l.org/lCGIO
An effective solution will be to use array_map function as:
$keyValue = 'some value';
$data = array_map(function($d) use ($keyValue){
return $d + ['keyName' => $keyValue];
}, $data);
I think this is the closer you can get, but it is probably not your expected result. The exact result you need cannot be done without using a loop as far as I know.
$t = array( 0 => array( 'date' => '01-06-2018', 'nav' => '30.65100' ), 1 => array( 'date' => '31-05-2018', 'nav' => '30.84900' ), 2 => array( 'date' => '30-05-2018', 'nav' => '30.73200' ), 3 => array( 'date' => '29-05-2018', 'nav' => '30.81500' ));
$tt = array( 0 => array( 'id' => '648'), 1 => array( 'id' => '332'), 2 => array( 'id' => '889'), 3 => array( 'id' => '285') );
$final = array_map(null, $t, $tt);
print_r($final);
The output will look like
Array
(
[0] => Array
(
[0] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
)
[1] => Array
(
[id] => 648
)
)
[1] => Array
(
[0] => Array
(
[date] => 31-05-2018
[nav] => 30.84900
)
[1] => Array
(
[id] => 332
)
)
[2] => Array
(
[0] => Array
(
[date] => 30-05-2018
[nav] => 30.73200
)
[1] => Array
(
[id] => 889
)
)
[3] => Array
(
[0] => Array
(
[date] => 29-05-2018
[nav] => 30.81500
)
[1] => Array
(
[id] => 285
)
)
)

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
)
)
)

PHP For Each Append Array

Example code array: https://eval.in/639002
A guest with (user_id = 1) bought multiple tickets (ticket_id = 1 & 2) On event (event_id = 11).
I am expecting the result format:
[ticket] => Array
(
[1] => Ticket Name, ***
[2] => Ticket Name
)
Code example below:
$data_db = array(
array(
'id' => 1,
'user_id' => 1,
'event_id' => 11,
'ticket_id' => 1,
'user_name' => 'guest 1'
),
array(
'id' => 2,
'user_id' => 2,
'event_id' => 11,
'ticket_id' => 1,
'user_name' => 'guest 2'
),
array(
'id' => 3,
'user_id' => 3,
'event_id' => 22,
'ticket_id' => 1,
'user_name' => 'guest 3'
),
array(
'id' => 4,
'user_id' => 1,
'event_id' => 11,
'ticket_id' => 2,
'user_name' => 'guest 1'
)
);
$output = [];
foreach ( $data_db as $key => $row ) {
$output[ $row['event_id'] ]['event'] = 'Event Name';
$output[ $row['event_id'] ]['attendee'][ $row['user_id'] ] = $row;
$output[ $row['event_id'] ]['attendee'][ $row['user_id'] ]['ticket'][ $row['ticket_id'] ] = 'Ticket Name';
}
print_r($output);
Current result
Array
(
[11] => Array
(
[event] => Event Name
[attendee] => Array
(
[1] => Array
(
[id] => 4
[user_id] => 1
[event_id] => 11
[ticket_id] => 2
[user_name] => guest 1
[ticket] => Array
(
[2] => Ticket Name
)
)
[2] => Array
(
[id] => 2
[user_id] => 2
[event_id] => 11
[ticket_id] => 1
[user_name] => guest 2
[ticket] => Array
(
[1] => Ticket Name ***
)
)
)
)
[22] => Array
(
[event] => Event Name
[attendee] => Array
(
[3] => Array
(
[id] => 3
[user_id] => 3
[event_id] => 22
[ticket_id] => 1
[user_name] => guest 3
[ticket] => Array
(
[1] => Ticket Name
)
)
)
)
)
First of you could start by sorting out an array of tickets of which ticket belongs to which user:
$output = [];
foreach($data_db as $key=>$row) {
// Define new $tickets Array E.g ['guest_name']=>['ticket_id']
$tickets[$row['user_name']][] = $row['ticket_id'];
$output [] = [
'id'=>$row['id'],
'user_id'=>$row['user_id'],
'event_id'=>$row['event_id'],
'user_name'=>$row['user_name'],
'tickets'=>[]
];
}
Your output of $tickets would look like:
Array
(
[guest 1] => Array
(
[0] => 1
[1] => 2
)
[guest 2] => Array
(
[0] => 1
)
[guest 3] => Array
(
[0] => 1
)
)
Then you can combine them, like this:
foreach($output as $k=>$v){
foreach($tickets as $guest=>$ticket){
if($v['user_name'] == $guest){
$output[$k]['tickets'] = $ticket;
}
}
}
Your output is:
Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[event_id] => 11
[user_name] => guest 1
[tickets] => Array
(
[0] => 1
[1] => 2
)
)
[1] => Array
(
[id] => 2
[user_id] => 2
[event_id] => 11
[user_name] => guest 2
[tickets] => Array
(
[0] => 1
)
........

How to Merge Arrays with WHERE Clause?

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);

Categories