Trouble with displaying JSON array - php

could you please help me figure out why I'm getting "undefined" instead of the value.
As it is plain to see I'm having trouble getting the data from the array, in firebug I get this as a response....
{"status":"success",
"response":[
{"email":
{"email":"xxxxx#iing.mxl.uabc.mx",
"valid":"1",
"reason":null,
"confirmed_at":"0000-00-00 00:00:00",
"contact_email":"1",
"login_email":"1",
"users_id":"6375"},
"history":[
{"contactRole":"Non Classified Lead with History",
"contactProject":"2082",
"contactBrand":"B"},
{"contactRole":"co Author",
"contactProject":"32",
"contactBrand":"B"},
{"contactRole":"co Author",
"contactProject":"176",
"contactBrand":"B"},
{"contactRole":"co Author",
"contactProject":"582",
"contactBrand":"B"},
{"contactRole":"co Author",
"contactProject":"1858",
"contactBrand":"B"},
{"contactRole":"Author",
"contactProject":"12",
"contactBrand":"J"},
{"contactRole":"Editor",
"contactProject":"176",
"contactBrand":"B"}]},
{"email":
{"email":"xxxxx#hotmail.com",
"valid":"1",
"reason":null,
"confirmed_at":"0000-00-00 00:00:00",
"contact_email":"0",
"login_email":"0",
"users_id":"6375"},
"history":[]}]}
this is the code referring to my javascript file
http://pastebin.com/gPaEAKim
snapshot of the view that I'm getting.
Just to be on the safe side...this is the way the array looks like when I debug it from the controller
Array
(
[status] => success
[response] => Array
(
[0] => Array
(
[email] => Array
(
[email] => xxxxx#iing.mxl.uabc.mx
[valid] => 1
[reason] =>
[confirmed_at] => 0000-00-00 00:00:00
[contact_email] => 1
[login_email] => 1
[users_id] => 6375
)
[history] => Array
(
[0] => Array
(
[contactRole] => Non Classified Lead with History
[contactProject] => 2082
[contactBrand] => B
)
[1] => Array
(
[contactRole] => co Author
[contactProject] => 32
[contactBrand] => B
)
[2] => Array
(
[contactRole] => co Author
[contactProject] => 176
[contactBrand] => B
)
[3] => Array
(
[contactRole] => co Author
[contactProject] => 582
[contactBrand] => B
)
[4] => Array
(
[contactRole] => co Author
[contactProject] => 1858
[contactBrand] => B
)
[5] => Array
(
[contactRole] => Author
[contactProject] => 12
[contactBrand] => J
)
[6] => Array
(
[contactRole] => Editor
[contactProject] => 176
[contactBrand] => B
)
)
)
[1] => Array
(
[email] => Array
(
[email] => xxxxxxx#hotmail.com
[valid] => 1
[reason] =>
[confirmed_at] => 0000-00-00 00:00:00
[contact_email] => 0
[login_email] => 0
[users_id] => 6375
)
[history] => Array
(
)
)
)
)

If your data var in your JS code is the entirety of the JSON response, then you're working one level too high, and the inner loop is working 2 levels too high:
$.each(data['response'], function(i, email) {
^^^^^^^^^^^^--- missing this
$.each(email, function(ii, ...)) {
as your code stands now, the inner loop's email is overwriten your outer loop's as well.

Related

Parse JSON with PHP using foreach

I've managed to decode and echo a JSON feed. After running this command
print_r(json_decode($data,true));
this is what I see on the screen:
Array
(
[sportId] => 29
[last] => 96466864
[league] => Array
(
[0] => Array
(
[id] => 1980
[events] => Array
(
[0] => Array
(
[id] => 667177156
[starts] => 2016-11-26T15:00:00Z
[home] => Hull City
[away] => W.B.A
[rotNum] => 2504
[liveStatus] => 1
[status] => O
[parlayRestriction] => 2
)
[1] => Array
(
[id] => 672139467
[starts] => 2016-12-10T15:00:00Z
[home] => Hull City
[away] => Crystal Palace
[rotNum] => 2510
[liveStatus] => 1
[status] => O
[parlayRestriction] => 2
)
[2] => Array
(
[id] => 676973849
[starts] => 2016-12-26T15:00:00Z
[home] => Burnley
[away] => Middlesbrough
[rotNum] => 2519
[liveStatus] => 1
[status] => O
[parlayRestriction] => 2
)
)
)
)
)
I need to be able to use foreach to go through each [events] in this associative array, and to be able to get the result such as this:
Hull City v W.B.A.
Hull City v Crystal Palace
Burnley v Middlesbrough
I think everything is already parsed correctly and now it's just a matter of using the correct syntax to echo the result from the associative array, which I cannot do myself.
You can try this:
$data=json_decode($data,true);//converts in array
foreach($data['league'] as $key=>$val){// this can be ommited if only 0 index is there after
//league and $data['league'][0]['events'] can be used in below foreach instead of $val['events'].
foreach($val['events'] as $keys=>$value){
echo $value['home'].' v '.$value['away'].'<br>;
}
}
Try like this..
$data=json_decode($data,true);//convert your json into array
$events = $data['leage'][0]['events'];//events array
foreach($events as $key=>$value)//loop inside your events array
{
echo $value['home'].' v '.$value['away'].'<br>;
}

Insert into mysql data

How will I insert into mysql using php if my data comes like this?
array(
[template] => template2
[quote] => 001
[date] => 01 December 2016
[referenceno] =>
[q_cust_name] => 0
[q_cat_name] => Array
(
[0] => drugs
[1] => oil
)
[p_name] => Array
(
[0] => Paracetamol
[1] => liquid nitrogen
)
[quot_per_unit] => Array
(
[0] => 600/gram
[1] => 500/liter
)
[quot_des] => Array
(
[0] => Cures fever. Use only in case of high fever
[1] => Hazardous Product.
)
[quot_price] => Array
(
[0] => 150.00
[1] => 7500.00
)
[quot_req_qty] => Array
(
[0] => 10
[1] => 10
)
)
$x="INSERT INTO quotation (user_id, quote, date, referenceno, q_cust_name, q_cat_name, p_name, quot_per_unit, quot_req_qty, quot_price, quot_dis, quot_des, incident_charge, tax, quote_total, status) VALUES";
for($i=0;$i<count($_REQUEST['q_cat_name']);$i++)
{ $x.="('', '".$_REQUEST['quote']."', '".$_REQUEST['date']."', '".$_REQUEST['referenceno']."', '".$_REQUEST['q_cust_name']."', '".$_REQUEST['q_cat_name'][$i]."', '".$_REQUEST['p_name'][$i]."', '".$_REQUEST['quot_per_unit'][$i]."', '".$_REQUEST['quot_req_qty'][$i]."', '".$_REQUEST['quot_price'][$i]."', '".$_REQUEST['discount']."', '".$_REQUEST['quot_des'][$i]."', '".$_REQUEST['incident_charge']."', '".$_REQUEST['tax']."', '".$_REQUEST['quote_total']."', 'draft'),";
}
echo $x;

How to split array data returned from json string in php

I have json return string like given below. I want to extract cancellation Policy list of objects like cutoff Time and refund In Percentage. I tried using for-loop but it didn't help me. Can you please help me on extracting this.
Array (
[apiStatus] => Array ( [success] => 1 [message] => SUCCESS ) <br>
[apiAvailableBuses] => Array ( <br>
[0] => Array ( [droppingPoints] => [availableSeats] => 41 <br>[partialCancellationAllowed] => [arrivalTime] => 08:00 AM <br>
[cancellationPolicy] => [<br>
{"cutoffTime":"1","refundInPercentage":"10"},<br>
{"cutoffTime":"2","refundInPercentage":"50"},<br>
{"cutoffTime":"4","refundInPercentage":"90"}<br>
] <br>
[boardingPoints] => Array ( [0] => Array ( [time] => 09:00PM [location] => Ameerpet,|Jeans Corner 9687452130 [id] => 6 ) [1] => Array ( [time] => 09:15PM [location] => S.R Nagar,S.R Nagar [id] => 2224 ) [2] => Array ( [time] => 09:10PM [location] => Kondapur,Toyota Show room [id] => 2244 ) ) [operatorName] => Deepak Travels [departureTime] => 9:00 PM [mTicketAllowed] => [idProofRequired] => [serviceId] => 6622 [fare] => 800 [busType] => 2+1 Hi-Tech Non A/c [routeScheduleId] => 6622 [commPCT] => 0 [operatorId] => 213 [inventoryType] => 0 ) <br>
[1] => Array ( [droppingPoints] => [availableSeats] => 41 [partialCancellationAllowed] => [arrivalTime] => 07:00 AM <br>
[cancellationPolicy] => [<br>
{"cutoffTime":"1","refundInPercentage":"10"},<br>
{"cutoffTime":"2","refundInPercentage":"50"},<br>
{"cutoffTime":"4","refundInPercentage":"90"}<br>
] <br>
[boardingPoints] => Array ( [0] => Array ( [time] => 09:10PM [location] => Ameerpet,|Jeans Corner [id] => 6 ) [1] => Array ( [time] => 09:00PM [location] => S.R Nagar,S.R Nagar [id] => 2224 ) [2] => Array ( [time] => 08:30PM [location] => KUKATPALLY,JNTU [id] => 2230 ) ) [operatorName] => Dhanunjayabus [departureTime] => 9:00 PM [mTicketAllowed] => [idProofRequired] => [serviceId] => 6743 [fare] => 900 [busType] => VOLVO [routeScheduleId] => 6743 [commPCT] => 0 [operatorId] => 233 [inventoryType] => 0 )
)
)
Use a foreach() for it like so:
foreach ($your_response['apiAvailableBuses'] as $el) {
$cancellationPolicy[] = $el['cancellationPolicy'];
}
Try this:
foreach($data['apiStatus']['apiAvailableBuses'] as $item) {
foreach($item['cancellationPolicy'] as $key => $json) {
$jsonDecoded = json_decode($json, true);
// And you will have access to the data like this
// $jsonDecoded['cutoffTime'];
// $jsonDecoded['refundInPercentage'];
}
}
$response = json_decode($apiResponse);
$cancellationPolicies = [];
foreach ($response->apiAvailableBuses as $availableBus) {
$cancellationPolicies[] = $availableBus['cancellationPolicy'];
// if you want to display something you can simply do it like this;
echo $availableBus['cancellationPolicy']->cutoffTime;
}

decode json response to get json parameter value

Here is my json response:
stdClass Object ( [is_claimed] => [rating] => 4 [mobile_url] => http://m.yelp.com/biz/the-waterboy-sacramento [rating_img_url] => http://s3-media4.ak.yelpcdn.com/assets/2/www/img/c2f3dd9799a5/ico/stars/v1/stars_4.png [review_count] => 455 [name] => The Waterboy [snippet_image_url] => http://s3-media1.ak.yelpcdn.com/photo/SZyFYvQmHWSLhK96SSzwwA/ms.jpg [rating_img_url_small] => http://s3-media4.ak.yelpcdn.com/assets/2/www/img/f62a5be2f902/ico/stars/v1/stars_small_4.png [url] => http://www.yelp.com/biz/the-waterboy-sacramento [menu_date_updated] => 1387494198 [reviews] => Array ( [0] => stdClass Object ( [rating] => 5 [excerpt] => AMAZING again, went here last Thursday at 5:00pm. Greeted by friendly man, he asked if we had a reservation, I said, 'no, do we need one?' He said, I'm... [time_created] => 1395158338 [rating_image_url] => http://s3-media1.ak.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png [rating_image_small_url] => http://s3-media1.ak.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png [user] => stdClass Object ( [image_url] => http://s3-media1.ak.yelpcdn.com/photo/SZyFYvQmHWSLhK96SSzwwA/ms.jpg [id] => H0qUqWctz5Ms6qdeaIvjFw [name] => Lori T. ) [rating_image_large_url] => http://s3-media3.ak.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png [id] => bZznirkpADmW3Qcpc5u_VA ) ) [phone] => 9164989891 [snippet_text] => AMAZING again, went here last Thursday at 5:00pm. Greeted by friendly man, he asked if we had a reservation, I said, 'no, do we need one?' He said, I'm... [image_url] => http://s3-media1.ak.yelpcdn.com/bphoto/9e5sodvvP3p6_53wOqVTcg/ms.jpg [categories] => Array ( [0] => Array ( [0] => French [1] => french ) [1] => Array ( [0] => Italian [1] => italian ) ) [display_phone] => +1-916-498-9891 [rating_img_url_large] => http://s3-media2.ak.yelpcdn.com/assets/2/www/img/ccf2b76faa2c/ico/stars/v1/stars_large_4.png [menu_provider] => single_platform [id] => the-waterboy-sacramento [is_closed] => [location] => stdClass Object ( [city] => Sacramento [display_address] => Array ( [0] => 2000 Capitol Ave [1] => Midtown [2] => Sacramento, CA 95811 ) [neighborhoods] => Array ( [0] => Midtown ) [postal_code] => 95811 [country_code] => US [address] => Array ( [0] => 2000 Capitol Ave ) [state_code] => CA ) )
which I get using:
$response = json_decode($data);
print_r($response);
echo $response["rating"]; //Why this does not give json response value?
Pass true into the second parameter of json_decode to return an array (which is how you're trying to access it):
$response = json_decode($data, true); // associative array returned
print_r($response);
echo $response["rating"]; // 4
Manual
Otherwise, access the rating as a property of the object that you've got:
$response = json_decode($data);
echo $response->rating; // 4

how to display individual values?

I get this from print_r($data):
Array (
[0] => Array (
[0] => stdClass Object (
[cto_office_id] => 1
[office_name] => Airtel India
[address_line1] => Bendoor well
[phone_office1] => 12345678912
[phone_office2] => 789451234512
[address_line2] => Kankanady Circle
[city] => Mangluru
[state] => Haryana
[pin_code] => 002
[country] => India
[web_url] => airte.co.in
[office_quote] => Office
[date_registered] => 2011-11-24 05:59:51
[bill_mail_id] => 15612561
[bill_mail_id_type] =>
[acc_status] => enabled
[comments] =>
[is_account_deleted] => 0
[account_deleted_date] => 0000-00-00 00:00:00
)
)
)
I tried this $data['phone_office1'], but that does not work.
You have an object that is nested inside two arrays. You can get it out like this:
$data[0][0]->phone_office1

Categories