如果一个json数组满足某个条件,那么在PHP解码后,如何从该数组中删除它?
一般来说,我对php非常陌生,所以如果这看起来像是一个愚蠢的问题或者我没有什么意义,我很抱歉 下面是我合并多个JSON文件的代码:如果一个json数组满足某个条件,那么在PHP解码后,如何从该数组中删除它?,php,arrays,json,Php,Arrays,Json,一般来说,我对php非常陌生,所以如果这看起来像是一个愚蠢的问题或者我没有什么意义,我很抱歉 下面是我合并多个JSON文件的代码: <?php $files = glob("*.json"); $newDataArray = []; foreach($files as $file){ $thisData = file_get_contents($file); $thisDataArray = json_decode($thisData); $newDataArray
<?php
$files = glob("*.json");
$newDataArray = [];
foreach($files as $file){
$thisData = file_get_contents($file);
$thisDataArray = json_decode($thisData);
$newDataArray[] = $thisDataArray;
}
$newDataJSON = json_encode($newDataArray);
file_put_contents("merged.json",$newDataJSON);
?>
所以在这个例子中,我会删除[0]的[0]和[3],因为股票为零,我会删除[1]的[2],因为股票为零
我一直在尝试的失败路线是:
<?php
$data = file_get_contents('merged.json');
$json_arr = json_decode($data, true);
$arr_index = array();
foreach ($json_arr as $key => $value) {
if ($value['stock'] == 0) {
$arr_index[] = $key;
}
}
foreach ($arr_index as $i) {
unset($json_arr[$i]);
}
$json_arr = array_values($json_arr);
file_put_contents('cleaned.json', json_encode($json_arr));
?>
如果查看如下所示的产品JSON数据,则产品数据位于
user
->products
的深处。此外,产品本身也是一个阵列。因此,您需要两个循环来实现这一点。外部一个是循环用户,内部一个是循环每个用户的产品,并将所有有库存的取消设置为0
Array
(
[0] => Array
(
[status] => 1
[user] => Array
(
[username] => Chris
[avatar] => https://s3-storage.//avatars/iiPjqYlVkijUv9XoPt0YUpyHuueBdkFkrTjIwnJd.gif
[rep] => Array
(
[positive] => 1
[neutral] => 0
[negative] => 0
)
[products] => Array
(
[0] => Array
(
[id] => JucpDM8
[title] => Non-Full Access(NFA)
[image] => Array
(
[url] => https://s3-storage.//product_images/SDcaqvqginNTmVKwYHCDfajoOqc0OqHjq3pMjyAw.png
[path] => product_images/SDcaqvqginNTmVKwYHCDfajoOqc0OqHjq3pMjyAw.png
)
[quantity] => Array
(
[min] => 25
[max] => 1000000
)
[price] => 0.02
[currency] => USD
[stock_warning] => 500
[type] => account
[stock] => 12
)
片段:
<?php
$str = '[{"status":true,"user":{"username":"Chris","avatar":"https:\/\/s3-storage.\/\/avatars\/iiPjqYlVkijUv9XoPt0YUpyHuueBdkFkrTjIwnJd.gif","rep":{"positive":1,"neutral":0,"negative":0},"products":[{"id":"JucpDM8","title":"Non-Full Access(NFA)","image":{"url":"https:\/\/s3-storage.\/\/product_images\/SDcaqvqginNTmVKwYHCDfajoOqc0OqHjq3pMjyAw.png","path":"product_images\/SDcaqvqginNTmVKwYHCDfajoOqc0OqHjq3pMjyAw.png"},"quantity":{"min":25,"max":1000000},"price":0.02,"currency":"USD","stock_warning":500,"type":"account","stock":12},{"id":"qX4vLC2","title":"Unmigrated(UFA)","image":{"url":"https:\/\/s3-storage.\/\/product_images\/F0PaLkMLbWS9gZt7DtRfMNYm2bufv796HTlm4gFe.jpeg","path":"product_images\/F0PaLkMLbWS9gZt7DtRfMNYm2bufv796HTlm4gFe.jpeg"},"quantity":{"max":1000000,"min":1},"price":5.99,"currency":"USD","stock_warning":5,"type":"account","stock":0},{"id":"sYu98Kq","title":"Semi-Full(SFA)","image":{"url":"https:\/\/s3-storage.\/\/product_images\/5mGCAH4Fsj23MlBHw5yw1UoPsw65dGI1ZkmxwNfN.jpeg","path":"product_images\/5mGCAH4Fsj23MlBHw5yw1UoPsw65dGI1ZkmxwNfN.jpeg"},"quantity":{"max":1000000,"min":1},"price":1,"currency":"USD","stock_warning":5,"type":"account","stock":0},{"id":"7efhwnl","title":"Optifine Cape","image":{"url":"https:\/\/s3-storage.\/\/product_images\/W6q61CMgmlzzwtQlk0jXfaXbOcse4YfhrHEIuF0N.jpeg","path":"product_images\/W6q61CMgmlzzwtQlk0jXfaXbOcse4YfhrHEIuF0N.jpeg"},"quantity":{"max":1000000,"min":1},"price":4.99,"currency":"USD","stock_warning":2,"type":"account","stock":0},{"id":"kBkgLAv","title":"Hypixel Ranked(NFA)","image":{"url":"https:\/\/s3-storage.\/\/product_images\/nxvFRT9YcKVuOHPilMGdLM08i7JhYTdd4Aqq0xSK.png","path":"product_images\/nxvFRT9YcKVuOHPilMGdLM08i7JhYTdd4Aqq0xSK.png"},"quantity":{"max":1000000,"min":1},"price":4.99,"currency":"USD","stock_warning":5,"type":"account","stock":0},{"id":"X5kmV3z","title":"Lamm","image":{"url":"https:\/\/s3-storage.\/\/product_images\/rgGnCcqMIqgvsETsVTWjrF2EhVSwIOE5bXNwEtUC.jpeg","path":"product_images\/rgGnCcqMIqgvsETsVTWjrF2EhVSwIOE5bXNwEtUC.jpeg"},"quantity":{"max":1000000,"min":5},"price":0.05,"currency":"USD","stock_warning":0,"type":"account","stock":0},{"id":"AFwEIkN","title":"Lammy","image":{"url":"https:\/\/s3-storage.\/\/product_images\/mUbSzYsq8iII9Ro3wmJkyWRwk4oBVJmDNKavBGr4.png","path":"product_images\/mUbSzYsq8iII9Ro3wmJkyWRwk4oBVJmDNKavBGr4.png"},"quantity":{"max":1000000,"min":1},"price":1,"currency":"USD","stock_warning":0,"type":"account","stock":0},{"id":"JYcUni4","title":"Hulu","image":{"url":"https:\/\/s3-storage.\/\/product_images\/GaVbNGASgkv6LCErOjg1zK9MFWlrySvZGUVNCjPb.png","path":"product_images\/GaVbNGASgkv6LCErOjg1zK9MFWlrySvZGUVNCjPb.png"},"quantity":{"max":1000000,"min":5},"price":0.5,"currency":"USD","stock_warning":10,"type":"account","stock":14}],"groups":[{"id":"4TlYivf","title":"Minecraft Is","image":"","unlisted":false,"products":["sYu98Kq","JucpDM8","qX4vLC2","7efhwnl","kBkgLAv"]},{"id":"GTXwlkU","title":"Streaming","image":"","unlisted":false,"products":["JYcUni4","AFwEIkN","X5kmV3z"]}],"feedbacks":[{"updated_at":"2018-09-16 00:29:35","stars":5,"rating":1,"comment":"The best thing in the world! Thanks!","response":"Thank you so much! Come again!","product":null}],"online":{"state":false,"ago":"1 year ago"},"staff":false}},{"status":true,"user":{"username":"bear","avatar":"\/images\/default-avatar.png?id=55b864e42ffc120971da","rep":{"positive":0,"neutral":0,"negative":0},"products":[{"id":"p9YymiV","title":"fortnite","image":null,"quantity":{"min":1,"max":1},"price":75,"currency":"EUR","stock_warning":0,"type":"service","stock":9223372036854775807}],"groups":[],"feedbacks":[],"online":{"state":false,"ago":"1 year ago"},"staff":false}},{"status":true,"user":{"username":"king","avatar":"\/images\/default-avatar.png?id=55b864e42ffc120971da","rep":{"positive":0,"neutral":0,"negative":0},"products":[{"id":"91vFhN4","title":"uplay with 40 game","image":null,"quantity":{"min":1,"max":1},"price":4,"currency":"EUR","stock_warning":0,"type":"account","stock":0},{"id":"vX5IJ4T","title":"spotify premium","image":null,"quantity":{"min":1,"max":1},"price":2,"currency":"EUR","stock_warning":0,"type":"service","stock":9223372036854775807}],"groups":[],"feedbacks":[],"online":{"state":false,"ago":"1 year ago"},"staff":false}}]';
$json_decoded = json_decode($str,true);
foreach($json_decoded as $index => $user_data){
foreach($user_data['user']['products'] as $product_index => $product_data){
if($product_data['stock'] == 0){
unset($json_decoded[$index]['user']['products'][$product_index]);
}
}
}
echo json_encode($json_decoded);
如果您查看产品的JSON数据,如下所示,产品数据位于用户
->产品
的深处。此外,产品本身也是一个阵列。因此,您需要两个循环来实现这一点。外部一个是循环用户,内部一个是循环每个用户的产品,并将所有有库存的取消设置为0
Array
(
[0] => Array
(
[status] => 1
[user] => Array
(
[username] => Chris
[avatar] => https://s3-storage.//avatars/iiPjqYlVkijUv9XoPt0YUpyHuueBdkFkrTjIwnJd.gif
[rep] => Array
(
[positive] => 1
[neutral] => 0
[negative] => 0
)
[products] => Array
(
[0] => Array
(
[id] => JucpDM8
[title] => Non-Full Access(NFA)
[image] => Array
(
[url] => https://s3-storage.//product_images/SDcaqvqginNTmVKwYHCDfajoOqc0OqHjq3pMjyAw.png
[path] => product_images/SDcaqvqginNTmVKwYHCDfajoOqc0OqHjq3pMjyAw.png
)
[quantity] => Array
(
[min] => 25
[max] => 1000000
)
[price] => 0.02
[currency] => USD
[stock_warning] => 500
[type] => account
[stock] => 12
)
片段:
<?php
$str = '[{"status":true,"user":{"username":"Chris","avatar":"https:\/\/s3-storage.\/\/avatars\/iiPjqYlVkijUv9XoPt0YUpyHuueBdkFkrTjIwnJd.gif","rep":{"positive":1,"neutral":0,"negative":0},"products":[{"id":"JucpDM8","title":"Non-Full Access(NFA)","image":{"url":"https:\/\/s3-storage.\/\/product_images\/SDcaqvqginNTmVKwYHCDfajoOqc0OqHjq3pMjyAw.png","path":"product_images\/SDcaqvqginNTmVKwYHCDfajoOqc0OqHjq3pMjyAw.png"},"quantity":{"min":25,"max":1000000},"price":0.02,"currency":"USD","stock_warning":500,"type":"account","stock":12},{"id":"qX4vLC2","title":"Unmigrated(UFA)","image":{"url":"https:\/\/s3-storage.\/\/product_images\/F0PaLkMLbWS9gZt7DtRfMNYm2bufv796HTlm4gFe.jpeg","path":"product_images\/F0PaLkMLbWS9gZt7DtRfMNYm2bufv796HTlm4gFe.jpeg"},"quantity":{"max":1000000,"min":1},"price":5.99,"currency":"USD","stock_warning":5,"type":"account","stock":0},{"id":"sYu98Kq","title":"Semi-Full(SFA)","image":{"url":"https:\/\/s3-storage.\/\/product_images\/5mGCAH4Fsj23MlBHw5yw1UoPsw65dGI1ZkmxwNfN.jpeg","path":"product_images\/5mGCAH4Fsj23MlBHw5yw1UoPsw65dGI1ZkmxwNfN.jpeg"},"quantity":{"max":1000000,"min":1},"price":1,"currency":"USD","stock_warning":5,"type":"account","stock":0},{"id":"7efhwnl","title":"Optifine Cape","image":{"url":"https:\/\/s3-storage.\/\/product_images\/W6q61CMgmlzzwtQlk0jXfaXbOcse4YfhrHEIuF0N.jpeg","path":"product_images\/W6q61CMgmlzzwtQlk0jXfaXbOcse4YfhrHEIuF0N.jpeg"},"quantity":{"max":1000000,"min":1},"price":4.99,"currency":"USD","stock_warning":2,"type":"account","stock":0},{"id":"kBkgLAv","title":"Hypixel Ranked(NFA)","image":{"url":"https:\/\/s3-storage.\/\/product_images\/nxvFRT9YcKVuOHPilMGdLM08i7JhYTdd4Aqq0xSK.png","path":"product_images\/nxvFRT9YcKVuOHPilMGdLM08i7JhYTdd4Aqq0xSK.png"},"quantity":{"max":1000000,"min":1},"price":4.99,"currency":"USD","stock_warning":5,"type":"account","stock":0},{"id":"X5kmV3z","title":"Lamm","image":{"url":"https:\/\/s3-storage.\/\/product_images\/rgGnCcqMIqgvsETsVTWjrF2EhVSwIOE5bXNwEtUC.jpeg","path":"product_images\/rgGnCcqMIqgvsETsVTWjrF2EhVSwIOE5bXNwEtUC.jpeg"},"quantity":{"max":1000000,"min":5},"price":0.05,"currency":"USD","stock_warning":0,"type":"account","stock":0},{"id":"AFwEIkN","title":"Lammy","image":{"url":"https:\/\/s3-storage.\/\/product_images\/mUbSzYsq8iII9Ro3wmJkyWRwk4oBVJmDNKavBGr4.png","path":"product_images\/mUbSzYsq8iII9Ro3wmJkyWRwk4oBVJmDNKavBGr4.png"},"quantity":{"max":1000000,"min":1},"price":1,"currency":"USD","stock_warning":0,"type":"account","stock":0},{"id":"JYcUni4","title":"Hulu","image":{"url":"https:\/\/s3-storage.\/\/product_images\/GaVbNGASgkv6LCErOjg1zK9MFWlrySvZGUVNCjPb.png","path":"product_images\/GaVbNGASgkv6LCErOjg1zK9MFWlrySvZGUVNCjPb.png"},"quantity":{"max":1000000,"min":5},"price":0.5,"currency":"USD","stock_warning":10,"type":"account","stock":14}],"groups":[{"id":"4TlYivf","title":"Minecraft Is","image":"","unlisted":false,"products":["sYu98Kq","JucpDM8","qX4vLC2","7efhwnl","kBkgLAv"]},{"id":"GTXwlkU","title":"Streaming","image":"","unlisted":false,"products":["JYcUni4","AFwEIkN","X5kmV3z"]}],"feedbacks":[{"updated_at":"2018-09-16 00:29:35","stars":5,"rating":1,"comment":"The best thing in the world! Thanks!","response":"Thank you so much! Come again!","product":null}],"online":{"state":false,"ago":"1 year ago"},"staff":false}},{"status":true,"user":{"username":"bear","avatar":"\/images\/default-avatar.png?id=55b864e42ffc120971da","rep":{"positive":0,"neutral":0,"negative":0},"products":[{"id":"p9YymiV","title":"fortnite","image":null,"quantity":{"min":1,"max":1},"price":75,"currency":"EUR","stock_warning":0,"type":"service","stock":9223372036854775807}],"groups":[],"feedbacks":[],"online":{"state":false,"ago":"1 year ago"},"staff":false}},{"status":true,"user":{"username":"king","avatar":"\/images\/default-avatar.png?id=55b864e42ffc120971da","rep":{"positive":0,"neutral":0,"negative":0},"products":[{"id":"91vFhN4","title":"uplay with 40 game","image":null,"quantity":{"min":1,"max":1},"price":4,"currency":"EUR","stock_warning":0,"type":"account","stock":0},{"id":"vX5IJ4T","title":"spotify premium","image":null,"quantity":{"min":1,"max":1},"price":2,"currency":"EUR","stock_warning":0,"type":"service","stock":9223372036854775807}],"groups":[],"feedbacks":[],"online":{"state":false,"ago":"1 year ago"},"staff":false}}]';
$json_decoded = json_decode($str,true);
foreach($json_decoded as $index => $user_data){
foreach($user_data['user']['products'] as $product_index => $product_data){
if($product_data['stock'] == 0){
unset($json_decoded[$index]['user']['products'][$product_index]);
}
}
}
echo json_encode($json_decoded);
我认为“产品”键中还有另一个数组。
因此,您必须在$value[“产品”]上添加另一个foreach
检查该产品的库存是否为0,并根据该值修改代码
如果您只需在第一个代码块中取消设置该值,这样您就不需要为每个代码块设置另一个值,这会更好
$data = json_decode($data,true);
foreach ($data as $key => $value) {
foreach ($value['user']['products'] as $k => $v) {
if($v['stock'] == 0){
//unset it
}
}
}
我认为您在“产品”键中有另一个数组。
因此,您必须在$value[“产品”]上添加另一个foreach
检查该产品的库存是否为0,并根据该值修改代码
如果您只需在第一个代码块中取消设置该值,这样您就不需要为每个代码块设置另一个值,这会更好
$data = json_decode($data,true);
foreach ($data as $key => $value) {
foreach ($value['user']['products'] as $k => $v) {
if($v['stock'] == 0){
//unset it
}
}
}
你能分享一个json样本吗?当然,我会更新我的帖子。你能分享一个json样本吗?当然,我会更新我的帖子。你太棒了,我不知道为什么我不能对数组结构保持清醒的头脑。我得到了完美的结果,直到有了新的东西。太感谢你了。@JasonWaltz很高兴我能帮上忙:)你太棒了,我不知道为什么我不能把我的头绕在阵列结构上。我得到了完美的结果,直到有了新的东西。太感谢你了。@JasonWaltz很高兴我能帮忙:)