如何使用重复的'id'值的JSON?

时间:2018-02-12 08:00:32

标签: php json

我正在使用json API从其他网站接收数据。

如何获取具有相同ID号的对象的数据。

我尝试如下但是变成空白..

有10个具有相同ID号的对象 我怎么能得到它们。

typeid数字相同,但specialvalue值不同。

   {"id":4620041,"active":true,"typeid":56,"specialvalue":"0.5","fields":{"magic":{"active":true,"type":"magic","value":"1.05"},"xmagic":{"active":true,"type":"xmagic","value":"7.5"}}}

{"id":4620045,"active":true,"typeid":56,"specialvalue":"1.5","fields":{"magic":{"active":true,"type":"magic","value":"3.05"},"xmagic":{"active":true,"type":"xmagic","value":"3.5"}}}

   	if($test->typeid =="56")
			{	
				if (specialvalue == "0.5") {
			$alta = $test->fields->magic->value;
			$uste = $test->fields->xmagic->value;
				}
			}
			
			echo $alta - $uste ;

提前感谢您的帮助

https://i.hizliresim.com/Pl0LL7.jpg

2 个答案:

答案 0 :(得分:0)

使用.filter

var filteredData = data.filter(
  (el) => {
     return el.typeid===56 && el.specialvalue==="0.5";
  }
);

这将只返回el.typeid===56 && specialvalue==="0.5"

的元素

答案 1 :(得分:0)

很少有观察结果:

解决方案:

let jsonObj = [{
	"id": 5246752,
	"active": true,
	"typeid": 56,
	"specialvalue": "0.5",
	"fields": {
		"magic": {
			"active": true,
			"type": "magic",
			"value": "1.06"
		},
		"xmagic": {
			"active": true,
			"type": "xmagic",
			"value": "7.0"
		}
	}
}, {
	"id": 5246753,
	"active": true,
	"typeid": 56,
	"specialvalue": "1.5",
	"fields": {
		"magic": {
			"active": true,
			"type": "magic",
			"value": "1.35"
		},
		"xmagic": {
			"active": true,
			"type": "xmagic",
			"value": "3.0"
		}
	}
}, {
	"id": 5246754,
	"active": true,
	"typeid": 56,
	"specialvalue": "2.5",
	"fields": {
		"magic": {
			"active": true,
			"type": "magic",
			"value": "2.0"
		},
		"xmagic": {
			"active": true,
			"type": "xmagic",
			"value": "1.7"
		}
	}
}, {
	"id": 5246755,
	"active": true,
	"typeid": 56,
	"specialvalue": "3.5",
	"fields": {
		"magic": {
			"active": true,
			"type": "magic",
			"value": "3.45"
		},
		"xmagic": {
			"active": true,
			"type": "xmagic",
			"value": "1.25"
		}
	}
}, {
	"id": 5246756,
	"active": true,
	"typeid": 56,
	"specialvalue": "4.5",
	"fields": {
		"magic": {
			"active": true,
			"type": "magic",
			"value": "6.25"
		},
		"xmagic": {
			"active": true,
			"type": "xmagic",
			"value": "1.08"
		}
	}
}, {
	"id": 5246757,
	"active": true,
	"typeid": 56,
	"specialvalue": "5.5",
	"fields": {
		"magic": {
			"active": true,
			"type": "magic",
			"value": "9.5"
		},
		"xmagic": {
			"active": true,
			"type": "xmagic",
			"value": "1.02"
		}
	}
}];

let filteredData = jsonObj.filter(item => item.typeid===56 && item.specialvalue==="0.5");

console.log(filteredData);