Add values to array in the same key - php

I've two arrays
118 => array:7 [
"date" => "19.10.2016"
"time1" => "dfg"
"purpose1" => "dfg"
"chair1" => "dfg"
"time2" => "dfg"
"purpose2" => "dfg5"
"chair2" => "5345"
]
123 => array:7 [
"date" => "20.10.2016"
"time1" => "gdf"
"purpose1" => "gdfg"
"chair1" => "gdf"
"time2" => "gdfg"
"purpose2" => "gdf"
"chair2" => "534534"
]
124 => array:7 [
"date" => "20.10.2016"
"time1" => "gdf"
"purpose1" => "gdfg"
"chair1" => "gdf"
"time2" => "gdfg"
"purpose2" => "gdf"
"chair2" => "534534"
]
and
0 => {#231
+"label_id": "D101102E"
+"id": 118
}
1 => {#232
+"label_id": "D1011100"
+"id": 123
}
2 => {#233
+"label_id": "D1011100"
+"id": 124
}
Where id is key in first array, and value in second array. I want add label_id to first array as value in the same id as key. I already try use array_fill_keys and array_push but it's not the point. Thank you

Iterate through the first array, then in a nested loop, iterate through the second array comparing the outer key to the inner id. If you have a match, then append the value to the outer array and continue to the next item.
<?php
$a = [
118 => [
"date" => "19.10.2016",
"time1" => "dfg",
"purpose1" => "dfg",
"chair1" => "dfg",
"time2" => "dfg",
"purpose2" => "dfg5",
"chair2" => "5345",
],
123 => [
"date" => "20.10.2016",
"time1" => "gdf",
"purpose1" => "gdfg",
"chair1" => "gdf",
"time2" => "gdfg",
"purpose2" => "gdf",
"chair2" => "534534",
],
124 => [
"date" => "20.10.2016",
"time1" => "gdf",
"purpose1" => "gdfg",
"chair1" => "gdf",
"time2" => "gdfg",
"purpose2" => "gdf",
"chair2" => "534534",
],
];
$b = [
0 => [
"label_id" => "D101102E",
"id" => 118,
],
1 => [
"label_id" => "D1011100",
"id" => 123,
],
2 => [
"label_id" => "D1011100",
"id" => 124,
],
];
foreach($a as $key => $value){
foreach($b as $k => $v){
if($key === $v['id']){
$a[$key]['label_id'] = $k;
continue;
}
}
}
print_r($a);
Array
(
[118] => Array
(
[date] => 19.10.2016
[time1] => dfg
[purpose1] => dfg
[chair1] => dfg
[time2] => dfg
[purpose2] => dfg5
[chair2] => 5345
[label_id] => 0
)
[123] => Array
(
[date] => 20.10.2016
[time1] => gdf
[purpose1] => gdfg
[chair1] => gdf
[time2] => gdfg
[purpose2] => gdf
[chair2] => 534534
[label_id] => 1
)
[124] => Array
(
[date] => 20.10.2016
[time1] => gdf
[purpose1] => gdfg
[chair1] => gdf
[time2] => gdfg
[purpose2] => gdf
[chair2] => 534534
[label_id] => 2
)
)

Related

How to create new row instead of using the same one?

I have the following array:
$original_array = [
[
"group" => "g1",
"department" => "d1",
"team" => null,
"data_col1" => "some_data1",
"data_col2" => "some_data2"
],
[
"group" => "g1",
"department" => "d1",
"team" => null,
"data_col1" => "some_data3",
"data_col2" => "some_data4"
],
[
"group" => "g1",
"department" => "d1",
"team" => "t3",
"data_col1" => "some_data5",
"data_col2" => "some_data6"
],
[
"group" => "g4",
"department" => "d6",
"team" => "t11",
"data_col1" => "some_data7",
"data_col2" => "some_data8"
]
];
I want to "group" the array into common group/department/team.
I tried the following - I created a new array, then I assign the relevant keys (group/department/team) and otherwise I assign the value:
$new_array = [];
foreach ($original_array as $row) {
foreach ($row as $key => $value) {
if ($key === "group" || $key === "department" || $key === "team") {
$new_array[$key] = $value;
} else {
$new_array[] = $value;
}
}
}
The expected result is:
[
0 => [
group => "g1",
department => "d1",
team => null,
data => [
0 => [data_col1 => "some_data1", data_col2 => "some_data2"],
1 => [data_col1 => "some_data3", data_col2 => "some_data4"]
],
1 => [
group => "g1",
department => "d1",
team => "t3",
data => [
0 => [data_col1 => "some_data5", data_col2 => "some_data6"]
],
2 => [
group => "g4",
department => "d6",
team => "t11",
data => [
0 => [data_col1 => "some_data7", data_col2 => "some_data8"]
]
]
But the result is:
[
"group" => "g4"
"department" => "d6"
"team" => "t11"
0 => "some_data1"
1 => "some_data2"
2 => "some_data3"
3 => "some_data4"
4 => "some_data5"
5 => "some_data6"
6 => "some_data7"
7 => "some_data8"
]
why? Looks like it only assigns values to the last row in the original array
This code should give the good result :
foreach ($original_array as $row) {
$key = md5($row['group'] . $row['department'] . $row['team']);
if(!isset($new_array[$key])){
$new_array[$key] = [
'group' => $row['group'],
'department' => $row['department'],
'team' => $row['team'],
'data' => [],
];
}
$data = [];
foreach ($row as $k => $v) {
if ($k !== "group" AND $k !== "department" AND $k !== "team") {
$data[$k] = $v;
}
}
$new_array[$key]['data'][] = $data;
}
This give me that :
Array
(
[336a26e12ea082ff360f8482891e137c] => Array
(
[group] => g1
[department] => d1
[team] =>
[data] => Array
(
[0] => Array
(
[data_col1] => some_data1
[data_col2] => some_data2
)
[1] => Array
(
[data_col1] => some_data3
[data_col2] => some_data4
)
)
)
[f06623fdd946c84a0c0c30378ba3f6f5] => Array
(
[group] => g1
[department] => d1
[team] => t3
[data] => Array
(
[0] => Array
(
[data_col1] => some_data5
[data_col2] => some_data6
)
)
)
[0b284dc4a7dac108ec2b1205a6484056] => Array
(
[group] => g4
[department] => d6
[team] => t11
[data] => Array
(
[0] => Array
(
[data_col1] => some_data7
[data_col2] => some_data8
)
)
)
)

PHP Mutli-Dimensional Array Grabbing Item

I have an API which I'm trying to get an ID from, but the output is a mutli-dimensional array and I can't grab just the ID.
This is what the array looks like:
Array ( [{ data] => [ { account_key [ is_owner] => true [ id] => 1 [ name] => test [ display_name] => test [ balance] => 0 [ paid_to_date] => 0 [ updated_at] => 1577724986 [ archived_at] => null [ address1] => [ address2] => [ city] => [ state] => [ postal_code] => [ country_id] => 0 [ work_phone] => [ private_notes] => [ public_notes] => [ last_login] => [ website] => [ industry_id] => 0 [ size_id] => 0 [ is_deleted] => false [ 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] => true [ send_reminders] => true [ credit_number_counter] => 1 [ custom_messages] => {} [ contacts] => [ { account_key [ is_owner] => true [ id] => 1 [ first_name] => test [ last_name] => test [ email] => me#idontlikespam.com [ contact_key] => mq1dzpkqznfgtqwhdwt9nte1ohmvsju1 [ updated_at] => 1577724986 [ archived_at] => null [ is_primary] => true [ phone] => [ last_login] => [ send_invoice] => true [ custom_value1] => [ custom_value2] => } ] } ] [ meta] => { pagination [ count] => 1 [ per_page] => 15 [ current_page] => 1 [ total_pages] => 1 [ links] => [] } } } )
This is the code I have so far to split it out:
$clients = str_replace('"', "", $clients);
$convert_to_array = explode(',', $clients);
for($i=0; $i < count($convert_to_array ); $i++){
$key_value = explode(':', $convert_to_array [$i]);
$end_array[$key_value [0]] = $key_value [1];
}
print_r($end_array);
foreach ($end_array as $key => $item)
{
echo "[" . $key . "] => " . $item . "<br />";
if ($key = ' id')
{
echo $item;
}
}
It's just the final step I'm missing, it's probably something easy, but I'm still running on a Christmas brain...
New data:
stdClass Object ( [data] => Array ( [0] => stdClass Object ( [account_key] => jvg7qgtw2btrlmpigrq2zpco48eegxvv [is_owner] => 1 [id] => 1 [name] => test [display_name] => test [balance] => 0 [paid_to_date] => 0 [updated_at] => 1577724986 [archived_at] => [address1] => [address2] => [city] => [state] => [postal_code] => [country_id] => 0 [work_phone] => [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] => ema#il.co.uk [contact_key] => mq1dzpkqznfgtqwhdwt9nte1ohmvsju1 [updated_at] => 1577724986 [archived_at] => [is_primary] => 1 [phone] => [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 ( ) ) ) )
In Array you can access to values by [index].
In stdObject you can access to properties by -> because it is object.
So
stdClass Object ( [data] => Array ( [0] => stdClass Object ( [account_key]
Is
Object->(property data has value array)[index 0 in array and is object]->(property account_key)
On your data you need this.
foreach ($end_array->data as $key => $item)
{
echo $item->id . "<br />";
}
or if you know if data can have always one item you can access with this shortcut
echo $end_array->data[0]->id;

Comparing two associative arrays values and replacing them

I'm facing issue while dealing with 2 associative arrays. I have two arrays, If id of Array 1 = id of Array 2 = id, then active_lession and active_learning_lession of Array 1 should be replaced by active_lession and active_learning_lession of Array 2 respectively.
Array 1 =>
array:3 [
0 => array:10 [
"id" => 3
"status" => "1"
"active_lession" => 0
"active_learning_lession" => 0
"learninglessions" => array:2 [
0 => array:12 [
"id" => 2
"language_id" => 1
"category_id" => 3
"sentence" => "अगर आपको अपना स्कूल का नाम पुछा जाए तो क्या कहेंगे"
"english_sentence" => "I am student of …… School."
]
1 => array:12 [
"id" => 27
"language_id" => 1
"category_id" => 3
"sentence" => "यह मेरा दोस्त/मित्र है"
"english_sentence" => "He is my friend."
]
]
]
1 => array:10 [
"id" => 4
"name" => "Module 2"
"image" => "public/icon/downloadxxx.jpeg"
"status" => "1"
"active_lession" => 10
"active_learning_lession" => 0
"learninglessions" => array:2 [
0 => array:12 [
"id" => 1
"language_id" => 1
"category_id" => 4
"sentence" => "अपना परिचय कैसे देंगे –"
"english_sentence" => "I am..."
]
]
]
2 => array:10 [
"id" => 5
"status" => "1"
"active_lession" => 0
"active_learning_lession" => 0
"learninglessions" => array:4 [
0 => array:12 [
"id" => 29
"language_id" => 1
"category_id" => 5
"sentence" => "यह एक बाग है ।"
"english_sentence" => "This is a Park."
]
1 => array:12 [
"id" => 34
"language_id" => 1
"category_id" => 5
"sentence" => "कैसा चल रहा है ?"
"english_sentence" => "How are things ?"
]
]
]
Array 2=>
array:3 [
0 => array:3 [
"id" => 3
"active_learning_lession" => 25
"active_lession" => 20
]
1 => array:3 [
"id" => 4
"active_learning_lession" => 20
"active_lession" => 15
]
]
Thus Expected Array will be
array:3 [
0 => array:10 [
"id" => 3
"status" => "1"
"active_lession" => 20
"active_learning_lession" => 25
"learninglessions" => array:2 [
0 => array:12 [
"id" => 2
"language_id" => 1
"category_id" => 3
"sentence" => "अगर आपको अपना स्कूल का नाम पुछा जाए तो क्या कहेंगे"
"english_sentence" => "I am student of …… School."
]
1 => array:12 [
"id" => 27
"language_id" => 1
"category_id" => 3
"sentence" => "यह मेरा दोस्त/मित्र है"
"english_sentence" => "He is my friend."
]
]
]
1 => array:10 [
"id" => 4
"name" => "Module 2"
"image" => "public/icon/downloadxxx.jpeg"
"status" => "1"
"active_lession" => 15
"active_learning_lession" => 20
"learninglessions" => array:2 [
0 => array:12 [
"id" => 1
"language_id" => 1
"category_id" => 4
"sentence" => "अपना परिचय कैसे देंगे –"
"english_sentence" => "I am..."
]
]
]
2 => array:10 [
"id" => 5
"status" => "1"
"active_lession" => 0
"active_learning_lession" => 0
"learninglessions" => array:4 [
0 => array:12 [
"id" => 29
"language_id" => 1
"category_id" => 5
"sentence" => "यह एक बाग है ।"
"english_sentence" => "This is a Park."
]
1 => array:12 [
"id" => 34
"language_id" => 1
"category_id" => 5
"sentence" => "कैसा चल रहा है ?"
"english_sentence" => "How are things ?"
]
]
]
Try this code..
$res = [];
foreach($x as $key => $xx)
{
foreach($y as $k => $yy)
{
if($xx['id'] == $yy['id'])
{
$res[$key] = $xx;
$res[$key]['active_lession'] = $yy['active_lession'];
$res[$key]['active_learning_lession'] = $yy['active_learning_lession'];
}
else
{
if(!array_key_exists($key,$res))
{
$res[$key] = $xx;
}
}
}
}
print_r($res);
OR
foreach($x as $key => $value)
{
foreach($y as $yy)
{
if($value['id'] == $yy['id'])
{
$x[$key]['active_learning_lession'] = $yy['active_learning_lession'];
$x[$key]['active_lession'] = $yy['active_lession'];
}
}
}
print_r($x);
Output will be
Array
(
[0] => Array
(
[id] => 3
[status] => 1
[active_lession] => 20
[active_learning_lession] => 25
[learninglessions] => Array
(
[0] => Array
(
[id] => 2
[language_id] => 1
[category_id] => 3
[sentence] => jhdbfhbs
[english_sentence] => I am student of …… School.
)
[1] => Array
(
[id] => 27
[language_id] => 1
[category_id] => 3
[sentence] => dbshbfjhf
[english_sentence] => He is my friend.
)
)
)
[1] => Array
(
[id] => 4
[name] => Module 2
[image] => public/icon/downloadxxx.jpeg
[status] => 1
[active_lession] => 15
[active_learning_lession] => 20
[learninglessions] => Array
(
[0] => Array
(
[id] => 1
[language_id] => 1
[category_id] => 4
[sentence] => jhbdsfhjferu
[english_sentence] => I am...
)
)
)
[2] => Array
(
[id] => 5
[status] => 1
[active_lession] => 0
[active_learning_lession] => 0
[learninglessions] => Array
(
[0] => Array
(
[id] => 29
[language_id] => 1
[category_id] => 5
[sentence] => jbfhgbdu
[english_sentence] => This is a Park.
)
[1] => Array
(
[id] => 34
[language_id] => 1
[category_id] => 5
[sentence] => jhsbdhjfbuyefr
[english_sentence] => How are things ?
)
)
)
)

Php Array_filter to unset key values from array of arrays

I want to unset array elements from my nested array of arrays.
I want to perform recursive array filter and i am not sure about how to do that efficiently.
Below is the original array.
[
[a] => 3
[b] => 0
[c] => [
[
[1] => aa
[2] => 1
[3] => [
[
[a1] => 6
[a2] => 5781
[a3] =>
[
[1] => 0
[2] => 19550
[3] => 5781
]
]
[
[a1] => 1
[a2] => 5781
[a3] =>[
[1] => 0
[2] => 19550
[3] => 5781
]
]
]
]
[
[1] => aa
[2] => 1
[3] => [
[
[a1] => 6
[a2] => 5781
[a3] =>
[
[1] => 0
[2] => 19550
[3] => 5781
]
]
[
[a1] => 1
[a2] => 5781
[a3] =>[
[1] => 0
[2] => 19550
[3] => 5781
]
]
]
]
]
]
Expected array
[
[a] => 3
[c] => [
[
[1] => aa
[3] => [
[
[a1] => 6
[a3] =>
[
[2] => 19550
[3] => 5781
]
]
[
[a1] => 1
[a3] =>[
[2] => 19550
[3] => 5781
]
]
]
]
[
[1] => aa
[3] => [
[
[a1] => 6
[a3] =>
[
[2] => 19550
[3] => 5781
]
]
[
[a1] => 1
[a3] =>[
[2] => 19550
[3] => 5781
]
]
]
]
]
]
As you can see in my expected array from every nested arrays some key value pairs have been removed.
I need to filter the array based on key name.
That is my key names are fixed and that needs to be removed from every children arrays inside.
Any helps would be appreciated.
Here is an example of a recursive foreach based solution to your code working off the data set you provided.
$sourceArray = array("a" => 3, "b" => 0, "c" => array("1" => "aa", "2" => 1, "3" => array("a1" => 6, "a2" => 5781, "a3" => array("1" => 0, "2" => 19550, "3" => 5781)), array( "a1" => 1, "a2" => 5781, "a3" =>array("1" => 0, "2" => 19550, "3" => 5781 ))), array( "1" => "aa", "2" => 1, "3" => array( array( "a1" => 6, "a2" => 5781, "a3" => array( "1" => 0, "2" => 19550,"3" => 5781))), array( "a1" => 1, "a2" => 5781, "a3" =>array( "1" => 0, "2" => 19550, "3" => 5781))));
print_r($sourceArray,1);
function removeKeys($keys, $sourceData) {
foreach ($sourceData as $key=>$val) {
if (in_array($key, $keys, true)) {
unset($sourceData[$key]);
} else if (is_array($val)) {
$sourceData[$key] = removeKeys($keys, $sourceData[$key]);
}
}
return $sourceData;
}
$keysToRemove = array("b","2","a2");
$newArray = removeKeys($keysToRemove, $sourceArray);
print_r($newArray);
Simple to implement though getting your data in was a bit of a challenge. I did notice a "bug" in this example in that if the key is "0" in the original array it still gets deleted even if it's not in the $keys array.
But I'm assuming that this example is sufficient to answer your question and that my stated edge case will not occur (ie, "0" is not a key value in your array.) If you do use "0" as a key you can add additional logic to trap that case but it would slow the function down a bit so I'll leave that choice up to you.
(Note, the bug referred to above is fixed now and in the code... see notes below for solution from original poster)

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