Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through a PHP array in jQuery?

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

like image 854
Angeline Avatar asked Jul 30 '09 11:07

Angeline


People also ask

How do you iterate through an array in jQuery?

The $. each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.

How do I pass a PHP array to Ajax jQuery?

if you would like to send 2 array via ajax, you can create a new array like this: <? php echo json_encode(array( 'issues' => $issue, 'foo' => $foo, )); ?>

Can we use for loop in jQuery?

You can use a JavaScript for loop to iterate through arrays, and a JavaScript for in loop to iterate through objects. If you are using jQuery you can use either the $. each() method or a for loop to iterate through an array.

What is each function in jQuery?

The each() method specifies a function to run for each matched element. Tip: return false can be used to stop the loop early.


1 Answers

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);
});
like image 102
Ionuț G. Stan Avatar answered Oct 19 '22 13:10

Ionuț G. Stan