【问题标题】:How to work with array JSON in php?如何在 php 中使用数组 JSON?
【发布时间】:2017-01-24 03:33:16
【问题描述】:

我是一名 JavaScript 程序员,有一个 PHP 项目。 我在使用 PHP 处理 JSON 时遇到问题。

这是我的 JSON

{
   "orders":[
  {
     "name":"#1002"
  },
  {
     "name":"#1001"
  }
  ]
}

我需要获取每个名称并回显它们,我尝试了以下代码$myarray = json_decode($order, true),但它返回了这个错误。

警告:json_decode() 期望参数 1 是字符串,数组中给出

如何将 json 从数组转换为字符串?还是我做错了。

【问题讨论】:

  • 你有没有在你的 javascript 中将 JSON 转换为字符串,比如JSON.stringify(myjson)
  • 向我们展示如何将 json 保存在 $order 变量中
  • print_r($order); 检查您的数组并调整 json 索引并更新 json_decodefunction。

标签: javascript php arrays json


【解决方案1】:

config.php

<?php 
$con = mysql_connect('localhost','root','');
mysql_select_db('db_school',$con);
$sql = "SELECT * FROM userlogin";
$result = mysql_query($sql,$con);
$data = array();
while ($row = mysql_fetch_array($result)) {
    $data[] =array(
        'id'=>$row['id'],
        'username'=>$row['username'],
        'userpass'=>$row['userpass'],
    );
}
echo json_encode(array('data'=>$data));
 ?>

view.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<table id="tbl" border="1">
        <thead><tr>
            <th>Id</th>
            <th>Username</th>
            <th>Password</th>
        </tr></thead>
        <tbody></tbody>
    </table>
    <script type="text/javascript">
        $.ajax({
            url: 'config.php',
        }).done(function(res) {
            var res_data = JSON.parse(res),table='';
            $.each(res_data.data,function(index, el) {
                console.log(el.id,' ', el.username,' ',el.userpass);
                table+='<tr>';                
                table+='<td>'+el.id+'</td>';                
                table+='<td>'+el.username+'</td>';                
                table+='<td>'+el.userpass+'</td>';                
                table+='</tr>';                
            });
            $('#tbl').html(table);
        });
    </script>

演示:

【讨论】:

  • 你好,我看到很多使用这种方法,你有视频教程或参考网址吗?
  • 这是我的 YouTube 香奈儿。 www.somelesson.blogspot.com youtube.com/rampukarinfo
【解决方案2】:

只需将 json 响应保存在一个变量上。然后做 json_decode

$orders = '{
       "orders":[
         {
            "name":"#1002"
         },
         {
            "name":"#1001"
         }
      ]
}';
$myarray = json_decode($orders, true);

现在执行 print_r 将产生类似的结果

echo '<pre>';
print_r($myarray);

结果

Array
(
    [orders] => Array
        (
            [0] => Array
                (
                    [name] => #1002
                )

            [1] => Array
                (
                    [name] => #1001
                )

        )

)

编辑:

为了只获取名称值,只需运行 foreach

$result = [];
foreach($myarray['orders'] as $value) {
    $result[] = $value['name'];
}

现在打印 $result 你会得到

Array
(
    [0] => #1002
    [1] => #1001
)

【讨论】:

  • 但我希望结果是这样的:#1002 #1001 而不是 print_R 的结果
  • 我不明白。你的意思是,你想要它像 ['#1002', '#1001']?
  • 然后foreach($myarray as $order) { echo $order['name']; }
猜你喜欢
  • 1970-01-01
  • 2014-12-17
  • 1970-01-01
  • 2015-07-21
  • 1970-01-01
  • 2021-05-17
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多