php array to javascript array format [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
php array:
Array([0] => http://XXX/xgsyw/content/Uploads/Img/1111.jpg[1] => http://XXX/xgsyw/content/Uploads/Img/222.jpg)
to javascript "images"
$('#top').bgStretcher({
images: ['images/sample-1.jpg', 'images/sample-2.jpg', 'images/sample-3.jpg', 'images/sample-4.jpg', 'images/sample-5.jpg', 'images/sample-6.jpg'],
slideDirection: 'N',
slideShowSpeed: 1000,
transitionEffect: 'fade',
sequenceMode: 'normal',
});

Encode it with the JSON library. Send it back to the Javascript, decode it, and append new img child nodes to some container.
echo json_encode(arrayOfImages);
Then in your JS:
var images = JSON.decode(returnValue);
images.each(function(path) {
var img = $('<img>');
img.attr('src', returnValue);
img.appendTo('#imagediv');
});
As noted in the comments..
It is quicker to use $(document.createElement("img")); than to use the aforementioned suggestion.

You might have done some more research online, because what you need for your solution is the JavaScript Object Notation short JSON.
Use json_encode($your_array);
http://php.net/manual/de/function.json-encode.php

Related

One liner to find maximum from array in php for associative array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I looked at the question for maximum from associative array but I am looking for one liner. I have array like following,
$studenRsults=array(
array(
'roll'=>1,
'name'=>'jack Smit',
'marks'=>70
),
array(
'roll'=>4,
'name'=>'Sita',
'marks'=>50
),
array(
'roll'=>2,
'name'=>'Akhilesh',
'marks'=>80
),
array(
'roll'=>3,
'name'=>'jon',
'marks'=>50
),
);
Please suggest one liner thanks.
This needs php 5.5+ for array_column
$max = max(array_column($studenRsults, 'marks'));
or php 5.3+ for lamdas
$max = max(array_map(function($el){ return $el['marks']; }, $studenRsults));
Assuming you mean max of roll. But yeah, code quality isn't measured in how few lines you have...
foreach($studenRsults as $result) $maxResult = (isset($maxResult['roll']) && ($maxResult['roll'] > $result['roll'])) ? $maxResult : $result;
print_r($maxResult); // print the max roll student

Extract the 'image' attribute from this chunk of text via regex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I can find many examples of finding URL's withing HTML, however, I need to be more specific and extract the URL for the image in the following piece of text:
I will only need the first match.
I am using PHP.
Expected value result: http://cdn.somewebsite.com/path/123.jpg
var flashvars = {
'url_mode':'1',
'image':'http://cdn.somewebsite.com/path/123.jpg',
'bufferlength':'3',
'id': 'player',
'autostart': 'true'
...
};
Using preg_match with positive lookbehind assertion:
$data = <<< EOF
var flashvars = {
'url_mode':'1',
'image':'http://cdn.somewebsite.com/path/123.jpg',
'bufferlength':'3',
'id': 'player',
'autostart': 'true'
...
};
EOF;
preg_match("/(?<='image':')[^']+/", $data, $match);
echo($match[0]);
Hi this should work for you.
preg_match(/http.*?\.jpg/)

How to convert te JSON string to a JSON object in php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I got a object like this:
{"1":"FFS","2":"S"}, and I use json_decode function to convert it into an array, but I am not success with it. It becomes the array like this: {"FFS", "S"}, but missing the {"1", "2"}, Can I convert it to become a dict or something, that I can access both value? Thanks.
use true param, to convert object to associative array for json_decode(), like do:
$str = '{"1":"FFS","2":"S"}';
echo "<pre>"; print_r(json_decode($str, true));
gives::
Array
(
[1] => FFS
[2] => S
)
$myjsonobject = json_decode('{"1":"FFS","2":"S"}');
Should working.
Try: print_r($myjsonobject); to validate.
Its working.

PHP PCRE syntax [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm a newbie with PCRE in PHP. I'm trying to make a very basic shortcode function that could make something with a format like this one: {somealphanumericthing}
In essence I need a preg_match_all() that could find in my post these type of occurrences. I tried something like this:
$shortcode = preg_match_all('/^\b\{[a-zA-Z0-9_]+(\}\b)$/', $body, $found);
var_dump($shortcode);
if($shortcode==1) {
for($i=0;$i<count($found);$i++) {
print_r($found);
//do something nice
}
}
But unfortunately it's not working: I get int 0 to the test string {test}
A few things about the regular expression:
You don't need your line anchors since you are searching in a larger string.
There's not need to capture the closing }
Optimization, use the character class \w
Condensed:
/\b\{[a-zA-Z0-9_]+\}\b/

How to write a Loop for a Dummy? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
this is probably an easy question, but I am not really a coder. I maintain a website for a group I belong to, and there are some php forms. I am trying to enhance a form, and I have added this php code, which works (see below). My question is, how can I put this code into a loop so that it repeats 10 times, for "01", "02", etc., "10" strings (code below is for "01" of the series)? Thanks for any help.
//Clean up the Photo Title & Maker Name to remove non-standard chars and replace spaces with underscores and generate a suggested fliename
$photo_01_clean = str_replace(" ","75357",$photo_01_name);
$photo_01_clean1 = str_replace("#","at",$photo_01_clean);
$photo_01_clean2 = preg_replace('/[^a-z0-9]/i', '', $photo_01_clean1);
$photo_01_clean3 = str_replace("75357","_",$photo_01_clean2);
$photo_01_maker_clean = str_replace(" ","75357",$photo_01_maker_name);
$photo_01_maker_clean1 = str_replace("#","at",$photo_01_maker_clean);
$photo_01_maker_clean2 = preg_replace('/[^a-z0-9]/i', '', $photo_01_maker_clean1);
$photo_01_maker_clean3 = str_replace("75357","_",$photo_01_maker_clean2);
$photo_01_filename_gen = $club_code."-01-".$photo_01_clean3."-".$photo_01_maker_clean3.".jpg";
You'd require a for loop control structure. You can find tons of resources that would teach you how to do it. example
Var i;
For (i =1; i<11; i++)
Execute Code here.

Categories