How to compare Associative Array in CodeIgniter - php

Array
(
[records] => Array
(
[0] => stdClass Object
(
[id] => 6
[fname] => hardik
[type] => Active
[rid] => 1
[rname] => Principle
[uid] => 48
)
[1] => stdClass Object
(
[id] => 12
[fname] => gautam
[type] => De-Active
[rid] => 1
[rname] => Principle
[uid] => 54
)
[2] => stdClass Object
(
[id] => 10
[fname] => burhan
[type] => Active
[rid] => 2
[rname] => Teacher
[uid] => 52
)
[3] => stdClass Object
(
[id] => 13
[fname] => gautam
[type] => De-Active
[rid] => 2
[rname] => Teacher
[uid] => 54
)
[4] => stdClass Object
(
[id] => 7
[fname] => abc
[password] => d41d8cd98f00b204e9800998ecf8427e
[type] => Active
[rid] => 3
[rname] => Parent
[uid] => 49
)
)
)
How can I compare associative array in Codeigniter for remove duplicate user?

Related

Comparing array objects if the have similar values in PHP

I am having two arrays $exam_questions and $attempted_responses. I want to compare attempted_responses['question_id'] object to $exam_questions['id'] if they match.
output of both arrays are:
$exam_questions
Array
(
[0] => stdClass Object
(
[id] => 747
[section_id] =>
[text] => What does HTML stand for?
[exam_id] => 83
[date_created] => 2019-11-27 09:41:41
[answered] => 1
)
[1] => stdClass Object
(
[id] => 748
[section_id] =>
[text] => Who is making the Web standards?
[exam_id] => 83
[date_created] => 2019-11-27 09:43:04
[answered] => 1
)
[2] => stdClass Object
(
[id] => 749
[section_id] =>
[text] => What does CSS stand for?
[exam_id] => 83
[date_created] => 2019-11-27 09:43:51
[answered] => 1
)
[3] => stdClass Object
(
[id] => 750
[section_id] =>
[text] => Which is the correct CSS syntax?
[exam_id] => 83
[date_created] => 2019-11-27 09:45:17
[answered] => 1
)
[4] => stdClass Object
(
[id] => 751
[section_id] =>
[text] => How do you insert a comment in a CSS file?
[exam_id] => 83
[date_created] => 2019-11-27 09:46:32
[answered] => 1
)
[5] => stdClass Object
(
[id] => 752
[section_id] =>
[text] => What is the correct HTML for inserting an image?
[exam_id] => 83
[date_created] => 2019-11-27 09:47:56
[answered] => 1
)
[6] => stdClass Object
(
[id] => 753
[section_id] =>
[text] => How can you make a list that lists the items with numbers?
[exam_id] => 83
[date_created] => 2019-11-27 09:48:50
[answered] => 1
)
[7] => stdClass Object
(
[id] => 754
[section_id] =>
[text] => What is the correct HTML for creating a hyperlink?
[exam_id] => 83
[date_created] => 2019-11-27 09:49:45
[answered] => 1
)
)
$attempted_responses
Array
(
[0] => Array
(
[question_id] => 747
[answer_id] => 2641
)
[1] => Array
(
[question_id] => 748
[answer_id] => 2646
)
[2] => Array
(
[question_id] => 749
[answer_id] => 2650
)
[3] => Array
(
[question_id] => 750
[answer_id] => 2654
)
[4] => Array
(
[question_id] => 751
[answer_id] => 2656
)
[5] => Array
(
[question_id] => 752
[answer_id] => 2661
)
[6] => Array
(
[question_id] => 753
[answer_id] => 2663
)
[7] => Array
(
[question_id] => 754
[answer_id] => 2668
)
)
foreach($exam_questions as $exam_question){
foreach($attempted_responses as $attempted_response){
if($exam_question->id === $attempted_response['question_id']){
// your code
}
}
}
or your convert $exam_questions as a nested array.
$exam_questions_array = json_decode(json_encode($exam_questions), true);
then it can treated as a nested array.
foreach($exam_questions_array as $exam_question){
foreach($attempted_responses as $attempted_response){
if($exam_question['id'] === $attempted_response['question_id']){
// your code
}
}
}

how to fetch data from json? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
i have data in json something like that:
stdClass Object
(
[contacts] => stdClass Object
(
[14] => stdClass Object
(
[data] => stdClass Object
(
[email] => veer#gmail.com
[first_name] => veer
[last_name] =>
[user_id] => 16
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 14
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => veer
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 92
[1] => 13
[2] => 12
[3] => 9
)
[files] => Array
(
)
)
[9] => stdClass Object
(
[data] => stdClass Object
(
[email] => singh.pratibha1432#gmail.com
[first_name] => Pratibha
[last_name] => Singh
[user_id] => 8
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 9
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => pratibha
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 94
[1] => 93
[2] => 92
[3] => 91
[4] => 82
[5] => 15
[6] => 14
[7] => 13
[8] => 9
)
[files] => Array
(
)
)
[4] => stdClass Object
(
[data] => stdClass Object
(
[email] => singh.dev1432#gmail.com
[first_name] => Devesh
[last_name] => Singh
[user_id] => 7
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 4
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => devesh
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 93
[1] => 92
[2] => 15
[3] => 12
[4] => 11
[5] => 9
)
[files] => Array
(
)
)
[2] => stdClass Object
(
[data] => stdClass Object
(
[email] => admin#gmail.com
[first_name] => veronica
[last_name] =>
[user_id] => 1
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 2
[gravatar] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => admin
[profile_picture] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 94
[1] => 92
[2] => 15
[3] => 14
[4] => 13
[5] => 9
)
[files] => Array
(
)
)
)
[status] => success
)
and now I am trying to fetch "email" from the contacts->id->data->email so for that i am using this code. I am trying to loop the things by which i can fetch email from all the ids present. and in the ids section, there is data and inside data the email is present so how can i fetch all emails from all the ids.
foreach ((Array)$body->contacts as $id => $values) {
foreach ($values as $data => $value) {
$emails = array('email'=>$value->email);
return $emails;
}
}
but it return only single data:
Array
(
[email] => veer#gmail.com
)
i want to fetch all the email like that:
Array
(
[0] => Array
(
[emails] => veer#gmail.com
)
[1] => Array
(
[emails] => singh.pratibha1432#gmail.com
)
[2] => Array
(
[emails] => singh.dev1432#gmail.com
)
[3] => Array
(
[emails] => admin#gmail.com
)
)
how can I achieve this?
Define null array and in foreach run store every email in that variable.
$emails = array();
foreach ((Array)$body->contacts as $id => $values) {
foreach ($values as $data => $value) {
$emails[] = array('emails'=>$contact->data->email;);
}
}
return $emails;

Laravel Foreach with a multidimensional array

I'm using Laravel and I wonder how can I put this array in a foreach? for read it
stdClass Object ( [data] => Array ( [0] => stdClass Object ( [id] => 0001 [type] => Item [attributes] => stdClass Object ( [product_name] => ST Slotted Sport Brake Rotors [part_number] => st126.39027SL [mfr_part_number] => 126.39027SL [part_description] => StopTech Power Slot 00-04 Volvo S40/V40 Front Left Slotted Rotor [category] => Brakes, Rotors & Pads [subcategory] => Brake Rotors - Slotted [dimensions] => Array ( [0] => stdClass Object ( [box_number] => 1 [length] => 15 [width] => 15 [height] => 4 [weight] => 12 ) ) [brand_id] => 56 [brand] => Stoptech [price_group_id] => 129 [price_group] => Stoptech [active] => 1 [regular_stock] => [dropship_controller_id] => 19 [air_freight_prohibited] => [not_carb_approved] => [prop_65] => Unknown [warehouse_availability] => Array ( [0] => stdClass Object ( [location_id] => 01 [can_place_order] => 1 ) [1] => stdClass Object ( [location_id] => 02 [can_place_order] => 1 ) [2] => stdClass Object ( [location_id] => 59 [can_place_order] => 1 ) ) [thumbnail] => https://d5otzd52uv6zz.cloudfront.net/fa33cfb5-0c91-47de-9a49-67f4175a5bff-100.jpg [barcode] => 805890204282 ) ) [1] => stdClass Object ( [id] => 078595 [type] => Item [attributes] => stdClass Object ( [product_name] => RAD Clutch Fork Stop [part_number] => rad20-0262 [mfr_part_number] => 20-0262 [part_description] => Radium Engineering Mitsubishi Evo 8-10 Clutch Fork Stop [category] => Drivetrain [subcategory] => Clutch Uncategorized [dimensions] => Array ( [0] => stdClass Object ( [box_number] => 1 [length] => 4.75 [width] => 4.5 [height] => 4.5 [weight] => 0.5 ) ) [brand_id] => 148 [brand] => Radium Engineering [price_group_id] => 406 [price_group] => Radium Engineering [active] => 1 [regular_stock] => 1 [dropship_controller_id] => 81 [air_freight_prohibited] => [not_carb_approved] => [prop_65] => Unknown [warehouse_availability] => Array ( [0] => stdClass Object ( [location_id] => 01 [can_place_order] => 1 ) [1] => stdClass Object ( [location_id] => 02 [can_place_order] => 1 ) [2] => stdClass Object ( [location_id] => 59 [can_place_order] => 1 ) ) [thumbnail] => https://d32vzsop7y1h3k.cloudfront.net/adb8244ed2d562e078c5ce2928750f2a.JPG ) ) [2] => stdClass Object ( [id] => 095906 [type] => Item [attributes] => stdClass Object ( [product_name] => MLR Trans Rebuild Kits [part_number] => mlr88075K [mfr_part_number] => 88075K [part_description] => McLeod Performance Transmission Rebuild Kit w/ Kolene Steels 4L80E 1997-2011 - Stage 1 [category] => Drivetrain [subcategory] => Transmission Rebuild Kits [dimensions] => Array ( [0] => stdClass Object ( [box_number] => 1 [length] => 24 [width] => 16 [height] => 5.25 [weight] => 14.75 ) ) [brand_id] => 169 [brand] => McLeod Racing [price_group_id] => 456 [price_group] => McLeod Racing [active] => 1 [regular_stock] => [dropship_controller_id] => 116 [air_freight_prohibited] => [not_carb_approved] => [prop_65] => Unknown [warehouse_availability] => Array ( [0] => stdClass Object ( [location_id] => 01 [can_place_order] => 1 ) [1] => stdClass Object ( [location_id] => 02 [can_place_order] => 1 ) [2] => stdClass Object ( [location_id] => 59 [can_place_order] => 1 ) ) ) ) [3] => stdClass Object ( [id] => 1 [type] => Item [attributes] => stdClass Object ( [product_name] => TXS Boost Controllers [part_number] => txs-BC-HPBC [mfr_part_number] => txs-BC-HPBC [part_description] => Turbo XS High Performance Boost Controller [category] => Forced Induction [subcategory] => Boost Controllers [dimensions] => Array ( [0] => stdClass Object ( [box_number] => 1 [length] => 4.75 [width] => 3.25 [height] => 2.25 [weight] => 0.5 ) ) [brand_id] => 63 [brand] => Turbo XS [price_group_id] => 130 [price_group] => Turbo XS [active] => 1 [regular_stock] => 1 [dropship_controller_id] => 0 [air_freight_prohibited] => [not_carb_approved] => [prop_65] => Unknown [warehouse_availability] => Array ( [0] => stdClass Object ( [location_id] => 01 [can_place_order] => 1 ) [1] => stdClass Object ( [location_id] => 02 [can_place_order] => 1 ) [2] => stdClass Object ( [location_id] => 59 [can_place_order] => 1 ) ) [thumbnail] => https://d5otzd52uv6zz.cloudfront.net/92670bde-a650-4ae6-a53e-0da8b2a26d1c-100.jpg [barcode] => 053176487038 ) )
Thanks!
Just loop through your data with
foreach($yourvar->data as $key=> $data){
echo $data->id;
}
If you get an StdClass object it means you're iterating over an object through -> if you've an array you can print through $data['key']

anyone to help me convert stdClass Object to an array in php

stdClass Object ( [Order] => stdClass Object
( [id] => c62f5d272ghjjgd346lmn34
[type] => [status] => placed
[keys_status] => Keys were generated )
[Products] => Array ( [0] => stdClass Object ( [name] => Mobile Security
[devices] => 1 [months] => 12 [quantity] => 1 [msrp_price] => 9 [discount] => 0.0 [end_user_price] => 9 [reseller_margin] => 0.0 [reseller_price] => 9.01[partner_margin] => 90 [partner_price] => 0.9[Keys] => Array ( [0] => stdClass Object ( [product_id] => 21 [product_name] => Mobile Security [key] => 1234) ) ) ) [Customer] => stdClass Object ( [name] => IPPS [phone] => 25667587576 [address] => Bishop willis Street [contact_person] => Mukama Gordon [city] => Kigali [zipcode] => 250 [country_id] => 202 [state_id] => [email] => mukamatest#test.com ) [Keys] => Array ( [0] => stdClass Object ( [product_id] => 21 [product_name] => Mobile Security [key] => 1234 ) ) )
This is a really hacky way of doing it, but try
$arrayversion = json_decode(json_encode($objectversion), true);
Then
print_r($arrayversion); will be an array.

array format issue PHP

I want to change an array format according to my needs. When I fetch data from a database using cakephp find('all') method, it returns something that is not in the format that I expected.
My resultant array is:
Array
(
[0] => Array
(
[DriverLocation] => Array
(
[id] => 15
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 43
[created] => 2015-05-20 12:25:34
)
[ZipCode] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[1] => Array
(
[DriverLocation] => Array
(
[id] => 19
[dispensary_id] => 1
[driver_id] => 43
[zip_code_id] => 42
[created] => 2015-05-20 12:37:12
)
[ZipCode] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[UserProfile] => Array
(
[first_name] => Pawan
[last_name] => Kumar
)
)
[2] => Array
(
[DriverLocation] => Array
(
[id] => 20
[dispensary_id] => 1
[driver_id] => 83
[zip_code_id] => 42
[created] => 2015-05-20 12:37:28
)
[ZipCode] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
[3] => Array
(
[DriverLocation] => Array
(
[id] => 26
[dispensary_id] => 1
[driver_id] => 83
[zip_code_id] => 43
[created] => 2015-05-20 12:43:59
)
[ZipCode] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
[4] => Array
(
[DriverLocation] => Array
(
[id] => 41
[dispensary_id] => 1
[driver_id] => 83
[zip_code_id] => 6
[created] => 2015-05-21 05:23:53
)
[ZipCode] => Array
(
[id] => 6
[province_id] => 3846
[city] => Whittier
[zip_code] => 90607
[status] => active
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
)
And I want to convert it into something like this:
Array
(
[DriverLocation] => Array
(
[id] => 15
[dispensary_id] => 1
[driver_id] => 85
)
[ZipCode] => Array
(
[zip_code] => Array
(
[0] => 15478
)
[city] => Array
(
[0] => Rohtak
)
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
Array
(
[DriverLocation] => Array
(
[id] => 19
[dispensary_id] => 1
[driver_id] => 43
)
[ZipCode] => Array
(
[zip_code] => Array
(
[0] => 30215
)
[city] => Array
(
[0] => Rohtak
)
)
[UserProfile] => Array
(
[first_name] => Pawan
[last_name] => Kumar
)
)
Array
(
[DriverLocation] => Array
(
[id] => 20
[dispensary_id] => 1
[driver_id] => 83
)
[ZipCode] => Array
(
[zip_code] => Array
(
[0] => 30215
[1] => 15478
[2] => 90607
)
[city] => Array
(
[0] => Rohtak
[1] => Rohtak
[2] => Whittier
)
)
[UserProfile] => Array
(
[first_name] => Ramesh
[last_name] => Saini
)
)
Filter according to DriverLocation->driver_id
Seems that these arrays are exactly the same, except that you put every "record" of DriverLocation in its own variable. The idea here is that you receive the data from the DriverLocation Model in the fictional controller and put it in a variable; for example:
$allDriverLocations = $this->DriverLocation->find("all");
Now $allDriverLocations contains the first one of your mentioned arrays.
Next step is to pass it to the view:
$this->set(compact("allDriverLocations"));
And now to create a table in the associated view you should iterate through $allDriverLocations:
<table>
<tbody>
<?php
// Loop through the array with a foreach
foreach($allDriverLocations as $driverLocation){
// Create the table row here using the HtmlHelper.
// If you want to reach "Rohtak" for example, you use $driverLocation["Zipcode"]["city"] to print it.
}
?>
</tbody>
</table>
I wouldn't know why you should not follow Cake's easy conventions...

Categories