How i can transform this first array [ONE}: Into second array [TWO} where i can call meta_key and than i get var: i get this array by calling specified sql rows and i am using this query: $user_data = $wpdb->get_results("SELECT * FROM $wpdb->usermeta WHERE user_id = $fivesdraft->user_id "); maybe its another better way to connect it in query? not in PHP
Array ONE
(
[0] => stdClass Object
(
[umeta_id] => 16
[user_id] => 2
[meta_key] => nickname
[meta_value] => user1
)
[1] => stdClass Object
(
[umeta_id] => 17
[user_id] => 2
[meta_key] => first_name
[meta_value] => testname
)
[2] => stdClass Object
(
[umeta_id] => 18
[user_id] => 2
[meta_key] => last_name
[meta_value] => testlastname
)
[3] => stdClass Object
(
[umeta_id] => 19
[user_id] => 2
[meta_key] => description
[meta_value] => user desc
)
)
Array TWO
(
[nickname] => 'user1'
[first_name] => 'user desc'
[last_name] => 'user desc'
[description] => 'user desc'
...
)
You can use array_reduce:
$myArray = array_reduce($arrayOne, function ($result, $item) {
$result[$item->meta_key] = $item->meta_value;
return $result;
}, array());
Just loop over the array and write the values in a new array
$two = array();
foreach ($one as $item) {
$two[$item->meta_key] = $item->meta_value;
}
Related
I am getting below json data thru Shiprocket API. Now I want to extract value of below variables in PHP code from this json.
I have tried to use json_decode but it did not work and show null value:
$data = json_decode($json);
$sr_status = $data['shipment_status'];
Please suggest the code to retrieve below variables value.
shipment_status , awb_code , courier_company_id
Array
(
[0] => stdClass Object
(
[tracking_data] => stdClass Object
(
[track_status] => 1
[shipment_status] => 7
[shipment_track] => Array
(
[0] => stdClass Object
(
[id] => 180339484
[awb_code] => 11150911492
[courier_company_id] => 55
[shipment_id] => 1711169662
[order_id] => 233223781187
[pickup_date] => 2023-01-11 03:02:00
[delivered_date] => 2023-01-16 12:22:00
[weight] => 0.25
[packages] => 1
[current_status] => Delivered
[delivered_to] => Solapur
[destination] => Solapur
[consignee_name] => ABC
[origin] => Ludhiana
[courier_agent_details] =>
[edd] =>
)
)
[shipment_track_activities] => Array
(
[0] => stdClass Object
(
[date] => 2023-01-16 12:22:00
[status] => 000-T-DL
[activity] => SHIPMENT DELIVERED
[location] => SOLAPUR
[sr-status] => 7
[sr-status-label] => DELIVERED
)
[1] => stdClass Object
(
[date] => 2023-01-16 11:34:00
[status] => 002-S-UD
[activity] => SHIPMENT OUTSCAN
[location] => SOLAPUR
[sr-status] => 17
[sr-status-label] => OUT FOR DELIVERY
)
)
[track_url] => https://shiprocket.co//tracking/11150911492
[etd] => 2023-01-14 17:02:00
[qc_response] => stdClass Object
(
[qc_image] =>
[qc_failed_reason] =>
)
)
)
)
you can try this:
$array = ...; // Your array here
$data= json_decode($array);
$shipment_status = $data[0]->tracking_data->shipment_status;
$awb_code = $data[0]->tracking_data->shipment_track[0]->awb_code;
$courier_company_id = $data[0]->tracking_data->shipment_track[0]->courier_company_id;
Or use $data = json_decode($json,true); which return an array where you can use
foreach($data as $val) {
$shipment_status = $val['tracking_data']['shipment_status'];
foreach ($val['shipment_track'] as $value) {
$awb_code = $value['awb_code'];
$courier_company_id = $value['courier_company_id'];
}
}
I am exporting data from mysqli to google firebase DB. In firebase table one of the column named siteGeoCode (latitude and longitude) having values like [17.426083,78.439241] in array format. But in mysqli table two separate columns are there for latitude and longitude. I am trying to fetch records like below and converting those to array format.
$emp_array = array();
$sel_query = "select companyId,countryCode,createdBy,createdDat,`latitude`,`longitude`,siteName,`status` from customers limit 0,3";
$res = mysqli_query($mysqliConn,$sel_query);
while($sel_row = mysqli_fetch_assoc($res))
{
$emp_array[] = $sel_row;
$lat_array['siteGeoCode'][0] = $sel_row['latitude'];
$lat_array['siteGeoCode'][1] = $sel_row['longitude'];
array_push($emp_array,$lat_array);
}
// output
[0] => Array
(
[companyId] => iArwfGLC1lE6x3lO24cb
[countryCode] => 91
[createdBy] => X54P6gVJ7dA4Fi2fjEmc
[createdDate] => 2019-08-20 00:58:08
[latitude] => 15.6070347
[longitude] => 79.6146273
[siteName] => Ongole
[status] => 1
)
[1] => Array
(
[siteGeoCode] => Array
(
[0] => 15.6070347
[1] => 79.6146273
)
)
[2] => Array
(
[companyId] => YPbhSLWQfAR6ThhszSAf
[countryCode] => 91
[createdBy] => iArwfGLC1lE6x3lO24cb
[createdDate] => 2019-09-10 22:37:08
[latitude] =>
[longitude] =>
[siteName] => Madhap
[status] =>
)
[3] => Array
(
[siteGeoCode] => Array
(
[0] =>
[1] =>
)
)
but my desired output should be like below
[0] => Array
(
[companyId] => iArwfGLC1lE6x3lO24cb
[countryCode] => 91
[createdBy] => X54P6gVJ7dA4Fi2fjEmc
[createdDate] => 2019-08-20 00:58:08
[siteGeoPoint] => Array
(
[0] => 15.6070347
[1] => 79.6146273
)
[siteName] => Ongole
[status] => 1
)
[1] => Array
(
[companyId] => YPbhSLWQfAR6ThhszSAf
[countryCode] => 91
[createdBy] => iArwfGLC1lE6x3lO24cb
[createdDate] => 2019-09-10 22:37:08
[siteGeoPoint] => Array
(
[0] =>
[1] =>
)
[siteName] => Madhap
[status] =>
)
How can I achieve this ? Any help would be greatly appreciated. Thanks in advance
If you are wanting to place the result of the latitude/longitude columns into the result grouped, do the manipulations to the row, then add it to your result set, abit like below.
$result = [];
while ($row = mysqli_fetch_assoc($res)) {
// group geo
$row['siteGeoCode'] = [
$row['latitude'],
$row['longitude']
];
// unset uneeded
unset($row['latitude'], $row['longitude']);
// place in result
$result[] = $row;
}
You can use following code.
$output_array = array();
while($sel_row = mysqli_fetch_assoc($res)) {
$output_array[] = array(
'companyId' => $sel_row['companyId'],
'countryCode' => $sel_row['countryCode'],
'createdBy' => $sel_row['createdBy'],
'createdDate' => $sel_row['createdDate'],
'siteGeoPoint' => array($sel_row['latitude'], $sel_row['longitude']),
'siteName' => $sel_row['siteName'],
'status' => $sel_row['status'],
);
}
print_r($output_array);
you can see the difference you've used the array and I did. From you output I guess you've print different arrays.
Hope the code get you desire result!
$email_users[] = $row;
print_r($email_users);
Result:
Array (
[0] => Array (
[user_id] => 87436
[username] => Admin
[user_email] => email#online.us
[user_lang] => de
[allowed] => 1 )
[1] => Array (
[user_id] => 68013
[username] => Testuser
[user_email] => email2#online.us
[user_lang] => de
[allowed] => 1 )
[2] => Array (
[user_id] => 68013
[username] => Testuser
[user_email] => email2#online.us
[user_lang] => de
[allowed] => 1 )
)
As you can see, the user_id 68013 is double. I have to remove the double Array. The Result should look like this:
Array (
[0] => Array (
[user_id] => 87436
[username] => Admin
[user_email] => email#online.us
[user_lang] => de
[allowed] => 1 )
[1] => Array (
[user_id] => 68013
[username] => Testuser
[user_email] => email2#online.us
[user_lang] => de
[allowed] => 1 )
)
I read and tried several solutions I found on stack. For example:
How to get unique value in multidimensional array
$email_users[] = $row;
$user_ids = array();
foreach ($email_users as $h) {
$user_ids[] = $h['user_id'];
}
$email_users = array_unique($user_ids);
print_r($email_users);
But the print_r is only:
Array ( [0] => 87436 [1] => 68013 )
Thanks for your time.
You should get distinct value from your query. Use SELECT DISTINCT * FROM tablename and you will get Distinct value.
Or use $email_users= array_unique($email_users, SORT_REGULAR);
Array(
[0] => stdClass Object
(
[id] => 1
[user_id] => 1
[following_id] => 2
[type] => user
[created_on] => 2014-03-01 04:43:57
)
[1] => stdClass Object
(
[id] => 15
[user_id] => 1
[following_id] => 4
[type] => user
[created_on] => 2014-05-27 11:55:17
)
)
Hi friends,
I have an array of products that I need to create manually next stdclass Object.These array are generated by pushing value.
I'm trying to solve this for more than an hour now, but I don't get it to work. I know it should be easy...but anyway - I don't get it :D
I have working for an hour to create manually next stdclass Object. I want like this.
How do i create this.
Can anyone help with this?
Thanks in Advance,
J
Array(
[0] => stdClass Object
(
[id] => 1
[user_id] => 1
[following_id] => 2
[type] => user
[created_on] => 2014-03-01 04:43:57
)
[1] => stdClass Object
(
[id] => 15
[user_id] => 1
[following_id] => 4
[type] => user
[created_on] => 2014-05-27 11:55:17
)
[2] => stdClass Object
(
[id] => 16
[user_id] => 1
[following_id] => 5
[type] => user
[created_on] => 2014-05-27 11:55:17
)
)
One way of doing this is to use a function like so :
function create($id, $user_id, $following_id, $type, $created_on){
$obj = new stdClass;
$obj->id = $id;
$obj->user_id = $user_id;
$obj->following_id = $following_id;
$obj->type = $type;
$obj->created_on = $created_on;
return $obj;
}
And assuming your array is $array, you add items to it like this:
$array[] = create(16, 1, 5, 'user', '2014-05-27 11:55:17');
Hope this helps
I have an SQL query that returns 14 rows per userID.
It pulls a lot of data to verify different fields, but specifically returned I need the first and last name. The first name I can get from $firstName[]=$row2[0]; but I can't get the last name value out of the array. I can see its position, but I'm not sure how to call that position in the loop.
echo '<table border=1><tr><td>Student ID</td><td>Full Name</td><td>Class</td></tr>';
//Build Student Info1 table
foreach($userID as $val) {
$user_id= $val;
$query="SELECT * FROM wp_usermeta WHERE meta_key ='purchased_instances' AND user_id ='$user_id'";
$query2="SELECT meta_value FROM wp_usermeta WHERE user_id ='$user_id'";
$result = $link->query($query)or die($link->error);
$result2 = $link->query($query2)or die($link->error);
if($result->num_rows > 0){
/* fetch associative array */
$i=0;
while ($row = mysqli_fetch_array($result)) {
while($row2 = mysqli_fetch_array($result2)) {
$details[$i][]=$row2;
$firstName[]=$row2[0];
$lastName[]=$row2[0]; //Can't get this value right, need the first element of the 2 indexed array
}
$result1[]=$row;
$studentID[$i]=$row[1];
$classDetails[$i]=$row[3];
echo'<tr><td>';
print_r($studentID[$i]);
echo '</td><td>';
print_r($firstName[$i]);
echo ' ';
print_r($lastName[$i]);
echo'</td><td>';
print_r($classDetails[$i]);
echo '</td></tr>';
$i++;
}
/* free result set */
mysqli_free_result($result);
}
}
echo '</table>';
This is what print_r($details) produces:
Array ( [0] => Array ( [0] => 2567 [umeta_id] => 2567 [1] => 185 [user_id] => 185 [2] => first_name [meta_key] => first_name [3] => Kevin [meta_value] => Kevin ) [1] => Array ( [0] => 2568 [umeta_id] => 2568 [1] => 185 [user_id] => 185 [2] => last_name [meta_key] => last_name [3] => RealLastNameHERE [meta_value] => RealLastNameHERE ) [2] => Array ( [0] => 2569 [umeta_id] => 2569 [1] => 185 [user_id] => 185 [2] => nickname [meta_key] => nickname [3] => KMOwner [meta_value] => KMOwner ) [3] => Array ( [0] => 2570 [umeta_id] => 2570 [1] => 185 [user_id] => 185 [2] => description [meta_key] => description [3] => [meta_value] => ) [4] => Array ( [0] => 2571 [umeta_id] => 2571 [1] => 185 [user_id] => 185 [2] => rich_editing [meta_key] => rich_editing [3] => true [meta_value] => true ) [5] => Array ( [0] => 2572 [umeta_id] => 2572 [1] => 185 [user_id] => 185 [2] => comment_shortcuts [meta_key] => comment_shortcuts [3] => false [meta_value] => false ) [6] => Array ( [0] => 2573 [umeta_id] => 2573 [1] => 185 [user_id] => 185 [2] => admin_color [meta_key] => admin_color [3] => fresh [meta_value] => fresh ) [7] => Array ( [0] => 2574 [umeta_id] => 2574 [1] => 185 [user_id] => 185 [2] => use_ssl [meta_key] => use_ssl [3] => 0 [meta_value] => 0 ) [8] => Array ( [0] => 2575 [umeta_id] => 2575 [1] => 185 [user_id] => 185 [2] => show_admin_bar_front [meta_key] => show_admin_bar_front [3] => true [meta_value] => true ) [9] => Array ( [0] => 2576 [umeta_id] => 2576 [1] => 185 [user_id] => 185 [2] => wp_capabilities [meta_key] => wp_capabilities [3] => a:1:{s:10:"subscriber";b:1;} [meta_value] => a:1:{s:10:"subscriber";b:1;} ) [10] => Array ( [0] => 2577 [umeta_id] => 2577 [1] => 185 [user_id] => 185 [2] => wp_user_level [meta_key] => wp_user_level [3] => 0 [meta_value] => 0 ) [11] => Array ( [0] => 2578 [umeta_id] => 2578 [1] => 185 [user_id] => 185 [2] => mgm_member_options [meta_key] => mgm_member_options [3] => a:35:{s:2:"id";i:185;s:13:"custom_fields";a:6:{s:10:"first_name";s:5:"Kevin";s:9:"last_name";s:13:"RealLastNameHERE";s:5:"email";s:23:"RealEmailAddressHERE";s:8:"username";s:7:"RealuserNameHERE";s:8:"password";s:16:"Lwp9E8RZECyB9bzb";s:13:"password_conf";N;}s:22:"other_membership_types";a:0:{}s:12:"payment_info";a:0:{}s:6:"coupon";a:2:{s:12:"update_usage";b:0;s:15:"coupon_usage_id";b:0;}s:7:"upgrade";a:1:{s:6:"coupon";a:0:{}}s:6:"extend";a:0:{}s:4:"code";s:10:"mgm_member";s:4:"name";s:10:"Member Lib";s:11:"description";s:10:"Member Lib";s:7:"setting";a:0:{}s:6:"saving";b:1;s:8:"trial_on";i:0;s:10:"trial_cost";d:0;s:14:"trial_duration";i:0;s:19:"trial_duration_type";s:1:"d";s:16:"trial_num_cycles";i:0;s:8:"duration";i:1;s:13:"duration_type";s:1:"l";s:6:"amount";d:0;s:8:"currency";s:3:"USD";s:9:"join_date";i:1383183050;s:13:"last_pay_date";s:0:"";s:11:"expire_date";s:0:"";s:15:"membership_type";s:4:"free";s:6:"status";s:6:"Active";s:12:"payment_type";s:0:"";s:13:"autoresponder";s:10:"mgm_aweber";s:10:"subscribed";s:1:"Y";s:22:"autoresponder_notified";s:1:"Y";s:13:"user_password";s:16:"Lwp9E8RZECyB9bzb";s:17:"active_num_cycles";i:1;s:7:"pack_id";s:2:"22";s:12:"account_desc";s:12:"Free Account";s:16:"hide_old_content";s:1:"0";} [meta_value] => a:35:{s:2:"id";i:185;s:13:"custom_fields";a:6:{s:10:"first_name";s:5:"Kevin";s:9:"last_name";s:13:"RealLastNameHERE";s:5:"email";s:23:"RealuserNameHERE";s:8:"username";s:7:"RealuserNameHERE";s:8:"password";s:16:"Lwp9E8RZECyB9bzb";s:13:"password_conf";N;}s:22:"other_membership_types";a:0:{}s:12:"payment_info";a:0:{}s:6:"coupon";a:2:{s:12:"update_usage";b:0;s:15:"coupon_usage_id";b:0;}s:7:"upgrade";a:1:{s:6:"coupon";a:0:{}}s:6:"extend";a:0:{}s:4:"code";s:10:"mgm_member";s:4:"name";s:10:"Member Lib";s:11:"description";s:10:"Member Lib";s:7:"setting";a:0:{}s:6:"saving";b:1;s:8:"trial_on";i:0;s:10:"trial_cost";d:0;s:14:"trial_duration";i:0;s:19:"trial_duration_type";s:1:"d";s:16:"trial_num_cycles";i:0;s:8:"duration";i:1;s:13:"duration_type";s:1:"l";s:6:"amount";d:0;s:8:"currency";s:3:"USD";s:9:"join_date";i:1383183050;s:13:"last_pay_date";s:0:"";s:11:"expire_date";s:0:"";s:15:"membership_type";s:4:"free";s:6:"status";s:6:"Active";s:12:"payment_type";s:0:"";s:13:"autoresponder";s:10:"mgm_aweber";s:10:"subscribed";s:1:"Y";s:22:"autoresponder_notified";s:1:"Y";s:13:"user_password";s:16:"Lwp9E8RZECyB9bzb";s:17:"active_num_cycles";i:1;s:7:"pack_id";s:2:"22";s:12:"account_desc";s:12:"Free Account";s:16:"hide_old_content";s:1:"0";} ) [12] => Array ( [0] => 2579 [umeta_id] => 2579 [1] => 185 [user_id] => 185 [2] => _wpsc_customer_profile [meta_key] => _wpsc_customer_profile [3] => a:5:{s:16:"shipping_country";s:2:"US";s:15:"billing_country";s:2:"US";s:15:"shipping_region";s:2:"22";s:14:"billing_region";s:2:"22";s:4:"cart";s:1056:"O:9:"wpsc_cart":37:{s:16:"delivery_country";s:2:"US";s:16:"selected_country";s:2:"US";s:15:"delivery_region";s:2:"22";s:15:"selected_region";s:2:"22";s:24:"selected_shipping_method";N;s:24:"selected_shipping_option";N;s:24:"selected_shipping_amount";N;s:6:"coupon";N;s:14:"tax_percentage";N;s:9:"unique_id";s:40:"905310a45d51d75129b9de961ae17f69a7129ced";s:6:"errors";a:0:{}s:9:"total_tax";N;s:13:"base_shipping";N;s:19:"total_item_shipping";N;s:14:"total_shipping";N;s:8:"subtotal";N;s:11:"total_price";N;s:13:"uses_shipping";N;s:13:"is_incomplete";b:1;s:10:"cart_items";a:0:{}s:9:"cart_item";N;s:15:"cart_item_count";i:0;s:17:"current_cart_item";i:-1;s:11:"in_the_loop";b:0;s:16:"shipping_methods";b:0;s:15:"shipping_method";N;s:21:"shipping_method_count";i:1;s:23:"current_shipping_method";i:-1;s:18:"in_the_method_loop";b:0;s:15:"shipping_quotes";a:0:{}s:14:"shipping_quote";N;s:20:"shipping_quote_count";i:0;s:22:"current_shipping_quote";i:-1;s:17:"in_the_quote_loop";b:0;s:12:"coupons_name";s:0:"";s:14:"coupons_amount";i:0;s:15:"shipping_option";N;}";} [meta_value] => a:5:{s:16:"shipping_country";s:2:"US";s:15:"billing_country";s:2:"US";s:15:"shipping_region";s:2:"22";s:14:"billing_region";s:2:"22";s:4:"cart";s:1056:"O:9:"wpsc_cart":37:{s:16:"delivery_country";s:2:"US";s:16:"selected_country";s:2:"US";s:15:"delivery_region";s:2:"22";s:15:"selected_region";s:2:"22";s:24:"selected_shipping_method";N;s:24:"selected_shipping_option";N;s:24:"selected_shipping_amount";N;s:6:"coupon";N;s:14:"tax_percentage";N;s:9:"unique_id";s:40:"905310a45d51d75129b9de961ae17f69a7129ced";s:6:"errors";a:0:{}s:9:"total_tax";N;s:13:"base_shipping";N;s:19:"total_item_shipping";N;s:14:"total_shipping";N;s:8:"subtotal";N;s:11:"total_price";N;s:13:"uses_shipping";N;s:13:"is_incomplete";b:1;s:10:"cart_items";a:0:{}s:9:"cart_item";N;s:15:"cart_item_count";i:0;s:17:"current_cart_item";i:-1;s:11:"in_the_loop";b:0;s:16:"shipping_methods";b:0;s:15:"shipping_method";N;s:21:"shipping_method_count";i:1;s:23:"current_shipping_method";i:-1;s:18:"in_the_method_loop";b:0;s:15:"shipping_quotes";a:0:{}s:14:"shipping_quote";N;s:20:"shipping_quote_count";i:0;s:22:"current_shipping_quote";i:-1;s:17:"in_the_quote_loop";b:0;s:12:"coupons_name";s:0:"";s:14:"coupons_amount";i:0;s:15:"shipping_option";N;}";} ) [13] => Array ( [0] => 2580 [umeta_id] => 2580 [1] => 185 [user_id] => 185 [2] => purchased_instances [meta_key] => purchased_instances [3] => Cardio Stretch/Flex on 11/04/2013 8:00 AM [meta_value] => Cardio Stretch/Flex on 11/04/2013 8:00 AM )
==============================================================================
New working code:
echo '<table border=1><tr><td>Full Name</td><td>Class Purchased</td></tr>';
//Build Student Info1 table
foreach($userID as $val) {
$user_id= $val;
$queryA="SELECT meta_value FROM wp_usermeta WHERE meta_key ='first_name' AND user_id ='$user_id'";
$queryB="SELECT meta_value FROM wp_usermeta WHERE meta_key ='last_name' AND user_id ='$user_id'";
$queryC="SELECT meta_value FROM wp_usermeta WHERE meta_key ='purchased_instances' AND user_id ='$user_id'";
$resultA = $link->query($queryA)or die($link->error);
$resultB = $link->query($queryB)or die($link->error);
$resultC = $link->query($queryC)or die($link->error);
$numPurchases = $resultC->num_rows;
if($numPurchases > 0){
$i=0;
foreach($resultA as $val){
$fName[$i]=$val['meta_value'];;
$i++;
}
$i=0;
foreach($resultB as $val){
$lName[$i]=$val['meta_value'];
$i++;
}
$i=0;
foreach($resultC as $val){
$classDetails[$i]=$val['meta_value'];
$i++;
}
for($i=0; $i < $numPurchases; $i++){
echo '<td>';
print_r($fName[$i]);
echo ' ';
print_r($lName[$i]);
echo '</td><td>';
print_r($classDetails[$i]);
echo '</td></tr>';
}
/* free result set */
mysqli_free_result($resultA);
mysqli_free_result($resultB);
mysqli_free_result($resultC);
}
}
echo '';
Some important notes:
Validate any user input
NEVER execute SQL in a loop
Use one query that fetches everything you need (you're using the same table)
Fetch an associative array, this will help you a lot
Specify exactly what you need in your projection (not * after SELECT, exactly state each field to fetch like SELECT foo, bar, …)
Here is some code to help you get started with the above tips:
<?php
// Validate input, please note that prepared statements would be best.
// Also note that we don't need mysql escape calls because we validate
// that this is an actual integer value which can't contain anything
// dangerous. If we'd be working with any kind of string, date, ... the
// world would look different.
if (!is_array($userID)) {
if (!is_int($userID)) {
throw new \RuntimeException;
}
$userIds = "= {$userID}";
}
else {
$userIds = null;
$c = count($userID);
for ($i = 0; $i < $c; ++$i) {
if (!is_int($userID[$i])) {
throw new \RuntimeException;
}
$userIds .= $userIds ? "," : "IN(";
$userIds .= $userID[$i];
}
$userIds .= ")";
}
// One query to rule them all and nice formatting for readability.
$result = $link->query(
"SELECT *
FROM `wp_usermeta`
WHERE `meta_key` = 'purchased_instances'
AND `user_id` {$userIds}"
);
// Now we fetch everything within a single loop.
while ($row = mysqli_fetch_assoc($result)) {
print_r($row);
}
// That's it!
mysqli_free_result($result);
?>