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...
Related
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;
I've an associative array. I am attempting to create each sub array within a loop and insert it into a multidimensional array. Google suggested using array_merge, but after using 'print_r' on the multidimensional array with the code below, only the last sub-array is being displayed.
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
[id] => 184
[firstName] => skg
[lastName] => s
[address] => Array
(
[address1] => sakthi
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
)
[jobOrder] => Array
(
[id] => 88
[title] => Tech Analyst
)
)
[1] => Array
(
[candidate] => Array
(
[id] => 852
[firstName] => mso cool
[lastName] =>
[address] => Array
(
[address1] =>
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
)
[jobOrder] => Array
(
[id] => 57
[title] => Tester
)
)
And Another Array like this :
[rating] => Array
(
[0] => 7
[1] => 5
[2] =>
)
How to get merge this array value into previous array values like
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
[id] => 184
[firstName] => skg
[lastName] => s
[address] => Array
(
[address1] => sakthi
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
[rating] => 7
)
[jobOrder] => Array
(
[id] => 88
[title] => Tech Analyst
)
)
[1] => Array
(
[candidate] => Array
(
[id] => 852
[firstName] => mso cool
[lastName] =>
[address] => Array
(
[address1] =>
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
[rating] => 5
)
[jobOrder] => Array
(
[id] => 57
[title] => Tester
)
)
I tried lot of tricks but couldn't get the desired array format. Can any one help me in this to get the desired array? Thanks in advance.
$arr = array(); //filled with data
$rating = array(); //filled with rates
foreach ( $rating as $key =>$rate ) {
$arr['data'][ $key ]['rating'] = $rate;
}
<?php
$arr=Array(
'data'=>Array(
0=>Array('candidate'=>Array()),
1=>Array('candidate'=>Array())
)
);
$rating=Array(7,5);
foreach($rating as $key=>$val){
$arr['data'][$key]['rating']=$val;
}
print_r($arr);
Returns:
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
)
[rating] => 7
)
[1] => Array
(
[candidate] => Array
(
)
[rating] => 5
)
)
)
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
(
[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?
I have an array that is fetched from database using some bindModel in cakephp.
finally now I want to change its format according to my liking to own.
here is the array contains.
Array
(
[0] => Array
(
[DriverLocation] => Array
(
[id] => 1
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 42
[created] => 2015-05-25 12:01:14
)
[ZipCode] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[1] => Array
(
[DriverLocation] => Array
(
[id] => 2
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 43
[created] => 2015-05-25 12:01:14
)
[ZipCode] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[2] => Array
(
[DriverLocation] => Array
(
[id] => 3
[dispensary_id] => 1
[driver_id] => 77
[zip_code_id] => 41
[created] => 2015-05-25 12:45:47
)
[ZipCode] => Array
(
[id] => 41
[province_id] => 3846
[city] => Malviya Vihar
[zip_code] => 12558
[status] => active
)
[UserProfile] => Array
(
[first_name] => Manish
[last_name] => Kumar
)
)
)
I want to convert or change its format something like as follows.
This will altered on the behalf of DriverLocation=>driver_id
Array
(
[0] => Array
(
[DriverLocation] => Array
(
[id] => 1
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 42
[created] => 2015-05-25 12:01:14
)
[ZipCode] => Array
(
[0] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[1] => 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] => 3
[dispensary_id] => 1
[driver_id] => 77
[zip_code_id] => 41
[created] => 2015-05-25 12:45:47
)
[ZipCode] => Array
(
[id] => 41
[province_id] => 3846
[city] => Malviya Vihar
[zip_code] => 12558
[status] => active
)
[UserProfile] => Array
(
[first_name] => Manish
[last_name] => Kumar
)
)
)
You can use this loop:
foreach($ar as $k1=>$a1){
foreach($ar as $k2=>$a2){
if($k1 < $k2 && $a1["DriverLocation"]["driver_id"] == $a2["DriverLocation"]["driver_id"]){
$ar[$k1]["ZipCode"][] = $a2["ZipCode"];
if(isset($ar[$k1]["ZipCode"]["id"])){
$ar[$k1]["ZipCode"][] = array("id"=>$ar[$k1]["ZipCode"]["id"]);
unset($ar[$k1]["ZipCode"]["id"]);
}
unset($ar[$k2]);
}
}
}
Use array_push() function to make it
for sample example
<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
output
Array ( [0] => red [1] => green [2] => blue [3] => yellow )