Array
(
[0] => Array
(
[price_id] => 1
[website_id] => 0
[all_groups] => 1
[cust_group] => 32000
[price_qty] => 2
[price] => 90.0000
)
[1] => Array
(
[price_id] => 2
[website_id] => 0
[all_groups] => 1
[cust_group] => 32000
[price_qty] => 5
[price] => 80.0000
)
.......
)
the array element maybe one or two or more, if i want to pass [price_qty] and [price] value to the jquery code. how should i do? could someone make me an example. thank you
you should consider using JSON strings in order to use key based arrays in JavaScript.
http://php.net/json
json_encode your php arrays to json
Use json_encode to convert your php array to json :)
A possible solution is to convert php array structures into JSON before passing the data to the client.
Have a look at php json.
And also have a look at this post.
Try with Json:
json_encode($array);
This will encode the array into a json object, that is friendly with javascript (and Jquery).
If you are passing it through an ajax request just echo it in the php and return that as response.
If it's in the same script you could do:
$object = json_encode($array);
echo "var myObject = $object;";
And for accessing the information in javascript/jquery you would do:
alert(myObject[0].price_id);
you use myObject[0] to access positions as in php arrays, and myObject[0].name to access what would be associative array key in an array.
For more information visit json documentation page
Well while you ask to move from [php]array to [javascript]array, this is how to. You should ue json_encode how the previous answer said, but in you javascript you can use the following code to convert a json into an array:
function json2Array = function(json){
var a = [];
for(var o in json){
a.push(json[o]);
}
return a;
}
var myArray = json2Array(youPhpJsonEncode);
and you will have your array in javascript
Related
Hello i am new with Json in php. I have a web service that gives me data in json format.
I take this data making decode put when i try to use this data i cant
Here is my code:
$url = "http://www.webinsurer.gr/....;
$json = json_decode(#file_get_contents($url), true);
and if i make debug i see the data i take :
[file] => C:\xampp\htdocs\development.insurancemarket.gr\mvc\protected\models\Ratingsmail.php
[line] => 18
[data] => Array
(
[0] => Array
(
[POL_EXPIREDATE] => 2014-05-19 12:00:00
[INCO_IWCODE] => 41
[INCO_DESC] => MAPFRE ASISTENCIA
[PACK_IWCODE] => 0
[PACK_DESC] =>
[OFFERCODE] =>
[PAYMENTCODE] =>
)
[1] => Array
(.....
But i dont now how to use that data. when i try this :
$b= $json->{1}->{'INCO_IWCODE'};
Debug::debuger($b);
the result is nothing
what is wrong? sorry for long post.
When setting the second argument on json_decode to true, you are actively asking for the data to be returned in an associative array and not objects. Thats why your code didn't work.
Demo
You are converting json to associative array. You need to use;
$b = $json["data"][1]["INCO_IWCODE"];
$a = $json[0]->INCO_IWCODE;
That worked for my guys. thanks you all!!!
I am using jQuery to convert form data to serialized form using:
var data = $('#frm').serialize();
In php I get this:
fiscalyear_id=4&category=Category+A&isgraph=on&Title=a&Value=a&Title=b&Value=b&category=Category+B&Title=c&Value=c&Title=d&Value=d&category=Category+C&Title=e&Value=e&Title=f&Value=f&data;=&csrf_check=9c288285b379701b27c3836091c00b04
And when I do:
parse_str($_POST['data'], $data);
pretty_print($data);
I get:
Array
(
[fiscalyear_id] => 4
[category] => Category C
[isgraph] => on
[Title] => f
[Value] => f
[data;] =>
[csrf_check] => 9c288285b379701b27c3836091c00b04
)
As can be seen, not all paramters are coming in array above. Does anyone have an idea what I am doing wrong ? Thanks for the help
parse_str parses the string in variable and you are getting it into array.
but duplicate array keys are not possible,
hence you are not getting all the values because they have the same key!
I'm trying to print a value from a object array, but I'm facing some problems. I want to echo in php the value of [title] but i am not getting it!
stdClass Object (
[result] => success
[records] => stdClass Object (
[500272328] => stdClass Object (
[nif] => 500272328
[pc4] => 2775
[pc3] => 372
[seo_url] => solnave-restaurantes-e-alimentacao-s-a
[title] => Solnave - Restaurantes e Alimentação S.a
[city] => Cascais
[racius] => http://www.racius.com/solnave-restaurantes-e-alimentacao-s-a/
[portugalio] => http://www.portugalio.com/solnave-restaurantes-e-alimentacao-sa-2/
)
)
)
How can i retrieve?
Thank you!
I am assuming you are calling json_decode to decode the JSON string.
You should call it as json_decode($thestring_to_be_decoded, true). This will convert the objects to associative arrays and you will be able to access the title field.
Your stdClass is a PHP object or a Javascript encoded object?
It seems a PHP, so to echo title element you must navigate at there:
echo $yourobjarray->records->500272328->title;
or
echo $yourobjarray->records->500272328['title'];
(not evident in your question)
But to echo into <script> section of a HTML page,
<script> ...
var x='<?= $yourobjarray->records->500272328['title'];?>';
...</script>
and, if a AJAX context, to JSON response you need
echo json_encode($x);
where $x is your array or the title string, or what you want.
You can make use of a foreach construct to loop through your JSON decoded object array or If you want to print individual elements you can simply do like this.
echo $yourobjarray->result; //"prints" success
I am having an issue with PHP to JavaScript and then sorting. I have the following JS script
function sortby(param, data) {
switch (param) {
case "aplha":
console.log(data);
data.sort();
break;
}
}
The array this is passing is through json_encode and the array looks like so
Array ( [0] => Array ( [Name] => 123456 [Clean_Name] => 123456 [CreateDate] => 1372479841 ) [1] => Array ( [Name] => 123456 [Clean_Name] => 123456 [CreateDate] => 1372479841 ) )
However I get the above error when I try to pass it as data.sort(). Any ideas?
PHP arrays aren't js arrays, but JSON objects, so you can't have and array on your js code. However, there's a workaround, refer to this answer for more info.
Cheers
I think i found my issue however i dont know how to fix it. when i pass the variable with json_encode to the javascript function it passing it as a string so data[0] == [ ... what am i missing here
ALRIGHT wow i found my issue i am so sorry guys i am so dumb
<script>sortby('aplha', '<?=json_encode($teamList);?>');</script>
That was my old this is my new
<script>sortby('aplha', <?=json_encode($teamList);?>);</script>
It was the ' that was passing it incorrectly ... it works fine now I hit my desk so hard ...
I have a PHP array that I want to pass to the jQuery function that called it. However when I tried to retrieve the value of 'lat' the way i did below, I get the error Cannot read property 'lat' of null, obviously because I dont know how to access a multidimensional JSON array. Can anyone show me how?
PHP Array
Array
(
[0] => Array
(
[price] => 1600
[bedroom] => 1
[lat] => -71.119385
[lng] => 42.373917
[distance] => 6.65195429565453
)
[1] => Array
(
[price] => 1800
[bedroom] => 1
[lat] => -71.104248
[lng] => 42.368172
[distance] => 4.957829810472103
)
}
This gets encoded into
JSON
[{"price":"1600","bedroom":"1","lat":"-71.119385","lng":"42.373917","distance":"6.65195429565453"},{"price":"1800","bedroom":"1","lat":"-71.104248","lng":"42.368172","distance":"4.957829810472103"}]
jQuery
$(function() {
$("#search_button").click(function(e){
e.preventDefault();
var search_location = $("#search_location").val();
$.getJSON('index.php/main/get_places', {search_location: search_location}, function(json){
$("#result").html(json.lat);
console.log(json.lat);
});
});
});
json is an array, so it won't have the property lat.
Try:
json[0].lat
To get the first object's lat property, for example.
$.getJSON('index.php/main/get_places', {search_location: search_location}, function(json){
$.each(json, function(key, val) {
console.log(val.lat);
});
});
Your json is returning as an array of objects, so you need to reference the scalar first. Change your references to json.lat into json[0].lat.
Then, if you need to you can write a for loop and reference it as json[i].lat, assuming that i is your iterator variable.
json is an array of objects, just as it is in your php code.
So there are two lat values:
json[0].lat // and
json[1].lat
The json variable itself is null, as stated by the error message. All the answers about accessing subindexes of json or iterating over it like an array will fail because of this.
Look at the documentation for jQuery's getJSON and parseJSON. getJSON passes the server's response through parseJSON to convert it to a Javascript object. parseJSON returns null "if you pass in nothing, an empty string, null, or undefined."
I would use your browser's debugger to look at the raw http response from the server, because that is likely the culprit.
http://api.jquery.com/jQuery.getJSON/
http://api.jquery.com/jQuery.parseJSON/