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
)
)
)
Related
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
)
)
)
I'm trying to access the second array (contacts) in this decoded json to grab the ID next to first_name (the two ID keys could be different), but the second array doesn't seem to be an object so I can't find a loop which can acces it:
stdClass Object (
[data] => Array (
[0] => stdClass Object (
[account_key] => jvg7qgtw2btrlmpigrq2zpco48eegxvv
[is_owner] => 1
[id] => 1
[name] => test test
[display_name] => test test
[balance] => 0
[paid_to_date] => 0
[updated_at] => 1578494555
[archived_at] =>
[address1] => Street
[address2] =>
[city] => Town
[state] => State
[postal_code] => Code
[country_id] => 0
[work_phone] => Number
[private_notes] =>
[public_notes] =>
[last_login] =>
[website] =>
[industry_id] => 0
[size_id] => 0
[is_deleted] =>
[payment_terms] => 30
[vat_number] =>
[id_number] =>
[language_id] => 0
[currency_id] => 0
[custom_value1] =>
[custom_value2] =>
[invoice_number_counter] => 1
[quote_number_counter] => 1
[task_rate] => 0
[shipping_address1] =>
[shipping_address2] =>
[shipping_city] =>
[shipping_state] =>
[shipping_postal_code] =>
[shipping_country_id] => 0
[show_tasks_in_portal] => 1
[send_reminders] => 1
[credit_number_counter] => 1
[custom_messages] => {}
[contacts] => Array (
[0] => stdClass Object (
[account_key] => jvg7qgtw2btrlmpigrq2zpco48eegxvv
[is_owner] => 1
[id] => 1
[first_name] => test
[last_name] => test
[email] => myemail#me.com
[contact_key] => mq1dzpkqznfgtqwhdwt9nte1ohmvsju1
[updated_at] => 1578494555
[archived_at] =>
[is_primary] => 1
[phone] => 07919446174
[last_login] =>
[send_invoice] => 1
[custom_value1] =>
[custom_value2] =>
)
)
)
)
[meta] => stdClass Object (
[pagination] => stdClass Object (
[total] => 1
[count] => 1
[per_page] => 15
[current_page] => 1
[total_pages] => 1
[links] => Array ( )
)
)
)
This is what I've tried, but it doesn't find anything:
$person = getclient($itemid);
$person_data = json_decode($person);
foreach ($person_data->contacts as $key => $item)
{
$itemid = $item->id . "<br />";
}
$person_data contains the data property, which is an array of objects.
foreach ($person_data->data as $person) {
foreach ($person->contacts as $contact) {
echo $contact->id . '<br>';
}
}
I have two array i want to add another key in first array by matching keys with second array below are the array that I have
First Array
Array
(
[0] => Array
(
[id] => 5198
[full_name] => Afnan
[username] => eay.d
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/haulysu4upload-image1499828198.jpg
)
[1] => Array
(
[id] => 5213
[full_name] => Nouf
[username] => noufalswailem
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/jlyfgi4fupload-image1502276119.jpg
)
[2] => Array
(
[id] => 5218
[full_name] => Mohammed Bin Abdullah
[username] => almuribadh
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/m3ttx0luupload-image1500789921.jpg
)
[3] => Array
(
[id] => 5225
[full_name] => Shadin Alshobaily
[username] => shash
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/srura4raupload-image1499829155.jpg
)
[4] => Array
(
[id] => 5251
[full_name] => Razan
[username] => ra.m
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/rwfieka6upload-image1499831173.jpg
)
)
Second array that I have
Array
(
[0] => Array
(
[id] => 38395
[request_by] => 2
[request_to] => 5198
)
)
Required Result
Array
(
[0] => Array
(
[id] => 5198
[full_name] => Afnan
[username] => eay.d
[is_live] => 1
[request_sent] => true
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/haulysu4upload-image1499828198.jpg
)
[1] => Array
(
[id] => 5213
[full_name] => Nouf
[username] => noufalswailem
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/jlyfgi4fupload-image1502276119.jpg
)
[2] => Array
(
[id] => 5218
[full_name] => Mohammed Bin Abdullah
[username] => almuribadh
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/m3ttx0luupload-image1500789921.jpg
)
[3] => Array
(
[id] => 5225
[full_name] => Shadin Alshobaily
[username] => shash
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/srura4raupload-image1499829155.jpg
)
[4] => Array
(
[id] => 5251
[full_name] => Razan
[username] => ra.m
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/rwfieka6upload-image1499831173.jpg
)
)
This is just because requested by user 2 has sent friend request to 5198 user and so that's why request_sent is true to this user only and remining will false if friend request not send I want to do this work using buitl in function instead of loops.
Seems very easy, may be i don't uderstand the task
$tmp = array_flip(array_column($arr2, 'request_to'));
foreach($arr1 as &$v) {
$v['request_sent'] = isset($tmp[$v[id]]);
}
Solution without loops (check it online):
<?php
$users = [
['id' => 5198, 'full_name' => 'Afnan', 'username' => 'eay.d', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5213, 'full_name' => 'Nouf', 'username' => 'noufalswailem', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5218, 'full_name' => 'Mohammed Bin Abdullah', 'username' => 'almuribadh', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5225, 'full_name' => 'Shadin Alshobaily', 'username' => 'shash', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5251, 'full_name' => 'Razan', 'username' => 'ra.m', 'is_live' => 1, 'picture' => 'someurl'],
];
$requests = [
['request_by' => 2, 'request_to' => 5198],
['request_by' => 2, 'request_to' => 5225],
];
array_walk($users, 'setUserRequestStatus', array_column($requests, 'request_to'));
var_dump($users);
function setUserRequestStatus(&$user, $key, $requests_sent_to) {
$user['request_sent'] = in_array($user['id'], $requests_sent_to);
}
I'm writing this query in yii2 -
$senderInfo[] = Customers::find()->where(['cust_id' => $resShipment['ship_shipper_id'], 'cust_country_code' => Yii::$app->user->identity->country_code])->distinct()->all();
and getting this as a result -
Array ( [0] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( [0] => common\models\Customers Object ( [_attributes:yii\db\BaseActiveRecord:private] => Array ( [cust_id] => 79 [cust_user_id] => 2 [cust_designation] => Mr [cust_fname] => abcd [cust_mname] => xyz [cust_lname] => xyz [cust_country_code] => 91 [cust_mobile] => 8888888888 [cust_email] => abcdxyz#gmail.com [cust_id_proof] => [cust_country] => [cust_state] => [cust_city] => [cust_location] => [cust_street] => [cust_address] => delhi [cust_pobox] => 02881 [cust_additional_detail] => [cust_picture] => [verified] => Yes [created] => 2016-04-20 14:45:42 [updated] => 0000-00-00 00:00:00 [deleted] => No ) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ( [cust_id] => 79 [cust_user_id] => 2 [cust_designation] => Mr [cust_fname] => abcd [cust_mname] => xyz [cust_lname] => xyz [cust_country_code] => 91 [cust_mobile] => 8888888888 [cust_email] => abcdxyz#gmail.com [cust_id_proof] => [cust_country] => [cust_state] => [cust_city] => [cust_location] => [cust_street] => [cust_address] => delhi [cust_pobox] => 413714 [cust_additional_detail] => [cust_picture] => [verified] => Yes [created] => 2016-04-20 14:45:42 [updated] => 0000-00-00 00:00:00 [deleted] => No ) [_related:yii\db\BaseActiveRecord:private] => Array ( ) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => Array ( ) ) ) )
But, because of the empty arrays in the start of this array, I can't access the elements. I've to populate it in a for(strictly) loop, after it gets accessible.
If you want the result in array you can use ActiveRecord function toArray()
$myModel = Customers::find()->
where(['cust_id' => $resShipment['ship_shipper_id'],
'cust_country_code' => Yii::$app->user->identity->country_code])->distinct()->all();
$senderInfo = myModel->toArray();
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);