将PHP数组转换为JavaScript

时间:2014-10-24 10:30:26

标签: javascript php arrays

我知道有很多关于将PHP数组转换为Javascript的问题,标准答案如下:

tempary=<?php echo json_encode($tmpary); ?>;

但是,当我尝试访问数组时,我无法访问这些元素。 e.g。

document.write(tempary[2]);                                 //debug

即使我知道有第3个元素。

以下是实际代码:

global $wpdb;
$query="SELECT * FROM wp_ta_members";
$member_table=$wpdb->get_results($query);
echo "<p>Size of wp_ta_members table is " . count($member_table) . "</p>";      //debug
?>
<script type="text/javascript">
var members = [];
var tempary = [];
</script>
<?php
foreach ($member_table as $member_row)
    {
    $tmpary=array($member_row->member_id,
    ($member_row->current==1)?"current":"not a member",
    $member_row->date_joined,
    $member_row->title,
    $member_row->first_names,
    $member_row->last_names)
    ?>
    <script type="text/javascript">
    tempary=<?php echo json_encode($tmpary); ?>;
    document.write(tempary[2]);                                 //debug
    console.log(tempary);                                   //debug
    members.push(tempary.concat());
</script>
<?php   }
?>

您会注意到我实际上正在创建一个数组数组,稍后我将使用以下循环处理:

for (mem in members)
{
document.write(workary.length + " " + members.length + " " + mem.length + " " + members[1] + "<br />");     //debug
document.write(mem[1] + " " + mem[2] + " " + mem[3] + " " + mem[4] +"<br />");      //debug
}

然而,我得到的所有tempary [0]都是&#34; 1&#34;以及任何其他元素 - &#34; undefined&#34;。

如果我输出tempary本身,我会得到一个由逗号分隔的所有元素的字符串。

我错过了什么?我希望tempary成为一个合适的JavaScript数组。

很抱歉再次回来,但这仍然无效

我已将以下代码添加到脚本中,只是为了查看概念:

var a1 = [100, "scooby", "doo"];
var a2 = [200, "soup", "dragon"];
var a3 = [];
a3.push(a1);
a3.push(a2);
console.log(a3);
console.log(a3[0]);
console.log(a3[1]);
console.log(a3[1][0] + " " + a3[1][1]);

结果是:

[Array[3], Array[3]]
[100, "scooby", "doo"]
[200, "soup", "dragon"]
200 soup

这是正确的!

当我运行我的真实代码并执行这些行(在循环中)时:

    console.log(tempary);                                   //debug
    members.push(tempary);

我得到了结果:

["78", "current", "2014-01-01", "", "Fred", "Bloggs"]

这也是正确的(虽然我更希望&#34; 78&#34;存储为数字而不是字符串 - 但这是另一个故事)。

然而,当我执行一个循环时,使用:

打印出数组数组的内容
for (mem in members)
    {
    console.log(members.length + " " + mem.length + " " + members[1]);      //debug
    console.log(mem + " " + mem[0] + " " + mem[1] + " " + mem[2] + " " + mem[3] + " " + mem[4]);        //debug

    }

我得到一个重复的日志列表:

1333 1 8000,current,2014-01-01,Ms,Joe,Soap
0 0 undefined undefined undefined undefined
1333 1 8000,current,2014-01-01,Ms,Joe,Soap
1 1 undefined undefined undefined undefined
1333 1 8000,current,2014-01-01,Ms,Joe,Soap
2 2 undefined undefined undefined undefined
1333 1 8000,current,2014-01-01,Ms,Joe,Soap
3 3 undefined undefined undefined undefined
1333 1 8000,current,2014-01-01,Ms,Joe,Soap
4 4 undefined undefined undefined undefined
1333 1 8000,current,2014-01-01,Ms,Joe,Soap
5 5 undefined undefined undefined undefined
1333 1 8000,current,2014-01-01,Ms,Joe,Soap
6 6 undefined undefined undefined undefined
1333 1 8000,current,2014-01-01,Ms,Joe,Soap
7 7 undefined undefined undefined undefined
1333 1 8000,current,2014-01-01,Ms,Joe,Soap
8 8 undefined undefined undefined undefined
1333 1 8000,current,2014-01-01,Ms,Joe,Soap
9 9 undefined undefined undefined undefined
1333 1 8000,current,2014-01-01,Ms,Joe,Soap
10 1 0 undefined undefined undefined
1333 1 8000,current,2014-01-01,Ms,Joe,Soap
11 1 1 undefined undefined undefined
1333 1 8000,current,2014-01-01,Ms,Joe,Soap
12 1 2 undefined undefined undefined

其中1333是成员数组中的数组数。 Joe Soap是列表中的最后一个成员。 为什么console.log(mem);产生一个递增的数字我不知道。当这个数字达到两位数时会发生另一种奇怪的事情,例如: 12使mem [0] = 1和mem [1] = 2 !! mem的其余元素是&#34; undefined&#34;。

为了完整起见,这是整个脚本:

<?php
/***** php and javascript to administer member details *****/

// **** get entire members table into a variable
    global $wpdb;
    $query="SELECT * FROM wp_ta_members";
    $member_table=$wpdb->get_results($query);
    echo "<p>Size of wp_ta_members table is " . count($member_table) . "</p>";  //debug
?>
<script type="text/javascript">
    var members = [2000];
    var tempary = [];
    var a1 = ["100", "scooby", "doo"];      //test
    var a2 = ["200", "soup", "dragon"];     //test
    var a3 = [];        //test
    a3.push(a1);        //test
    a3.push(a2);        //test
    console.log(a3);
    console.log(a3[0]);
    console.log(a3[1]);
    console.log(a3[1][0] + " " + a3[1][1]);
<?php
    foreach ($member_table as $member_row)
        {
        $tmpary=array($member_row->member_id,
        ($member_row->current==1)?"current":"not a member",
        $member_row->date_joined,
        $member_row->title,
        $member_row->first_names,
        $member_row->last_names);
        $x = 0;
        foreach($tmpary as $value){
            echo 'tempary[' . $x . '] = "' . $value . '";';
            $x++;
            }
?>
//      tempary=JSON.parse("<?php echo json_encode($tmpary); ?>");
        document.write(tempary[4] +"<br />");       //debug
        console.log(tempary);           //debug
        members.push(tempary);
<?php       }
?>
    document.write(members + "<br />");     //debug
    document.write("Hi Will again<br />");      //debug
    for (mem in members)
        {
        console.log(members.length + " " + mem.length + " " + members[1]);  //debug
        console.log(mem + " " + mem[0] + " " + mem[1] + " " + mem[2] + " " + mem[3] + " " + mem[4]);    //debug

        }
</script>

我已经和他一起摔跤已经好几天了,而且已经没想完了。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

我看到你在每次迭代时都要覆盖你的临时数组。所以当你json_encode时,你只剩下最后一项了。

请改为尝试:

global $wpdb;
$query="SELECT * FROM wp_ta_members";
$member_table=$wpdb->get_results($query);
echo "<p>Size of wp_ta_members table is " . count($member_table) . "</p>";      //debug
?>
<script type="text/javascript">
<?php
$members = array();
foreach ($member_table as $member_row){
    $member = array(
        $member_row->member_id,
        ($member_row->current==1)?"current":"not a member",
        $member_row->date_joined,
        $member_row->title,
        $member_row->first_names,
        $member_row->last_names
    );
    $members[] = $member;
}
?>
var members = <?php echo json_encode($members) ?>;
</script>
<?php   }
?>   

答案 1 :(得分:1)

我发现了问题所在。特别是 killneel 谁为2D阵列问题提出了一个非常优雅的解决方案。 事实证明,问题的关键在于我访问阵列的方式。仅供参考NEVER用户使用for..in循环来处理数组!使用起来要好得多:

var len = arr.length;
for (var i=0; i<len; i++) {
    console.log(arr[0];     // or whatever you want to do with the array element
    }

答案 2 :(得分:0)

使用foreach循环并在声明JS数组后在每次迭代中写出JS代码:

<script>

var js_array = new Array();

<?php

    $x = 0;

    foreach($item as $key => $value){

        echo 'js_array[$x] = "' . $value . '";';

        $x++;
    }

?>

</script>