【问题标题】:Remove part of an array if its not contain如果不包含数组的一部分,则删除它
【发布时间】:2015-01-16 18:04:30
【问题描述】:

我在 php 中有一个名为 $json 的数组。看起来是这样的

Array ( 
[0] => Array ( 
    [name] => Knife 
    [url] => http://steamcommunity.com/market/listings/753/297120... 
    [price] => 0.16 USD 
    [image] => http://steamcommunity-a.akamaihd.net/economy/imag...
    [quantity] => 30 
    [game] => Counter-Strike: Global Offensive ) 

[1] => Array ( 
    [name] => Strange Knife 
    [url] => http://steamcommunity.com/market/listings/440/Strange%2...
    [price] => 0.55 USD 
    [image] => http://steamcommunity-a.akamaihd.net/economy/imag... 
    [quantity] => 177 
    [game] => Team Fortress 2 ) 

[2] => Array ( 
    [name] => Festive Knife 
    [url] => http://steamcommunity.com/market/listings/440/Festive%20Knife" id="resultlink_2 
    [price] => 3.72 USD 
    [image] => http://steamcommunity-a.akamaihd.net/economy/image/fWFc82js0fmoRAP-qOIPu5THSWqffx
    [quantity] => 66 
    [game] => Team Fortress 2 ) 

[3] => Array ( 
    [name] => â… Flip Knife 
    [url] => http://steamcommunity.com/market/listings/7
    [price] => 3.72 USD 
    [image] => http://steamcommunity-a.akamaihd.net/economy/image/fWFc82js0fmoRAP-qOIPu5THSWqffx
    [quantity] => 24 
    [game] => Counter-Strike: Global Offensive ) 

...

如何删除 GAME != "Counter-Strike: Global Offensive" 的数组的一部分?

【问题讨论】:

  • 你有没有尝试过,或者你只是想让我们做你的功课?!
  • $json = array_filter($json, function($value) { return $value['game'] === 'Counter-Strike: Global Offensive'; });
  • 对数组的每个元素使用“strpos”PHP函数:php.net/manual/ro/function.strpos.php
  • 感谢重播。这不是我的作业,我自己做的。我在 stackoverflow 和 google 上搜索了很多,但找不到答案。
  • @MarkBaker 它运行良好。非常感谢。

标签: php arrays remove-if


【解决方案1】:
$array; // Your array.

foreach($array as $subarray) {
    if($subarray['game'] != "Counter-Strike: Global Offensive") {
        unset($subarray);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多