You do not have to call parseJSON since the output of json_decode is a javascript literal. Just assign it to a variable.
var tempArray = <?php echo json_encode($php_array); ?>;
You should be able then to access the properties as
alert(tempArray[0].Key);
Answer from Orangepill on Stack OverflowYou do not have to call parseJSON since the output of json_decode is a javascript literal. Just assign it to a variable.
var tempArray = <?php echo json_encode($php_array); ?>;
You should be able then to access the properties as
alert(tempArray[0].Key);
This worked for me.
<script type='text/javascript'>
<?php
$php_array = array(
array("casa1", "abc", "123"),
array("casa2", "def", "456"),
array("casa3", "ghi", "789" )
);
$js_array = json_encode($php_array);
echo "var casas = ". $js_array . ";\n";
?>
alert(casas[0][1]);
</script>
I know this is old but you could try the following piece of code:
$array = json_decode(json_encode($object), true);
where $object is the response of the API.
You can use recursive function like below:
function object_to_array($obj, &$arr)
{
if (!is_object($obj) && !is_array($obj))
{
$arr = $obj;
return $arr;
}
foreach (
key => $value)
{
if (!empty($value))
{
key] = array();
objToArray($value,
key]);
}
else {
key] = $value;}
}
return $arr;
}
You want $series to be an associative array:
$series = array();
$series['sample1'] = array('color' => 'Green');
$series['sample2'] = array('color' => 'DarkBlue');
$series['sample3'] = array('color' => 'DarkOrange');
Or
$series = array(
'sample1' => array('color' => 'Green'),
'sample2' => array('color' => 'DarkBlue'),
'sample3' => array('color' => 'DarkOrange')
);
Prepare array to send from php to javascript:
echo json_encode( $array );
Translate the data in javascript to a nice object
var a = JSON.parse( phpdata );
Prep to send data to php from javascript
var tosend = JSON.stringify( a );
Translate incoming js object back into php
$a = json_decode( $some_json );
or
$a = json_decode( $some_json, true );
In your code,
$array3 = array(array1, array2);
here array1 and array2 considered as string without quotes, so you will not get your answer. This should be PHP variable. You have to include $ sign in this.
$array3 = array($array1, $array2);
$array3 = array(array1, array2); should be $array3 = array($array1, $array2);
You do not have to call JSON.parse since the output of json_decode is a javascript literal. Just assign it to a variable.
var yourArray = <?php echo json_encode($array); ?>;
You can access property like this
alert(yourArray[0].Key);
You can try use:
PHP:
$array = [
"label" => [
"january",
"february"
],
"data" => [
0,
1
]
];
$response = new Response(json_encode($array));
$response->headers->set('Content-Type', 'application/json');
return $response;
JavaScript:
var data = JSON.parse(response);
console.log(data.label)
Hope help you.
You cannot use an integer as a variable name, like in this line: print "var 25[".$i."]=" .$result1['25'].";\n";. 25 cannot be a variable.
If you want to map an array to a javascript object, you might want to take a look at json_encode
EXAMPLE
Your code could be written like this:
<?php
$result = array();
while ($row = mysql_fetch_array($query1)){
$result[] = $row;
}
?>
<script>
var result = <?= json_encode($result); ?>;
alert (result[1][500]);
</script>
looks much cleaner to me.
The way you are working with arrays is not correct.
First you should initialize the array:
var myArr = [];
Then if you just want to add to the array, you can use push:
myArr.push("something");
or to a specific index:
myArr[11] = "something";
The syntax you are using is completely invalid.
$jsArray = array();
foreach($myArray as $array) {
$jsArray[] = array($array['Focus_Date'], (int) $array['WorkHrs']);
}
echo json_encode($jsArray);
echo json_encode(array_map(array_values, $arr));
EDIT: To get it in the specified order:
function to_focus_work_array($arr)
{
return array($arr['Focus_Date'], $arr['WorkHrs']);
}
echo json_encode(array_map('to_focus_work_array', $arr));
Your object's properties are private. If you var_dump() an object as you did it prints also private properties.
Have a look at this article http://php.net/manual/en/language.oop5.iterations.php. You can iterate object as you do but it iterates only public properties.
First of all, we can't see the scope. All your code is private (object properties and the method). Show the scope and we can help you better.
The problem seems to be here:
if (!empty($value))
{
$arr[$key] = array();
objToArray($value, $arr[$key]);
}
You are passing a second argument that doesn't defined in the method
private function objToArray($obj){...}
So you are passing a value to objToArray(), that value is a string I guess, and when it reach this line:
$arr = array();
if(!is_object($obj) && !is_array($obj)){
$arr = $obj;
die("invalid data");
}
Then it will display "invalid data", and empty your array.
A quick way to do this is:
$obj = json_decode(json_encode($array));
Explanation
json_encode($array) will convert the entire multi-dimensional array to a JSON string. (php.net/json_encode)
json_decode($string) will convert the JSON string to a stdClass object. If you pass in TRUE as a second argument to json_decode, you'll get an associative array back. (php.net/json_decode)
I don't think the performance here vs recursively going through the array and converting everything is very noticeable, although I'd like to see some benchmarks of this. It works, and it's not going to go away.
The best way would be to manage your data structure as an object from the start if you have the ability:
$a = (object) array( ... ); $a->prop = $value; //and so on
But the quickest way would be the approach supplied by @CharlieS, using json_decode(json_encode($a)).
You could also run the array through a recursive function to accomplish the same. I have not benchmarked this against the json approach but:
function convert_array_to_obj_recursive($a) {
if (is_array($a) ) {
foreach($a as $k => $v) {
if (is_integer($k)) {
// only need this if you want to keep the array indexes separate
// from the object notation: eg. $o->{1}
$a['index'][$k] = convert_array_to_obj_recursive($v);
}
else {
$a[$k] = convert_array_to_obj_recursive($v);
}
}
return (object) $a;
}
// else maintain the type of $a
return $a;
}
Hope that helps.
EDIT: json_encode + json_decode will create an object as desired. But, if the array was numerical or mixed indexes (eg. array('a', 'b', 'foo'=>'bar') ), you will not be able to reference the numerical indexes with object notation (eg. $o->1 or $o[1]). the above function places all the numerical indexes into the 'index' property, which is itself a numerical array. so, you would then be able to do $o->index[1]. This keeps the distinction of a converted array from a created object and leaves the option to merge objects that may have numerical properties.