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' => '',
)
Related
The first array has the same keys as those in the sheet key content in the second array.
I would need to edit the key element of the first array, with the contents that I have in the key table in the second array, according to the keysheet
I tried in countless ways but without success, here is an illustrative image
the first:
Array
(
[0] => Array
(
[CIDADES] => PONTA GROSSA
[PERIODO] => 12:00 - 18:00
[ID] => Z8932
[END] => AV VSC DE MAUA,, ALT
[CONTRATO] => 7947488
[AREA] => AR01
[ASSINANTE] => DAVI
[BAIRRO] => OFICINAS
[OBS] => [Agendamento par]
[TEL RES] => 42410435
[TEL COM] =>
)
[1] => Array
(
[CIDADES] => PONTA GROSSA
[PERIODO] => 08:00 - 12:00
[ID] => Z7526
[END] => R P1000, FD
[CONTRATO] => 799644
[AREA] => AR01
[ASSINANTE] => BEATRIZ
[BAIRRO] => NEVES
[OBS] => [Agendamento]
[TEL RES] => 42988674761
[TEL COM] =>
)
[2] => Array
(
[CIDADES] => PONTA GROSSA
[PERIODO] => 08:00 - 12:00
[ID] => Z0979
[END] => R J93, FD
[CONTRATO] => 79490
[AREA] => AR01
[ASSINANTE] =>FERNANDES REIS
[BAIRRO] => UVARANAS
[OBS] => [Agendamentoente]
[TEL RES] => 4289986
[TEL COM] =>
)
)
And the second
Array
(
[0] => Array
(
[id] => 1
[table] => id_operacao
[sheet] => CIDADES
)
[1] => Array
(
[id] => 2
[table] => horario
[sheet] => PERIODO
)
[2] => Array
(
[id] => 3
[table] => tipo_wo
[sheet] => TIPO DE SERVICO
)
[3] => Array
(
[id] => 4
[table] => tecnico_id
[sheet] => ID
)
[4] => Array
(
[id] => 5
[table] => contrato
[sheet] => CONTRATO
)
[5] => Array
(
[id] => 6
[table] => roteamento
[sheet] => AREA
)
[6] => Array
(
[id] => 7
[table] => endereco
[sheet] => END
)
[7] => Array
(
[id] => 8
[table] => nome_cliente
[sheet] => ASSINANTE
)
[8] => Array
(
[id] => 9
[table] => bairro
[sheet] => BAIRRO
)
[9] => Array
(
[id] => 10
[table] => obs1
[sheet] => OBS
)
[10] => Array
(
[id] => 11
[table] => telefone
[sheet] => TEL RES
)
[11] => Array
(
[id] => 12
[table] => celular
[sheet] => TEL COM
)
)
Hey man I skipped a few items in the array but hopefully I got the structure right ;)
$firstArray = [
[
'CIDADES' => 'PONTA GROSSA',
'PERIODO' => '12:00 - 18:00',
'ID' => 'Z8932',
'END' => 'AV VSC DE MAUA,, ALT',
'CONTRATO' => '7947488',
'AREA' => 'AR01',
'ASSINANTE' => 'DAVI',
'BAIRRO' => 'OFICINAS',
'OBS' => ['Agendamento par'],
'TEL RES' => '42410435',
],
[
'CIDADES' => 'PONTA GROSSA',
'PERIODO' => '08:00 - 12:00',
'ID' => 'Z7526',
'END' => 'R P1000, FD',
'CONTRATO' => '799644',
'AREA' => 'AR01',
'ASSINANTE' => 'DAVI',
'BAIRRO' => 'BEATRIZ',
'OBS' => ['Agendamento'],
'TEL RES' => '42988674761',
],
[
'CIDADES' => 'PONTA GROSSA',
'PERIODO' => '08:00 - 12:00',
'ID' => 'Z0979',
'END' => 'R J93, FD',
'CONTRATO' => '79490',
'AREA' => 'AR01',
'ASSINANTE' => 'FERNANDES REIS',
'BAIRRO' => 'UVARANAS',
'OBS' => ['Agendamentoente'],
'TEL RES' => '4289986',
],
];
$secondArray = [
[
'id' => 1,
'table' => 'id_operacao',
'sheet' => 'CIDADES',
],
[
'id' => 2,
'table' => 'horario',
'sheet' => 'PERIODO',
],
[
'id' => 3,
'table' => 'tipo_wo',
'sheet' => 'TIPO DE SERVICO',
],
[
'id' => 4,
'table' => 'tecnico_id',
'sheet' => 'ID',
],
[
'id' => 5,
'table' => 'contrato',
'sheet' => 'CONTRATO',
],
[
'id' => 6,
'table' => 'roteamento',
'sheet' => 'AREA',
],
[
'id' => 7,
'table' => 'endereco',
'sheet' => 'END',
],
[
'id' => 8,
'table' => 'nome_cliente',
'sheet' => 'ASSINANTE',
],
];
//I guess this is what you were missing most
$oldKeysToNewKeys = array_combine(
array_column($secondArray, 'sheet'),
array_column($secondArray, 'table')
);
foreach ($firstArray as $key => $firstArrayElements) {
$newFirstArrayElement = [];
foreach ($firstArrayElements as $oldKey => $value) {
//You could add checks to avoid errors when keys are not set etc..
$newKey = $oldKeysToNewKeys[$oldKey];
$newFirstArrayElement[$newKey] = $value;
}
$firstArray[$key] = $newFirstArrayElement;
}
//There you go!
var_dump($firstArray);
I call an api and the response i get is
Array (
[meta] => Array (
[code] => 200
[type] => Success
[message] => Success
)
[data] => Array (
[items] => Array (
[0] => Array (
[id] => b4a235596fd9550dfb69f181f4db007f
[tracking_number] => 2649884668232181
[carrier_code] => hermes
[order_create_time] =>
[destination_code] =>
[status] => delivered
[track_update] =>
[original_country] =>
[itemTimeLength] => 7
[stayTimeLength] => 74
[service_code] =>
[packageStatus] =>
[substatus] =>
[last_mile_tracking_supported] =>
[origin_info] => Array (
[ItemReceived] => 2019-04-09 09:29
[ItemDispatched] =>
[DepartfromAirport] =>
[ArrivalfromAbroad] =>
[CustomsClearance] =>
[DestinationArrived] =>
[weblink] => https://www.hermesworld.com/en/
[phone] =>
[carrier_code] => hermes
[trackinfo] => Array (
[0] => Array (
[Date] => 2019-04-15 11:51
[StatusDescription] => Posted through letterbox
[Details] =>
[checkpoint_status] => delivered
)
[1] => Array (
[Date] => 2019-04-15 09:45
[StatusDescription] => Delivery will be attempted between 10:00 and 14:00 today
[Details] =>
[checkpoint_status] => transit
)
[2] => Array (
[Date] => 2019-04-15 06:39
[StatusDescription] => On its way to the courier
[Details] =>
[checkpoint_status] => transit
)
[3] => Array (
[Date] => 2019-04-14 22:33
[StatusDescription] => At the customers local depot
[Details] =>
[checkpoint_status] => transit
)
.....
)
)
[destination_info] =>
[lastEvent] => Posted through letterbox,2019-04-15 11:51
[lastUpdateTime] => 2019-04-15 11:51
)
)
)
) 1
I would like to access the value of checkpoint_status but im not getting there using the following ways:
response in variable
$track = print_r($track);
1. $track['checkpoint_status']
2. $track[0] // just to see if it returns anything, but no result
3. $track[1] // just to see if it returns anything, but no result
4. array_column($track, 'checkpoint_status'); // returns nothing
You need to loop, to get all checkpoint_status
foreach($array['data']['items'] as $item){
foreach($item['origin_info']['trackinfo'] as $trackinfo){
echo $trackinfo['checkpoint_status'].PHP_EOL;
}
}
Sample output:-https://3v4l.org/IM7I1
You can use array_walk_recursive
$r = [];
array_walk_recursive($a, function($v, $k) use(&$r){
($k == 'checkpoint_status') ? ($r[]=$v) : '';
});
https://3v4l.org/rCVEB
$response = [
'meta' => [
'code' => 200,
'type' => 'Success',
'message' => 'Success',
],
'data' => [
'items' => [
'0' => [
'id' => 'b4a235596fd9550dfb69f181f4db007f',
'tracking_number' => '2649884668232181',
'carrier_code' => 'hermes',
'order_create_time' => null,
'destination_code' => null,
'status' => 'delivered',
'track_update' => null,
'original_country' => null,
'itemTimeLength' => 7,
'stayTimeLength' => 74,
'service_code' => null,
'packageStatus' => null,
'substatus' => null,
'last_mile_tracking_supported' => null,
'origin_info' => [
'ItemReceived' => '2019-04-09 09:29',
'ItemDispatched' => null,
'DepartfromAirport' => null,
'ArrivalfromAbroad' => null,
'CustomsClearance' => null,
'DestinationArrived' => null,
'weblink' => 'https://www.hermesworld.com/en/',
'phone' => null,
'carrier_code' => 'hermes',
'trackinfo' => [
'0' => [
'Date' => '2019-04-15 11:51',
'StatusDescription' => 'Posted through letterbox',
'Details' =>null,
'checkpoint_status' => 'delivered',
],
'1' => [
'Date' => '2019-04-15 09:45',
'StatusDescription' => 'Delivery will be attempted between 10:00 and 14:00 today',
'Details' => null,
'checkpoint_status' => 'transit',
],
'2' => [
'Date' => '2019-04-15 06:39',
'StatusDescription' => 'On its way to the courier',
'Details' =>null,
'checkpoint_status' => 'transit',
],
'3' => [
'Date' => '2019-04-14 22:33',
'StatusDescription' => 'At the customers local depot',
'Details' => null,
'checkpoint_status' => 'transit',
],
]
],
'destination_info' => null,
'lastEvent' => 'Posted through letterbox,2019-04-15 11:51',
'lastUpdateTime' => '2019-04-15 11:51',
]
]
]
];
If you are working on development mode you could turn on display errors on php and may get this result:
echo $response['checkpoint_status']; // won't work as response array has no checkpoint_status keyed array
echo $response[0]; // won't work as response array has no 0 keyed array
echo $response[1]; // won't work as response array has no 1 keyed array
print_r(array_column($response, 'checkpoint_status')); // won't work as response array has no column checkpoint_status
There are many ways in which you could get your result one of which is given below:
$trackinfo = $response['data']['items'][0]['origin_info']['trackinfo'];
$checkpoint_status = array_column($trackinfo, 'checkpoint_status');
print_r($checkpoint_status);
Result:
Array (
[0] => delivered
[1] => transit
[2] => transit
[3] => transit
)
Problem:
I dont know/understand how to check if date and place exists on the same "row" and they exists more then once.
Second, how do i then merge an array
my case MergeArray with ArraySchedule
Code:
$ArraySchedule = array();
while ($data = $stmt -> fetch(PDO::FETCH_ASSOC)) {
$schedules = array(
"id" => $data['id'],
"name" => $data['name'],
"date" => $data['date'],
"time" => $data['time'],
"place_id" => $data['place_id'],
"place" => $data['place'],
);
array_push($ArraySchedule, $schedules);
}
$dupe_array = array();
foreach ($ArraySchedule as $key => $value) {
if(++$dupe_array[$value["date"]] > 1 && ++$dupe_array[$value["place_id"]] > 1 ){
// this statement is wrong, i want something like:
// if date and place_id exists on the same "row" and they exists more then once
}
}
What i want to do:
Check if ArraySchedule contains schedules that have the same date and place,
if there is more than one schedule that has the same date and place_id.
then I want to update ArraySchedule with this structure
$MergeArray = array(
"id" => ArraySchedule['id'],
"name" => array(
"name" => scheduleSameDateAndPlace['name'],
"name" => scheduleSameDateAndPlace['name'],
"name" => scheduleSameDateAndPlace['name'],
),
"date" => $ArraySchedule['date'],
"time" => $ArraySchedule['time'],
"place_id" => $ArraySchedule['place_id'],
"place_name" => $ArraySchedule['place_name'],
),
MergeArray with ArraySchedule?
anyway...
Output I think I want?
Print_r($ArraySchedule)
array(
[0] =>
array(
[id] => 1
[names] => Simon
[date] => 2019-01-02
[time] 18.00
[place_id] => Tystberga Park
[place] => Tystberga
)
[1] =>
array(
[id] => 2
//[names] insted of [name]?
[names] =>
array(
[name] => Vincent
[name] => Angel
[name] => Kim
)
[date] => 2019-02-17
[time] => 13.00
[place_id] => Borås Park
[place] => Borås
)
[2] =>
array(
[id] => 3
// [names] is always an array?
[names] => Caitlyn
[date] => 2019-03-15
[time] 13.00
[place_id] => Plaza Park
[place] => EvPark
)
)
You can use array-reduce. Consider the following:
function mergeByDateAndPlace($carry, $item) {
$key = $item["place_id"] . $item["date"]; // creating key matching exact place and date
if (!isset($carry[$key])) {
$carry[$key]["name"] = $item["name"];
} else {
$carry[$key] = $item;
$item["name"] = [$item["name"]]; // make default array with 1 element so later can be append other names
}
return $carry;
}
Now use it with:
$MergeArray = array_reduce($ArraySchedule, "mergeByDateAndPlace", []);
If you later want to know if there were any duplicate you can just loop on $MergeArray. You can also use array_values if you want to discard the concat keys.
Notice #Nick 2 important comment about saving the first loop and the "time" value that need to be decided. Also notice your desire output contain multi element with the same key ("name") - you need to append them with int key - Array can not have duplicate keys.
Hope that helps!
Here is my data from my database:
var_export($ArraySchedule)
array (
0 => array ( 'id' => '225', 'place_id' => 'Alviks Kulturhus', 'name' => 'BarraBazz', 'date' => '2019-03-19', 'placeadress' => 'Gustavslundsvägen 1', ),
1 => array ( 'id' => '229', 'place_id' => 'Axelhuset Göteborg', 'name' => 'Anders Björk', 'date' => '2019-04-08', 'placeadress' => 'Axel Dahlströms torg 3', ),
2 => array ( 'id' => '230', 'place_id' => 'Axelhuset Göteborg', 'name' => 'Black Jack', 'date' => '2019-04-08', 'placeadress' => 'Axel Dahlströms torg 3', ),
3 => array ( 'id' => '227', 'place_id' => 'Arosdansen Syrianska Kulturcentret', 'name' => 'BarraBazz', 'date' => '2019-05-08', 'placeadress' => 'Narvavägen 90', ),
4 => array ( 'id' => '228', 'place_id' => 'Aspåsnäset', 'name' => 'Blender', 'date' => '2019-05-25', 'placeadress' => 'Aspåsnäset 167', ),
5 => array ( 'id' => '226', 'place_id' => 'Arenan Västervik Resort', 'name' => 'Blender', 'date' => '2019-06-29', 'placeadress' => 'Lysingsvägen', ),
6 => array ( 'id' => '222', 'place_id' => 'Alingsåsparken', 'name' => 'Bendéns', 'date' => '2019-07-16', 'placeadress' => 'Folkparksgatan 3A', ),
7 => array ( 'id' => '223', 'place_id' => 'Alingsåsparken', 'name' => 'Charlies', 'date' => '2019-07-16', 'placeadress' => 'Folkparksgatan 3A', ),
8 => array ( 'id' => '224', 'place_id' => 'Allhuset Södertälje', 'name' => 'Cedrix', 'date' => '2019-07-16', 'placeadress' => 'Barrtorpsvägen 1A', ), )
I want to update the "name" with an array of names everytime that place_id and date are the same.
This is the output I want:
Array (
[0] =>
Array ( [id] => 225 [place_id] => Alviks Kulturhus [name] => BarraBazz [date] => 2019-03-19 [placeadress] => Gustavslundsvägen 1 )
[1] =>
Array ( [id] => 229 [place_id] => Axelhuset Göteborg [name] => Array([0] => Anders Björk [1] => Black Jack ) [date] => 2019-04-08 [placeadress] => Axel Dahlströms torg 3 )
[3] =>
Array ( [id] => 227 [place_id] => Arosdansen Syrianska Kulturcentret [name] => BarraBazz [date] => 2019-05-08 [placeadress] => Narvavägen 90 )
[4] =>
Array ( [id] => 228 [place_id] => Aspåsnäset [name] => Blender [date] => 2019-05-25 [placeadress] => Aspåsnäset 167 )
[5] =>
Array ( [id] => 226 [place_id] => Arenan Västervik Resort [name] => Blender [date] => 2019-06-29 [placeadress] => Lysingsvägen )
[6] =>
Array ( [id] => 222 [place_id] => [Alingsåsparken] [name] => Array([0] => Bendéns [1] => Charlies) [date] => 2019-07-16 [placeadress] => Folkparksgatan 3A )
[8] =>
Array ( [id] => 224 [place_id] => Allhuset Södertälje [name] => Cedrix [date] => 2019-07-16 [placeadress] => Barrtorpsvägen 1A ) )
Here is my updated code
$sql = "SELECT `schedule`.`id`,`schedule`.`place_id`,`schedule`.`name`,`schedule`.`date`,`places`.`placeadress` FROM `schedule` INNER JOIN `places` ON `schedule`.`place_id`=`places`.`place_id` ORDER BY `date`";
$stmt = $db -> prepare($sql);
$stmt -> execute();
$ArraySchedule = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_export($ArraySchedule);
$DatePlace = array();
foreach ($ArraySchedule as $key => $Schedule){
$Arrayquery = "SELECT `schedule`.`id`,`schedule`.`place_id`,`schedule`.`name`,`schedule`.`date`,`places`.`placeadress` FROM `schedule` INNER JOIN `places` ON `schedule`.`place_id`=`places`.`place_id` WHERE `schedule`.`date`= :date_ AND `schedule`.`place_id` = :place_id ORDER BY `date`";
$ArrayStmt = $db->prepare($Arrayquery);
$ArrayStmt -> execute(array(":date_" => $Schedule['date'],":place_id" => $Schedule['place_id']));
//Getting every $Schedule that has the same date and place_id
if($ArrayStmt->rowCount() > 1){
//Here i want two update the name inside
//$ArrayArraySchedule with an array of names
//that has the same place and date?
}
}
print_r($ArraySchedule);
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
)
I'm facing a little problem and I don't know is it's possible to achieve it or not. Let me explain :
I have an associative array like this :
Array
(
[res_id] => 104
[subject] => Test
[type_id] => 503
[format] => pdf
[typist] => fefe
[creation_date] => 2017-02-10 14:27:37.236711
[modification_date] => 2017-02-10 14:27:37.236711
[fulltext_result] => 1
[doc_date] => 2017-02-01 00:00:00
[docserver_id] => FASTHD_MAN
[path] => 2017#02#0001##
[filename] => 0008.pdf
[fingerprint] =>
[filesize] => 84979
[status] => VAL
[destination] => DSG
[priority] => 2
[is_multi_docservers] => N
[is_frozen] => N
[tablename] => res_letterbox
[initiator] => COU
[dest_user] => ddaull
[locker_user_id] => fefefef
[locker_time] => 2017-02-13 15:52:25.624521
[confidentiality] => N
[tnl_path] => 2017#02#0001##
[tnl_filename] => 0008.png
)
I want to know if I can use this associative array, in order to make an INSERT TO request ? I want the first part of the array (like res_id, subject) goes to column part for the insertion. The second part of the array (like 104,Test) will go to the values
Thanks in advance for your help, hope I am clear enough..
#Nathan30 try this :
<?php
$arr = array(
"res_id" => 104,
"subject" => "Test",
"type_id" => 503,
"format" => "pdf",
"typist" => "fefe",
"creation_date" => "2017-02-10 14:27:37.236711",
"modification_date" => "2017-02-10 14:27:37.236711",
"fulltext_result" => 1,
"doc_date" => "2017-02-01 00:00:00",
"docserver_id" => "FASTHD_MAN",
"path" => "2017#02#0001##",
"filename" =>" 0008.pdf",
"fingerprint" => "",
"filesize" => 84979,
"status" => "VAL",
"destination" => "DSG",
"priority" => 2,
"is_multi_docservers" => "N",
"is_frozen" => "N",
"tablename" => "res_letterbox",
"initiator" => "COU",
"dest_user" => "ddaull",
"locker_user_id" => "fefefef",
"locker_time" => "2017-02-13 15:52:25.624521",
"confidentiality" => "N",
"tnl_path" => "2017#02#0001##",
"tnl_filename" => "0008.png",
);
echo "<pre>";
print_r($arr);
$column = array();
$values = array();
foreach($arr as $key => $value){
$column[] = $key;
$values[] = $value;
}
now query will be like:
"insert into table values(".implode(',', $column).") values (".implode(',', $values).")";