15

How do I iterate through a PHP array in jQuery? I have an array in php named $viewfields. How do I iterate through each element of this array using jQuery?

EDIT 1

<?php foreach ($viewfields as $view): ?>           

if(<?=$view['Attribute']['type'];?>=='text'||<?=$view['Attribute']['type'];?>=='number')
{ 
     $("<input id=input<?=$view['Attribute']['sequence_no'];?> type= 'text' style= 'width:<?=$view['Attribute']['size'];?>px' data-attr=<?=$view['Attribute']['type'];?> ></input><br>").appendTo("#fb_contentarea_col1down21 #<?=$view['Attribute']['sequence_no'];?>");
}

If i give

$.each(arrayfromPHP,function(i,elem){

}

how do I write the code for $view['Attribute']['type'] in jQuery? elem['Attribute']['type'] won't work I suppose?

EDIT 2

elem['Attribute']['type'] does work

2 Answers 2

44
var arrayFromPHP = <?php echo json_encode($viewFields) ?>;

$.each(arrayFromPHP, function (i, elem) {
    // do your stuff
});

To better understand how the things are wired together (thanks Jonathan Sampson):

<!DOCTYPE html>

<html>
<head>
<script type="text/javascript">
var arrayFromPHP = <?php echo json_encode($viewFields) ?>;

$.each(arrayFromPHP, function (i, elem) {
    // do your stuff
});
</script>
</head>
<body>

</body>
</html>

You can of course place that SCRIPT tag wherever you want in the page, or you can even reference arrayFromPHP from external scripts as arrayFromPHP is declared as global.

EDIT

Given this PHP array:

$viewFields = array(
    'Attributes' => array(
        'type'  => 'foo',
        'label' => 'bar',
    ),
    'Attributes' => array(
        'type'  => 'foo',
        'label' => 'bar',
    ),
);

Accessing its elements with jQuery would be done like this:

// json_encode() will output:
// {"Attributes":{"type":"foo","label":"bar"}}

$.each(arrayFromPHP, function (i, elem) {
    alert(elem.type);
    alert(elem.label);
});
Sign up to request clarification or add additional context in comments.

5 Comments

Note that PHP and jQuery aren't written side-by-side. You'll need to wrap the PHP in <?php and ?>, and the jQuery in <script type="text/javascript"> and </script.
In the php for array, I use the individual elements of the array as $view['Attribute']['id'] or $view['Attribute']['label'], where I assign $view as <?php foreach($viewfields as $view):?> how do I access those elements in .each function()?
@Angeline, can you edit the question and post the structure of the PHP array as returned by print_r? Just paste the output of print_r($viewFields).
By the way, here's some documentation about jQuery.each: docs.jquery.com/Utilities/jQuery.each
@lonut G.Stan : Thank you so much. I'm getting the answer. but one small change, it's elem['Attribute']['type'] rather than elem.type. ;)
-2

The easily way is:

PHP:

$an_array=array();
$an_array[]='Element 1';
$an_array[]='Element 2';
$an_array[]='Element 3';
$array_js=implode(",",$this->js_pagina); //join elements in a string

JQUERY:

//Converter
window.array=new String('<?php echo $array_js?>');
window.array=window.js_pagina.split(",");
//Iterator
$.each(window.array, function (i, elem) 
{
        alert(elem);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.