在 jQuery 中遍历 PHP 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1205892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:32:23  来源:igfitidea点击:

Iterating through a PHP array in jQuery?

phpjqueryarraysforeach

提问by Angeline

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?

如何遍历 jQuery 中的 PHP 数组?我在 php 中有一个名为$viewfields. 如何使用 jQuery 遍历此数组的每个元素?

EDIT 1

编辑 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?

如何在 jQuery 中为 $view['Attribute']['type'] 编写代码?我想 elem['Attribute']['type'] 不起作用吗?

EDIT 2

编辑 2

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

elem['Attribute']['type'] 确实有效

回答by Ionu? G. Stan

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):

为了更好地理解事物是如何连接在一起的(感谢 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 SCRIPTtag wherever you want in the page, or you can even reference arrayFromPHPfrom external scripts as arrayFromPHPis declared as global.

您当然可以将该SCRIPT标签放置在页面中的任何位置,或者您甚至可以arrayFromPHParrayFromPHP声明为全局的外部脚本中引用。

EDIT

编辑

Given this PHP array:

鉴于此 PHP 数组:

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

Accessing its elements with jQuery would be done like this:

使用 jQuery 访问它的元素将是这样完成的:

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

$.each(arrayFromPHP, function (i, elem) {
    alert(elem.type);
    alert(elem.label);
});

回答by Damian

The easily way is:

简单的方法是:

PHP:

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);
});