How to pass array from php to javascript using smarty 3? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
In php file
When I code like this
$names = ["jim", "lucy"];
$smarty->assign('names', $names);
In javascript,
var arr = {$names};
Smarty will parse the {$names} as
...&quot... .
What should I do to avoid this? I want Smarty parse the array as
var arr = ['jim', 'lucy'];

In php:
$names = ['jim', 'lucy'];
$smarty->assign('names', $names);
In javascript,
var arr = {$names|json_encode};
Notice:
If you changed your smarty default_modifiers to array('escape:"html"'), use
var arr = {$names|json_encode nofilter};
to make everything work.
Good luck.

Related

How to parse object array that has been stringified using backslashes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
I have been searching for the correct regex for converting this string to JSON. It works if there is a single object in the array, but I believe the comma is messing it up somehow.
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
const obj = JSON.parse(json.replace(/("[^"]*"\s*:\s*)(\d{17,})/g, '$1"$2"'));
console.log(obj);
There was nothing wrong except that the list of objects was not contained within square brackets
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
console.log(JSON.parse(`[${json}]`))

Javascript convert [[ ]] to [] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Convert
var a = [['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']]
to
var a= ['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']
I dont want two square brackets in front and end.
I want output
a[2] = 'a123'
The best way to do is just assigning the first value to the array:
var a = [['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']];
a = a[0];
console.log(a);
But the right way of dealing it should be making sure that the endpoint or whatever that outputs should output correctly.
You can use Array.prototype.flat().
console.log([['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']].flat());
This solution works if it's extremely nested too:
console.log([
['12ae11ee12-1bhb222'],
['2019-10-10T19:46.19.632z', 'a123']
].flat());

How can I assign variable from json? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
So my codes are like below;
$.getJSON('https://api.myjson.com/bins/gxs81',function(data){
$.each(data.employees,function(i,emp){
$('ul').append('<li>'+emp.firstName+' '+emp.lastName+'</li>');
});
I have multiple objects in json file, how can I assign them in an array? e.g.
array[2][0]=*firstName field should be here*
array[2][1]=*lastname field should be here*
Here's a simple push() to build a new array from your JSON:
$.getJSON('https://api.myjson.com/bins/gxs81',function(data){
var current = '';
var myarray = [];
$.each(data.employees,function(i,emp){
current = emp.firstName+' '+emp.lastName;
$('ul').append('<li>'+current+'</li>');
myarray.push(current);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul></ul>

how to make dynamic javascript array with key and value group [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am integrating jqbarGraph, the demo worked fine. I want to make the values dynamic.
this is my json response
{"success":false,"message":{"12":7887,"11":159}}
I need an array like this
graphByMonth = new Array(
[7887,12],
[159,11]
);
Need to create dynamic array from JSON data.
Does this work?
var graphByMonth = [],
jsonResponse = {"success":false,"message":{"12":7887,"11":159}},
data = jsonResponse.message;
Object.keys(data).forEach(function (k, i ) {
graphByMonth.push([data[k],+k]);
});
console.log(graphByMonth);

How do i extract the first image from a particular URL using PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to extract the first image from a URL using hypertext-preprocessor. How do i do that?
If I understand you question correctly, you could use the DOMDocument class:
$url = 'www.yoururltocrawl.com';
$html = file_get_contents($url);
$dom = new DOMDocument();
$dom->loadHTML($html);
$images = $dom->getElementsByTagName('img');
From here on you should be able to get the rest by yourself I guess.
If understand your question correctly... this ll be useful for you...
$url = file_get_html('http://www.anysite.com/anypath/');
foreach($url->find('img') as $element) {
echo $element->src, "\n";
}

Categories

Resources