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>';
}
}
<?php
$ar = Array (
[0] => Array ( [id] => 1 [social_id] => [role_id] => 0 [name] => [first_name] => Jimmy [last_name] => rathore [gender] => male [dob] => 06-02-1992 [language] => english [location] => xyz [address] => xyz [email] => xyz#xyz.com [phone] => 9876543210 )
[1] => Array ( [id] => 3 [social_id] => [role_id] => 0 [name] => [first_name] => Dev [last_name] => Smith [gender] => [dob] => [language] => [location] => xyz [address] => xyz [email] => yyy#sds.com [phone] => 7838344344 )
[2] => Array ( [id] => 4 [social_id] => [role_id] => 0 [name] => [first_name] => Col [last_name] => Manon [gender] => male [dob] => 02-02-2017 [language] => english [location] => London [address] => ABCD [email] => londs#an.com [phone] => 7894561230)
) ;
$array = Array (
[0] => Array ( [id] => 1 [sm_id] => 1 [field_name] => first_name [value] => G2 [created_at] => 2017-02-24 11:05:03 [updated_at] => 2017-02-24 11:05:03 [deleted_at] => )
);
$arr = array();
foreach($row=0; $row<count($ar); $row++){
foreach($col=0; $col<count($array); $col++){
if($ar['0']['id'] == $array['0']['id']){
$arr['first_name'] = $ar['0']['first_name'];
$arr['last_name'] = $ar['0']['last_name'];
$arr['email'] = $ar['0']['email'];
$arr['phone'] = $ar['0']['phone'];
}
}
}
print_r($arr);die;
?>
I want map data from one array to other array based on $ar['0]['id'] with $array['0']['sm_id']
if $ar's id matched with $array's sm_id push to one array with its value how can i achieve the same. please suggest
Expected result
Array( [id] => 1 [first_name] => Jimmy [last_name] => rathore [gender] => male [dob] => 06-02-1992 [language] => english [location] => xyz [address] => xyz [email] => xyz#xyz.com [phone] => 9876543210 );
You could do it like this, given the original array is $ar, and the array with sm_id values $array:
// Create a hash of all sm_id values to be found
$ids = array_flip(array_column($array, "sm_id"));
// filter the original array to get only those with an id value in that hash
$arr = array_filter($ar, function ($row) use ($ids) {
return isset($ids[$row["id"]]);
});
print_r ($arr);
my first array is already sorted by name,but i want to usort it with position,
what i have tried is here,
array before usort
Array
(
[0] => Array
(
[name] => admin
[designation] => admin
[email] => admin#admin
[phone] => 999777788
[ext] => 67767
[position] => 1
[image] =>
)
[1] => Array
(
[name] => ATeam
[designation] => Manager
[email] => service#mail.com
[phone] =>
[ext] => 777
[position] => 3
[image] =>
)
[2] => Array
(
[name] => BTeam
[designation] => Manager
[email] =>g#mail.co.in
[phone] =>
[ext] =>
[position] => 4
[image] =>
)
[3] => Array
(
[name] => hi team
[designation] => new
[email] => abc
[phone] => 3333
[ext] => 333
[position] => 10
[image] =>
)
[4] => Array
(
[name] => new team
[designation] => maneger
[email] => mg#g
[phone] => 445567676
[ext] => ext
[position] => 10
[image] =>
)
)
usort function i applied to the array is:
usort($a_teams,function($a,$b){
if($a['position'] == '') return 1;
if($b['position'] == '') return -1;
return $a['position']-$b['position'];
});
the result array is:
Array
(
[0] => Array
(
[name] => admin
[designation] => admin
[email] => admin#admin
[phone] => 999777788
[ext] => 67767
[position] => 1
[image] =>
)
[1] => Array
(
[name] => ATeam
[designation] => Manager
[email] => service#mail.com
[phone] =>
[ext] => 777
[position] => 3
[image] =>
)
[2] => Array
(
[name] => BTeam
[designation] => Manager
[email] => g#mail.co.in
[phone] =>
[ext] =>
[position] => 4
[image] =>
)
[3] => Array
(
[name] => new team
[designation] => maneger
[email] => mg#g
[phone] => 445567676
[ext] => ext
[position] => 10
[image] =>
)
[4] => Array
(
[name] => hi team
[designation] => new
[email] => abc
[phone] => 3333
[ext] => 333
[position] => 10
[image] =>
)
)
but the problem is the alphabetical order of name is not getting in proper order when the positions is same
Changed the usort function for sorting by position and name:
usort($a_teams,function($a,$b){
if ($a["position"]==$b["position"]){
return strcmp($a["name"], $b["name"]);
}
return ($a["position"]<$b["position"])?-1:1;
});
Changed the usort function for sorting by position and name:
usort($a_specials,function($a,$b){
if($a['position'] == '') return 1;
if($b['position'] == '') return -1;
if($a['position'] == $b['position'])
return strnatcmp($a['title'],$b['title']);
return $a['position']-$b['position'];
});
I am attempting to json_encode an array in php5.3.
json_encode($paperwork_info[0])
The result is bad json. The array is as follows:
[paperwork_guid] => c5bfe512-908d-c513-5a5e-e3a2fbb5548b
[name] => recycle
[sections] => Array
(
[0] => header
[1] => customer
[2] => scope_bullets
[3] => product
[4] => signature
[5] => installer
[6] => order_data
)
[data] => Array
(
[header] => Array
(
[image] => Array
(
[src] => recycle.png
[format] => float:left
)
)
[customer] => Array
(
[f_name] => TEST ONLY
[l_name] => NEIMEIER
[middle_name] => none
[title] => none
[address1] => 28 OEHMAN BLVD
[address2] =>
[address3] =>
[zip] => 14225
[city] => BUFFALO
[state_abbrev] => NY
[email_address] => DALE.NEIMEIER#INSTALLS.COM
[phones] => Array
(
[alt] =>
[cell] =>
[work] =>
[home] => 7165551212
)
)
[scope_bullets] => Array
(
[sku_3001] => Array
(
[bullet_0] => Array
(
[paperwork_bullet_obj_guid] => 2ebefa96-6f6b-069e-e194-245d138b9845
[ordering] => 1
[bullet_text] => This is bullet point #1
[child_level] => 1
[formatting] => Array
(
[0] => Array
(
[start_word] => 2
[num_words] => 0
[tag] => b
[href] =>
)
[1] => Array
(
[start_word] => 2
[num_words] => 1
[tag] => a
[href] => http://www.cnn.com
)
)
)
[bullet_1] => Array
(
[paperwork_bullet_obj_guid] => 734db3f4-01a0-b025-9624-cc52a1845dff
[ordering] => 1
[bullet_text] => Sub-point #1.1
[child_level] => 2
)
[bullet_2] => Array
(
[paperwork_bullet_obj_guid] => ebf5ef02-906e-2005-e499-27eae2edefe9
[ordering] => 1
[bullet_text] => Sub point #1.1.1
[child_level] => 3
)
[bullet_3] => Array
(
[paperwork_bullet_obj_guid] => 447997c1-fd9c-25be-b9bf-39257009fb8b
[ordering] => 1
[bullet_text] => This is bullet point #2
[child_level] => 1
)
[bullet_4] => Array
(
[paperwork_bullet_obj_guid] => 5def2d9c-d322-788c-0afe-d13707004b97
[ordering] => 1
[bullet_text] => Sub point #2.1
[child_level] => 2
)
[bullet_5] => Array
(
[paperwork_bullet_obj_guid] => 84936151-65a3-bcca-951f-b69ff16d34ec
[ordering] => 2
[bullet_text] => Sub point #2.2
[child_level] => 2
)
)
[sku_4405] =>
)
[product] => Array
(
[0] => Array
(
[model] => Paperwork Test abc123
[serial] => 777333
[weight] => 26
[size] =>
[width] =>
[height] => 32
[num_boxes] => 1
[product_name] =>
[sku] =>
[model_number] =>
[part_number] =>
[serial_number] =>
[cat_value] => Best Buy Equipment
[product] =>
[product_type] =>
[product_size] =>
[category] =>
)
)
[signature] => Array
(
[date] => 2016-06-15
[need_customer] => 1
[need_tech] => 1
[need_store] =>
[need_warehouse] =>
)
[installer] => Array
(
[anum] => 45455
[inst_obj_guid] => fb91cf85-381c-b740-e063-775151743da2
[phone] => (317) 519-0481
[tech_f_name] =>
[tech_l_name] =>
[company_name] => ZICO LLC
[email_address] => ZICOLLC#GMAIL.COM
)
[order_data] => Array
(
[assigned_date] => 2016-06-07 22:24:47-04
[purchased_date] => 2016-06-06
[creation_date] => 2016-06-06
[install_date] => 2016-06-10
[install_time] => 19:00:00
[prescheduled_date] => 1969-12-31
[prom_start_time] => 2016-06-10 08:06:00
[prom_end_time] => 2016-06-10 11:06:00
[client_order_id] => 12365478996325412
[job_num] => 4043269
[campaign_wkord_obj_guid] => a9a8cc0b-d7ef-ac0e-df61-dad41c023cb0
[pos_obj_guid] => 096e38c2-55c3-80cf-0778-23f81f1cf2f6
[inst_obj_guid] => fb91cf85-381c-b740-e063-775151743da2
[client_obj_guid] => 247e893a-3ea4-c544-47b2-f23ff16017c6
[ord_obj_guid] => ae55e034-d7d0-5d13-3dd3-22f99df8ead4
[skus] => Array
(
[0] => Array
(
[job_type_obj_guid] => 8234ca2c-e40e-c48c-befc-7ceac5e9de32
[job_sku] => 3001
[client_sku] =>
[sku_name] => Site Survey
)
[1] => Array
(
[job_type_obj_guid] => a3f60c3b-ad3c-d828-9898-fa200edcd3cd
[job_sku] => 4405
[client_sku] =>
[sku_name] => Home Delivery
)
)
[client_specific_data] =>
)
)
)
The resultant json string is as follows:
{"paperwork_guid":"c5bfe512-908d-c513-5a5e-e3a2fbb5548b","name":"recycle","sections":{"0":"header","1"
:"customer","2":"scope_bullets","3":"product","4":"signature","5":"installer","6":"order_data"},"data"
:{"header":{"image":{"src":"recycle.png","format":"float:left"}},"customer":{"f_name":"TEST ONLY","l_name"
:"NEIMEIER","middle_name":"none","title":"none","address1":"28 OEHMAN BLVD","address2":"","address3"
:null,"zip":"14225","city":"BUFFALO","state_abbrev":"NY","email_address":"DALE.NEIMEIER#INSTALLS.COM"
,"phones":{"alt":"","cell":"","work":"","home":"7165551212"}},"scope_bullets":{"sku_3001":{"bullet_0"
:{"paperwork_bullet_obj_guid":"2ebefa96-6f6b-069e-e194-245d138b9845","ordering":"1","bullet_text":"This
is bullet point #1","child_level":"1","formatting":{"0":{"start_word":"2","num_words":"0","tag":"b"
,"href":""},"1":{"start_word":"2","num_words":"1","tag":"a","href":"http:\/\/www.cnn.com"}}},"bullet_1"
:{"paperwork_bullet_obj_guid":"734db3f4-01a0-b025-9624-cc52a1845dff","ordering":"1","bullet_text":"Sub-point
#1.1","child_level":"2"},"bullet_2":{"paperwork_bullet_obj_guid":"ebf5ef02-906e-2005-e499-27eae2edefe9"
,"ordering":"1","bullet_text":"Sub point #1.1.1","child_level":"3"},"bullet_3":{"paperwork_bullet_obj_guid"
:"447997c1-fd9c-25be-b9bf-39257009fb8b","ordering":"1","bullet_text":"This is bullet point #2","child_level"
:"1"},"bullet_4":{"paperwork_bullet_obj_guid":"5def2d9c-d322-788c-0afe-d13707004b97","ordering":"1","bullet_text"
:"Sub point #2.1","child_level":"2"},"bullet_5":{"paperwork_bullet_obj_guid":"84936151-65a3-bcca-951f-b69ff16d34ec"
,"ordering":"2","bullet_text":"Sub point #2.2","child_level":"2"}},"sku_4405":null},"product":{"0":{"model"
:"Paperwork Test abc123","serial":"777333","weight":"26","size":null,"width":null,"height":"32","num_boxes"
:"1","product_name":null,"sku":null,"model_number":null,"part_number":null,"serial_number":null,"cat_value"
:"Best Buy Equipment","product":null,"product_type":null,"product_size":null,"category":null}},"signature"
:{"date":"2016-06-15","need_customer":true,"need_tech":true,"need_store":false,"need_warehouse":false
},"installer":{"anum":"45455","inst_obj_guid":"fb91cf85-381c-b740-e063-775151743da2","phone":"(317) 519-0481"
,"tech_f_name":null,"tech_l_name":null,"company_name":"ZICO LLC","email_address":"ZICOLLC#GMAIL.COM"
},"order_data":{"assigned_date":"2016-06-07 22:24:47-04","purchased_date":"2016-06-06","creation_date"
:"2016-06-06","install_date":"2016-06-10","install_time":"19:00:00","prescheduled_date":"1969-12-31"
,"prom_start_time":"2016-06-10 08:06:00","prom_end_time":"2016-06-10 11:06:00","client_order_id":"12365478996325412"
,"job_num":"4043269","campaign_wkord_obj_guid":"a9a8cc0b-d7ef-ac0e-df61-dad41c023cb0","pos_obj_guid"
:"096e38c2-55c3-80cf-0778-23f81f1cf2f6","inst_obj_guid":"fb91cf85-381c-b740-e063-775151743da2","client_obj_guid"
:"247e893a-3ea4-c544-47b2-f23ff16017c6","ord_obj_guid":"ae55e034-d7d0-5d13-3dd3-22f99df8ead4","skus"
:{"0":{"job_type_obj_guid":"8234ca2c-e40e-c48c-befc-7ceac5e9de32","job_sku":"3001","client_sku":null
,"sku_name":"Site Survey"},"1":{"job_type_obj_guid":"a3f60c3b-ad3c-d828-9898-fa200edcd3cd","job_sku"
:"4405","client_sku":null,"sku_name":"Home Delivery"}},"client_specific_data":""}}}
It appears as though new line feeds are being inserted randomly throughout the josn string. I am not sure if it is just how firebug is outputting the string, but copying-and-pasting the string into jsonLINT produces errors.
Any help would be appreciated.
First of all what I want to achieve it's quite simple : I want to use relationship belongsTo from Laravel 5 to get a value from another table. Like so
<?= $email->contact->first_name; ?>
But this is not working at all, instead, I get this error : Trying to get property of non-object. But when I do print_r($email) it shows me that $email is actually an object...So I really don't understand now.
This is (a part of) what I have in my Controller :
$emails = Email::where('user_id', '=', Auth::user()->id)->take(6)->orderBy('created_at', 'desc')->get();
This is what I have in my view :
foreach($emails as $key => $email) {
echo $email->contact->first_name; ?>
}
This is my model email
public function contact()
{
return $this->belongsTo('App\Contact', 'contact_id');
}
As you can see, I am using a second parameter inside belongsTo method, which is 'contact_id' because I don't want to use 'user_id' as the foreign key.
EDIT:
This is my output of print_r($email) :
App\Email Object ( [connection:protected] => [table:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [id] => 88 [user_id] => 1 [group_id] => 1 [contact_id] => 79 [reminder_id] => 31 [subject] => Test task #3 [view] => 0 [view_hash] => $2y$10$NZ3B.UUqtRRl2opH5UwXOeRNeRROeZGdid6Crs4ShT1VptbZJS.iu [content] => blablabla
EDIT 2 :
This is my output of print_r($email->contact) :
App\Contact Object ( [fillable:protected] => Array ( [0] => title [1] => content ) [table:protected] => contacts [connection:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [id] => 86 [user_id] => 1 [group_id] => 1 [first_name] => Matthieu [last_name] => Boistoule [email] => m.boistoule#gmail.com [phone] => [company] => [title] => [website] => [industry] => [address] => [city] => [country] => [province] => [postal_code] => [lead_source] => [notes] => [created_at] => 2015-06-04 12:13:00 [updated_at] => 2015-06-04 12:13:00 ) [original:protected] => Array ( [id] => 86 [user_id] => 1 [group_id] => 1 [first_name] => Matthieu [last_name] => Boistoule [email] => m.boistoule#gmail.com [phone] => [company] => [title] => [website] => [industry] => [address] => [city] => [country] => [province] => [postal_code] => [lead_source] => [notes] => [created_at] => 2015-06-04 12:13:00 [updated_at] => 2015-06-04 12:13:00 ) [relations:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [casts:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 )