gpt4 book ai didi

php - 循环按类型过滤的 json 编码

转载 作者:行者123 更新时间:2023-11-28 21:05:38 25 4
gpt4 key购买 nike

我在 PHP 中创建了一个 json 编码,如下所示:

$get = mysql_query("SELECT * FROM $table") or die(mysql_error());
$data = array();
while($row = mysql_fetch_assoc($get)){
$data[$row['id']][0] = $row['g'];
$data[$row['id']][1] = $row['w'];
$data[$row['id']][2] = $row['s'];
$data[$row['id']][3] = $row['name'];
$data[$row['id']][4] = $row['type'];
}
$data4 = json_encode($data);

我将它分配给一个js变量:var bdata;

所以我现在想做的是循环按 [4] = type 过滤的内容。

这是我的循环:

 for(var key in bdata){
//stuff here
}

但这将循环所有这些...但我只想循环具有特定值的 [4] (类型)的那些,假设该值为“2”。

有办法做到这一点吗?

最佳答案

您应该在 php 中过滤数组并将过滤后的数组传递给 JS-Variable:

function filter($item) {
return $item[4] == $whateverYouWant;
}
$data4 = json_encode(array_filter($data, "filter"));

关于php - 循环按类型过滤的 json 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9956088/

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