jQuery的转换字符串数组到PHP数组 [英] Convert jQuery Array string to PHP Array

查看:239
本文介绍了jQuery的转换字符串数组到PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我得说我是pretty新到PHP,我试图让我可以使用的foreach PHP对象。

First of all, I gotta say I'm pretty new to PHP and I'm trying to get a PHP object on which I can use foreach.

以下字符串是通过$阿贾克斯()通过;我试图把以下字符串:

The following string is passed through $.ajax(); I'm trying to turn the following string:

$menu = "[
 {"title" : TEST1, "href" : #},
 {"title" : TEST2, "href" : QWERTY},
 {"title" : TEST3, "href" : QWERTY, "active" : 1}
]"

进入和PHP对象上,我可以用一个foreach循环:

into and php object on which I can use a foreach loop:

foreach($menu as $li){
    echo $li['title'];
}

我使用的最佳解决方案,用于创建菜单项,或者我应该是继另一种格式?

Am I using the optimal solution for creating the menu items or should I be following another format?

非常感谢你提前!

最好的问候,
亚历克斯-G。

Best regards, Alex G.

推荐答案

这是一个JSON格式..它被打破..修正你的JSON数据如图所示,并使用一个循环的foreach

That's a JSON format.. and it is broken.. Fix your JSON data as shown and loop using a foreach

PHP

<?php
$menu = '[{"title" : "TEST1", "href" : "#"},
 {"title" : "TEST2", "href" : "QWERTY"},
 {"title" : "TEST3", "href" : "QWERTY", "active" : 1}]';

foreach(json_decode($menu,true) as $k=>$arr)
{
    echo $arr['title']."<br>";
}

OUTPUT:

TEST1
TEST2
TEST3

这篇关于jQuery的转换字符串数组到PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆