views:

141

answers:

5

I give you what I want in PHP :

foreach($myarray as $key=>$value) { ${$key} = $value; }

Now, can we do it with JS/jQuery ?

This is the jQuery input, I want the classTD value as key, in other words the equivalent of the famous PHP statement ${$key} in the example above:

data  = {};
$('#resul_rech_doc_fact tr.document-2 td').each(function(index) {
var classTD = $(this).attr('class');
var contentTD = $(this).text();
data = $.extend(data , { classTD : contentTD });
});
+3  A: 

If you want to convert PHP array to object in Javascript - you can use function :

http://php.net/manual/en/function.json-encode.php

Example from manual:

// PHP
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
$js = json_encode($arr);
echo $js;

// output
{"a":1,"b":2,"c":3,"d":4,"e":5} 

So you can use PHP to "echo" $js variable to initialize your javascript for example.

For iteration through properties of JS object you can use:

http://www.w3schools.com/js/js_loop_for_in.asp

Example:

for (key in objectInstance)
{
  alert(objectInstance[key]); // value
}
MartyIX
I have any php data !! I want the equivalent of the php statement in jquery.
Amirouche Douda
@Amirouche Douda mean to say "I don't have any PHP data", as in, he isn't interacting with PHP at all. (The grammar mistake is a common French => English error.)
strager
@ strager : Exactly,I am sorry.
Amirouche Douda
@Amirouche Douda: Ok, I see now. Stager answered your question.
MartyIX
A: 

Well, you could use with, but with is considered harmful:

var obj = { bar: 'foo' };

with (obj) {
    alert(bar); // 'foo'
}

alert(bar); // undefined

If you must have the loop, you can do this:

var obj = { bar: 'foo' };
var key, value;

with (obj) {
    for (key in obj) {
        if (!obj.hasOwnProperty(key)) {  // Never forget this check!
            continue;
        }

        value = obj[key];

        // Rest of loop here.
    }
}
strager
@Marcel Korpel, What I wrote is more or less exactly what his PHP does for the inner block. I see he edited his question with more information, though; he was trying to use a hammer to punch in a screw.
strager
I added in a looping version in case he really wanted a loop.
strager
@Marcel Korpel, Oops; yes I am. There is a `for` statement, however. Simple fix.
strager
A: 

For all web purposes, global scope = window, so:

window['foobar'] = 'bar';
alert(foobar);

Hence the (unrecommendable):

var json = {'foobar':'bar'};
for i in json {
    window[i] = json[i];
}
alert(foobar);
Wrikken
+3  A: 

The line:

data = $.extend(data , { classTD : contentTD });

Doesn't do what you expect. It is identical to:

data = $.extend(data , { 'classTD' : contentTD });

It seems you want to evaluate classTD and sue it as the object's key. You have two options:

You can assign the data member, modifying data:

data[classTD] = contentTD;

or you can use $.extend with a temporary object:

var obj = { };
obj[classTD] = contentTD;
data = $.extend(data, obj);
strager
Thx, you resolved it, can you suggest me other idea to do it well.
Amirouche Douda
what is wrong with temporary object?
MartyIX
@Amirouche Douda, I don't understand. This solution fixed your problem; why do you need another solution?
strager
I am looking for a way better than temporary object solution.
Amirouche Douda
In that case you should say why. Otherwise it seems you're just 'picky' ;-)
MartyIX
I think that sje397 has found the no temporary object solution.
Amirouche Douda
@Amirouche Douda, That's my third code block.
strager
@strager : Yes, thank's for your effort in this thread, your response has included all the question that’s why you deserve the best answer ;)
Amirouche Douda
+1  A: 

I think what you want is:

data  = {};
$('#resul_rech_doc_fact tr.document-2 td').each(function(index) {
  data[$(this).attr('class')] = $(this).text();
});

If you want to create global variables, you can set poperties on the window object (but I wouldn't recommend it):

$('#resul_rech_doc_fact tr.document-2 td').each(function(index) {
  window[$(this).attr('class')] = $(this).text();
});

Then, if for example you had a class called 'foo' you could access 'foo' as a "global variable". The danger here is that you could very easily overwrite important properties of the window object.

sje397
Good, but have you any idea to how to impelement $($key) with jquery.
Amirouche Douda
@Amirouche Douda - that's what indexing an oject property with square brackets does. using `obj[key]` makes `key` a property of `obj`. In javascript, global variables are really just properties of the `window` object.
sje397
@Amirouche Douda - edited.
sje397
@Amirouche Douda, It's possible, but not recommended. *Why* do you want to do it?
strager
@strager : Why not recommended ?
Amirouche Douda
@Amirouche Douda, Because it's a hack. There are better ways to do what you want to do. Why do you want to do `${$key}` specifically?
strager