gpt4 book ai didi

javascript - 使用 javascript 或 php 从 json 数组获取值

转载 作者:行者123 更新时间:2023-11-28 07:46:29 25 4
gpt4 key购买 nike

这是我的代码:

var newval =[{"new_id":"1","new_no":"1","total":"N.A"},{"new_id":"2","new_no":"3","total":"4"},{"new_id":"2","new_no":"4","total":"5"}];

如何使用 javascript 或 php 从该 json 值获取基于 new_no 值的详细信息。

  function passfun(new_no)
{
alert(newval[0].plot_no);
}

我尝试了这样的操作,但它基于 json 数组值工作,我没有根据 new_no 获得准确的值

最佳答案

您有一个 JSON 格式的对象数组。在 PHP 中,您可以使用以下代码:

PHP:

<?php
$json = '[{"new_id":"1","new_no":"1","total":"N.A"},{"new_id":"2","new_no":"3","total":"4"},{"new_id":"2","new_no":"4","total":"5"}]';

$data = json_decode($json);

function get_prop($object, $name)
{
return $object->{$name};

}


print get_prop($data[0], 'new_no')."\n";
print get_prop($data[0], 'new_id')."\n";

JavaScript:

var newval =[{"new_id":"1","new_no":"1","total":"N.A"},
{"new_id":"2","new_no":"3","total":"4"},
{"new_id":"2","new_no":"4","total":"5"}];

function get_prop(obj, name)
{
return obj[name];
}

alert(get_prop(newval[0], 'new_no'));

关于javascript - 使用 javascript 或 php 从 json 数组获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27377176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com