associative array check any column is empty or not - php

I have associative array like -
[0] => Array
(
[date] => 2018-06-22
[id] => 2282991
[type] => VIDEO
[domain] =>
[code] => Austin
[address] => Phone
)
[1] => Array
(
[date] => 2018-06-22
[id] => 2282991
[type] => VIDEO
[domain] =>
[code] =>
[address] => Phone
)
[3] => Array
(
[date] => 2018-06-22
[id] => 2282991
[type] => VIDEO
[domain] =>
[code] => Austin
[address] => Phone
)
I need to check is there any column having all the values are blank.
That means it should return only domain from above array because it is blank everywhere.
Is there any way to do this with minimum use of forloop? I need to check this for all these columns.

This will work if your subarray having same number of keys.Like "Date, id, Type etc".
$array = [
[ "date" => "2018-06-22", "id" => 2282991, "type" => "VIDEO", "domain" =>'', "code" => "Austin", "address" => "Phone"],
[ "date" => "2018-06-22", "id" => 2282991, "type" => "VIDEO", "domain" =>'', "code" => "", "address" => "Phone"],
[ "date" => "2018-06-22", "id" => 2282991, "type" => "VIDEO", "domain" =>'', "code" => "Austin", "address" => "Phone"]
];
$empty = [];
foreach($array[0] as $key=>$val){
$error = array_column($array, $key);
if(empty (array_filter($error)) ) {
$empty[] = $key;
}
}
print_r($empty);
Output:
Array
(
[0] => domain
)

Related

Change specific value in Dynamic nested array with parent and children structure PHP

I have a dynamic nested array structure with unique id and name, need to change value of name for a specific id given.
I have a dynamic nested array with structure
`
$array = [
[
"id" => "31350880",
"name" => "HOD",
"children" => [
[
"id" => "57f94cd7",
"parent_id" => "31350880",
"name" => "New HOD",
"children" => [
[
"id" => "e7f1c88b",
"parent_id" => "57f94cd7",
"name" => "Gold",
"children" => [
]
]
]
]
]
],
[
"id" => "45881fa8",
"name" => "Pictures",
"children" => [
[
"id" => "770e6e20",
"parent_id" => "45881fa8",
"name" => "New Picture",
"children" => [
[
"id" => "a403a8fa",
"parent_id" => "770e6e20",
"name" => "Silver",
"children" => [
]
]
]
]
]
]
];
`
For a given ID(unique) , need to find it from array and change the name from that node to a specific name.
for example : $id = ''770e6e20' which is a child of parent node with name "Pictures", need to find child node with specific id and change its name and retrieve the full array in its initial structure ?
This is a recursive function that gets a reference of your array, the id of the item and the new name to be assigned, and performs in place modification:
function modify_element_name(&$arr, $id, $name){
foreach($arr as $index => $item){
if($item['id'] === $id){
$arr[$index]['name'] = $name;
}
else {
if(count($item['children']) > 0){
modify_element_name($arr[$index]['children'], $id, $name);
}
}
}
}
Calling it as below:
modify_element_name($array, '770e6e20', 'New Picture Modified');
Will modify the array:
Array
(
[0] => Array
(
[id] => 31350880
[name] => HOD
[children] => Array
(
[0] => Array
(
[id] => 57f94cd7
[parent_id] => 31350880
[name] => New HOD
[children] => Array
(
[0] => Array
(
[id] => e7f1c88b
[parent_id] => 57f94cd7
[name] => Gold
[children] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 45881fa8
[name] => Pictures
[children] => Array
(
[0] => Array
(
[id] => 770e6e20
[parent_id] => 45881fa8
[name] => New Picture Modified
[children] => Array
(
[0] => Array
(
[id] => a403a8fa
[parent_id] => 770e6e20
[name] => Silver
[children] => Array
(
)
)
)
)
)
)
)

How to change the collection on the basis of array in column value

I want to change the laravel collection because I want to pass this collection to generate report.
How to change collection on the basis of the value in the column.
Previous:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 23
[form_type] => radio
[details] => [{"id":55 title:"AA" name:"BB"}, {"id":56 title:"CC" name:"DD"}]
)
[1] => stdClass Object
(
[id] => 24
[form_type] => checkbox
[details] => [{"id":57 title:"PP" name:"QQ"}, {"id":58 title:"RR" name:"SS"}]
)
)
)
--details column has array of objects, I want it as below format:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 23
[form_type] => radio
[title] => "AA"
[name] => "BB"
)
[1] => stdClass Object
(
[id] => 23
[form_type] => radio
[title] => "CC"
[name] => "DD"
)
[2] => stdClass Object
(
[id] => 24
[form_type] => checkbox
[title] => "PP"
[name] => "QQ"
)
[4] => stdClass Object
(
[id] => 24
[form_type] => checkbox
[title] => "RR"
[name] => "SS"
)
)
)
The only way is to use each method of Laravel collection. I expect the data in the following format and you will get the desired output.
$data = collect([
(object)["id" => 23, "form_type" => "radio", "details" => [["id" => 55, 'title' => "AA", 'name' => "BB"], ["id" => 56, 'title' => "CC", 'name' => "DD"]]]
]);
$result = collect();
$data->each(function($d) use ($result) {
$d = collect($d);
unset($t['id']);
$tmp = collect($d['details']);
$tmp->each(function($t) use (&$d, $result) {
$result->push($d->forget('details')->merge($t));
});
});
echo "<pre>";
print_r($result);
try this with ->map() function then ->push() into new collection
$data = [
[
"id" => 23,
"form_type" => "radio",
"details" => [["id" => 55, "title" => "AA", "name" => "BB"], ["id" => 56, "title" => "CC", "name" => "DD"]]
],
[
"id" => 24,
"form_type" => "checkbox",
"details" => [["id" => 57, "title" => "PP", "name" => "QQ"], ["id" => 58, "title" => "RR", "name" => "SS"]]
],
];
$data = collect($data);
$final = collect();
$data->map(function ($row) use ($data, $final) {
return collect($row['details'])->map(function ($item) use ($data, $row, $final) {
$parentData = $data->where('id', $row['id'])->first();
$final->push([
'id' => $parentData['id'],
'title' => $item['title'],
'form_type' => $parentData['form_type'],
'name' => $item['name']
]);
});
});
return $final;

Forming final array with multiple array in PHP [duplicate]

This question already has answers here:
How to find array / dictionary value using key?
(2 answers)
Closed 3 years ago.
I need to form the final array, its not displaying how i am expecting, here is the code i used.
I need to print the final array in the expected format, I have attached the output how i am looking for in the below section,
<?php
$order_id = 12345;
$array1 = array(
"firstName" => "Test Fname",
"lastName" => "Test Lname",
"address1" => "Test Address",
"city" => "Test City",
"country" => "IND",
"email" => "test#blank.in"
);
$array2 = array(
"firstName" => "Test Fname",
"lastName" => "Test Lname",
"address1" => "Test Address",
"city" => "Test City",
"country" => "IND",
"email" => "test#blank.in"
);
$result = array("user_id" =>"9999","item_id" =>"cloth","price" =>"500","qty" =>"100");
$itemDetails = array();
foreach($result as $res){
$itemDetails[] =
array(
"User" => array(
"uservalue" => $res['user_id'],
"imageId" => $res['item_id']
),
"price" => $res['price'],
"qty" => $res['qty']
);
}
$final_array = array(
"id" => $order_id,
"billing" => $array1,
"shipping" => $array2,
"item" => $itemDetails
);
echo '<pre>';
print_r($final_array);die;
?>
Output of current Code
Array
(
[id] => 12345
[billing] => Array
(
[firstName] => Test Fname
[lastName] => Test Lname
[address1] => Test Address
[city] => Test City
[country] => IND
[email] => test#blank.in
)
[shipping] => Array
(
[firstName] => Test Fname
[lastName] => Test Lname
[address1] => Test Address
[city] => Test City
[country] => IND
[email] => test#blank.in
)
[item] => Array
(
[0] => Array
(
[User] => Array
(
[uservalue] => 9999
[imageId] => cloth
)
[price] => 500
[qty] => 100
)
[1] => Array
(
[User] => Array
(
[uservalue] => 9999
[imageId] => cloth
)
[price] => 500
[qty] => 100
)
[2] => Array
(
[User] => Array
(
[uservalue] => 9999
[imageId] => cloth
)
[price] => 500
[qty] => 100
)
)
)
Expected Result should be like below
array
(
"id" => 12345
"billing" => array
(
[firstName] => Test Fname,
[lastName] => Test Lname,
[address1] => Test Address,
[city] => Test City,
[country] => IND,
[email] => test#blank.in
)
"shipping" => array
(
[firstName] => Test Fname,
[lastName] => Test Lname,
[address1] => Test Address,
[city] => Test City,
[country] => IND,
[email] => test#blank.in
)
"item" => array
(
array
(
"User" => Array
(
"uservalue" => 9999,
"imageId" => cloth
)
[price] => 500,
[qty] => 100
)
)
)
I am expecting output like above, can we display the final array in that format, can anyone look into it and update me your thoughts. Thanks!!
if you allow only single item in itemdetails
$itemDetails[] =
[
"User" => [
"uservalue" => $result['user_id'],
"imageId" => $result['item_id'],
],
"price" => $result['price'],
"qty" => $result['qty'],
];
Single item demo
if itemdetails allow multiple items
$result = [
["user_id" =>"9999","item_id" =>"cloth","price" =>"500","qty" =>"100"],
["user_id" =>"9999","item_id" =>"cloth","price" =>"400","qty" =>"200"]
];
$itemDetails = array();
foreach($result as $res){
$itemDetails[] =
array(
"User" => array(
"uservalue" => $res['user_id'],
"imageId" => $res['item_id']
),
"price" => $res['price'],
"qty" => $res['qty']
);
}
Multiple items demo
If $result has multiple values, this should work.
$result = array(array("user_id" =>"9999","item_id" =>"cloth","price" =>"500","qty" =>"100"),array("user_id" =>"500","item_id" =>"cloth123","price" =>"511","qty" =>"80"));
$itemDetails = array();
foreach($result as $res){
$itemDetails[] =
array(
"User" => array(
"uservalue" => $res['user_id'],
"imageId" => $res['item_id']
),
"price" => $res['price'],
"qty" => $res['qty']
);
}
$final_array = array(
// "id" => $order_id,
// "billing" => $array1,
// "shipping" => $array2,
"item" => $itemDetails
);
echo '<pre>';
print_r($final_array);
Output
Array
(
[item] => Array
(
[0] => Array
(
[User] => Array
(
[uservalue] => 9999
[imageId] => cloth
)
[price] => 500
[qty] => 100
)
[1] => Array
(
[User] => Array
(
[uservalue] => 500
[imageId] => cloth123
)
[price] => 511
[qty] => 80
)
)
)

ElasticSearch - How to return data from elastic search with not empty value?

I've got a problem with eliminating empty values from a document of one of my index.
$params = [
"scroll" => "30s", // how long between scroll requests. should be small!
"size" => 1,
"index" => $eddIndex,
'type' => $eddIndexType
];
$response = $client->search ( $params );
while ( isset ( $response ['hits']['hits'] ) && count ( $response['hits']['hits'] ) > 0 ) {
$scroll_id = $response ['_scroll_id'];
$response = $client->scroll ( [
"scroll_id" => $scroll_id,
"scroll" => "30s"
] )
;
This will Return
Array
(
[_scroll_id] => cXVlcnlUaGVuRmV0Y2g7NTs0NTY0NDM6di1Bd2x4X0ZSaTJ5cnpVUkFHb3FRdzs0NTY0NDQ6di1Bd2x4X0ZSaTJ5cnpVUkFHb3FRdzs0NTY0NDY6di1Bd2x4X0ZSaTJ5cnpVUkFHb3FRdzs0NTY0NDc6di1Bd2x4X0ZSaTJ5cnpVUkFHb3FRdzs0NTY0NDU6di1Bd2x4X0ZSaTJ5cnpVUkFHb3FRdzswOw==
[took] => 12
[timed_out] =>
[_shards] => Array
(
[total] => 5
[successful] => 5
[failed] => 0
)
[hits] => Array
(
[total] => 378887
[max_score] => 1
[hits] => Array
(
[0] => Array
(
[_index] => index
[_type] => edd
[_id] => 13038
[_score] => 1
[_source] => Array
(
[reason] =>
[booking_date] => 2013-05-03T18:30:00.000Z
[booking_location] => ABC
[scan_edd] =>
[call_status] => Call
[patient_name] => ABCD
[#version] => 1
[effective_edd] =>
[id] => 13038
[email] => xxxxxxxxx#gmail.com
[last_known_edd] =>
[booking_amount] => 8000
[address] => xxxxxxxxxxxxxxxxx
[eda] => 2013-09-09T18:30:00.000Z
[edd] =>
[mpi] => xxxxxxx
[mobile] => xxxxxxx
[statuss] => Open Payment
[tags] => Array
(
[0] => edd
)
[last_consult_by] => Dr. XXXX [site] => BLR
[#timestamp] => 2018-07-06T06:30:39.040Z
[patient_id] => 75220
[last_consult_on] => 2014-02-28T18:30:00.000Z
[ip_booking] => XYX Package
[is_converted] => 0
)
)
)
)
)
If you look at my output, There is a field [effective_edd] returning empty.
I wanted to return data with [effective_edd] not empty.
I'm referring to the below document https://packagist.org/packages/elasticsearch/elasticsearch
You need to add a query that checks for the existence of the field to your scroll request:
$params = [
"scroll" => "30s", // how long between scroll requests. should be small!
"size" => 1,
"index" => $eddIndex,
'type' => $eddIndexType,
'body' => [
'query' => [
'exists' => [
'field' => 'effective_edd'
]
]
]
];

update the user table from request table using laravel 5

I want to update the user table from request table using laravel 5
Please suggest how it can be done with ease. I have two tables data and want to update user table based on request table data where request table has foreign sm_id and user table has primary id please suggest
Thanks in advance
Request data:
Array (
[0] => Array ( [id] => 1 [sm_id] => 1 [field_name] => first_name [value] => G2 [created_at] => 2017-02-27 14:17:35 [updated_at] => 2017-02-24 11:05:03 [deleted_at] => )
[1] => Array ( [id] => 2 [sm_id] => 4 [field_name] => phone [value] => 123467890 [created_at] => 2017-02-27 16:55:48 [updated_at] => 2017-02-27 11:02:27 [deleted_at] => )
[2] => Array ( [id] => 3 [sm_id] => 4 [field_name] => first_name [value] => John [created_at] => 2017-02-27 16:55:48 [updated_at] => 2017-02-27 11:02:27 [deleted_at] => )
[3] => Array ( [id] => 4 [sm_id] => 4 [field_name] => last_name [value] => Hunny [created_at] => 2017-02-27 16:55:48 [updated_at] => 2017-02-27 11:02:27 [deleted_at] => ) )
User Data:
Array
(
[id] => 4
[social_id] =>
[role_id] => 0
[name] =>
[first_name] => max
[last_name] => rony
[gender] => male
[dob] => 02-02-2017
[language] => english
[location] => USA
[address] => ABCD
[email] => max.jrny#jioc.com
[phone] => 7894561230
[id_proof] =>
[id_proof_path] =>
[remember_token] =>
[provider] => website
[biography] => Here is about
[approve] => 1
[created_at] => 2017-02-22 12:16:56
[updated_at] => 2017-02-22 12:16:56
[deleted_at] =>
)
Expected result in user table
Array
(
[id] => 4
[social_id] =>
[role_id] => 0
[name] =>
[first_name] => john
[last_name] => Hunny
[gender] => male
[dob] => 02-02-2017
[language] => english
[location] => USA
[address] => ABCD
[email] => max.jrny#jioc.com
[phone] => 123467890
[id_proof] =>
[id_proof_path] =>
[remember_token] =>
[provider] => website
[biography] => Here is about
[approve] => 1
[created_at] => 2017-02-22 12:16:56
[updated_at] => 2017-02-22 12:16:56
[deleted_at] =>
)
Here is my code
public function verifyInformation($id){
if($id){
$getRequest = request::all()->toArray();
$user = User::where('id', $id)->first()->toArray();
foreach($getRequest as $row=>$value){
foreach($value as $rowKey => $result){
$array_key = array_keys($user);
$arrayDiff = array_diff($value, $user);
$find = $arrayDiff['field_name'];
if(in_array($find, $array_key)){
foreach($array_key as $k => $v){
if($v == $find){
$fieldKey = $v;
$fieldValue = $arrayDiff['value'];
if($fieldValue){
$user = new User;
$user->$fieldKey = $fieldValue;
$user->save();
return redirect()->back()->with("status", "Well done!! profile updated successfully");
}
}
}
}
}
}
}
}
Note: only phone, first_name and last_name updated in expected result
This code will provide an updated array to save and indicate if any values have changed. It is just four declarations to set things up, followed by a foreach loop containing a condition statement which commands the push of data into the array. Modify my snippet to suit your purpose.
Demo
$request_data=array(
0=>array(
"id" => 1,
"sm_id" => 1,
"field_name" => "first_name",
"value" => "G2",
"created_at" => "2017-02-27 14:17:35",
"updated_at" => "2017-02-24 11:05:03",
"deleted_at" => ""),
1=>array(
"id" => 2,
"sm_id" => 4,
"field_name" => "phone",
"value" => 123467890,
"created_at" => "2017-02-27 16:55:48",
"updated_at" => "2017-02-27 11:02:27",
"deleted_at" => ""
),
2=>array(
"id" => 3,
"sm_id" => 4,
"field_name" => "first_name",
"value" => "John",
"created_at" => "2017-02-27 16:55:48",
"updated_at" => "2017-02-27 11:02:27",
"deleted_at" => ""
),
3=>array(
"id" => 4,
"sm_id" => 4,
"field_name" => "last_name",
"value" => "Hunny",
"created_at" => "2017-02-27 16:55:48",
"updated_at" => "2017-02-27 11:02:27",
"deleted_at" => ""
)
);
$user_data=array(
"id" => 4,
"social_id" => "",
"role_id" => 0,
"name" => "",
"first_name" => "max",
"last_name" => "rony",
"gender" => "male",
"dob" => "02-02-2017",
"language" => "english",
"location" => "USA",
"address" => "ABCD",
"email" => "max.jrny#jioc.com",
"phone" => "7894561230",
"id_proof" => "",
"id_proof_path" => "",
"remember_token" => "",
"provider" => "website",
"biography" => "Here is about",
"approve" => 1,
"created_at" => "2017-02-22 12:16:56",
"updated_at" => "2017-02-22 12:16:56",
"deleted_at" => ""
);
$new_data=$user_data;
$user_id=$user_data["id"]; // declare target id
$request_ids=array_column($request_data,"sm_id"); // read sm_id column to array
$request_indexes=array_keys($request_ids,$user_id); // declare matching indexes
// loop an subarrays that contain the appropriate sm_id
foreach(array_intersect_key($request_data,array_flip($request_indexes)) as $row){
// if row contains one of the chosen values, overwrite the original data
if(in_array($row["field_name"],array("phone","first_name","last_name"))){
$new_data[$row["field_name"]]=$row["value"];
}
}
if($user_data!=$new_data){
echo "Changes to save";
}else{
echo "No changes to save";
}
echo "<pre>";
var_export($new_data);
echo "</pre>";
This is the output:
Changes to save
array (
'id' => 4,
'social_id' => '',
'role_id' => 0,
'name' => '',
'first_name' => 'John',
'last_name' => 'Hunny',
'gender' => 'male',
'dob' => '02-02-2017',
'language' => 'english',
'location' => 'USA',
'address' => 'ABCD',
'email' => 'max.jrny#jioc.com',
'phone' => 123467890,
'id_proof' => '',
'id_proof_path' => '',
'remember_token' => '',
'provider' => 'website',
'biography' => 'Here is about',
'approve' => 1,
'created_at' => '2017-02-22 12:16:56',
'updated_at' => '2017-02-22 12:16:56',
'deleted_at' => '',
)

Categories