PHP通过值删除数组索引

时间:2015-12-18 13:26:30

标签: php arrays unset

我试图取消设置两个包含两个值的特定数组位置。

我填充数组的实际代码。

addOne(number:)

该数组看起来像下面的错误使用print_r函数回显:

function get_store_list(){
    $data = @file_get_contents('http://www.zwinky.com/xml/clothingList.xml');
    $data = @simplexml_load_string($data);
    $data = $data->xpath('//stores/store');

    foreach($data as $store) {
        $storeArray[] = array(
           "name" => (string)$store['name'],
           "id"   => (string)$store['id']
        );
     }

    return $storeArray;
}

$store = get_store_list();

现在Array ( [0] => Array ( [name] => Artizans [id] => 20037336 ) [1] => Array ( [name] => Bwabies! [id] => 20080134 ) [2] => Array ( [name] => Crave Mart [id] => 20097365 ) [3] => Array ( [name] => David & Goliath [id] => 20099998 ) [4] => Array ( [name] => Domo [id] => 20098166 ) [5] => Array ( [name] => Emily the Strange [id] => 20101926 ) [6] => Array ( [name] => Garfield [id] => 20098167 ) [7] => Array ( [name] => Jetsetter [id] => 26 ) [8] => Array ( [name] => Like Dat [id] => 3 ) [9] => Array ( [name] => Paris Hilton [id] => 21 ) [10] => Array ( [name] => Peppermint Place [id] => 12 ) [11] => Array ( [name] => Rocawear [id] => 19 ) [12] => Array ( [name] => ShoeBuy [id] => 10 ) [13] => Array ( [name] => Skelanimals [id] => 20100198 ) [14] => Array ( [name] => Snoop Dogg [id] => 20 ) [15] => Array ( [name] => SW&TH [id] => 20096121 ) [16] => Array ( [name] => The Castle [id] => 1 ) [17] => Array ( [name] => The Lair [id] => 4 ) [18] => Array ( [name] => The Mix [id] => 923 ) [19] => Array ( [name] => The Powerpuff Girls [id] => 20098121 ) [20] => Array ( [name] => The Surf Shop [id] => 5 ) [21] => Array ( [name] => Tie The Knot [id] => 20076231 ) [22] => Array ( [name] => tokidoki [id] => 20099224 ) [23] => Array ( [name] => University Club [id] => 2 ) [24] => Array ( [name] => Z Avenue [id] => 6 ) [25] => Array ( [name] => Z's Greetings [id] => 20099506 ) ) 确实包含2个我必须删除的数组索引。以下是哪些ID:21和20076231

我已经尝试了以下内容:

Array search code没有取得成功。有谁知道我可以尝试什么?

5 个答案:

答案 0 :(得分:1)

这个简单问题有一些不同的方法。其中一个可以使用函数array_filter()

/**
 * @param array $list        the list to process
 * @param array $IDsToRemove the IDs of elements to remove from $list
 * @return array a subset of $list that does not contain elements having 'id' in $IDsToRemove
 */
function removeFromArray(array $list, array $IDsToRemove)
{
    return array_filter(
        // Filter the input list...
        $list,
        // ... using a function...
        function (array $item) use ($IDsToRemove) {
            // ... that accepts an element if its "id" is not in $IDsToRemove
            return ! in_array($item['id'], $IDsToRemove);
        }
    );
}

// Usage
$filteredStore = removeFromArray($store, array(21, 20076231));

答案 1 :(得分:0)

如果您需要在创建项目后取消设置项目,请查看--- title: "Untitled" runtime: shiny output: html_document --- ```{r, echo=FALSE} inputPanel( numericInput("n_digits", label = "Number of digits:", min = 0, max = 5, value = 1) ) renderTable({ a<-rep(1.1,3) b<-rep(2.22,3) c<-rep(3.333,3) cbind(a,b,c) }, digits=c(0,rep(input$n_digits,3))) ```

首先映射数组以检索每个ID的索引。

array_map

然后从地图中获取您的ID索引(例如21)。

$map = array_map(function($item){ return $item['id']; }, $store);

然后使用$index = array_search(21, $map); 删除。

array_splice

答案 2 :(得分:0)

在你的循环中尝试这个,这不会包含在你的数组中,而不需要像这样解除设置:

TEST_METHOD(Average_Gradient) {
        int x1 = 683675;
        int x2 = x1 + 86400;
        float gradient = averageGradient(x1, x2);
        float answer = 0.001145833;
        Assert::AreEqual(answer, gradient);
    }

答案 3 :(得分:0)

为什么不直接在$ storeArray中使用id?因为它似乎是整数,你为什么强迫它(字符串)?

试试这个:

function get_store_list(){
    $data = @file_get_contents('http://www.zwinky.com/xml/clothingList.xml');
    $data = @simplexml_load_string($data);
    $data = $data->xpath('//stores/store');

    foreach($data as $store) {
        $storeArray[(int)$store['id']] = array(
           "name" => (string)$store['name']
        );
     }

    return $storeArray;
}
// delete the keys you want
unset ($storeArray[21], $storeArray[20076231]);
// or if you have more ids to delete you can create a deleteArray
$deleteArray = array(2, 20076231);
foreach ($deleteArray as $toDelete){
    unset($storeArray($toDelete);
}

答案 4 :(得分:0)

一行代码很酷,但有时显式代码更合适。

function filter_by_id(array $data, $id)
{
        foreach ($data as $k => &$v) {
                foreach ((array) $id as $i) {
                        if ($v['id'] === $i) {
                                $v = null;
                        }
                }
        }

        // 'array_filter()' produces a new array without the null entries.
        // 'array_values()' produces a new array with indexes without gaps.
        return array_values(array_filter($data));
}

您可以按时间过滤一个ID

$store = filter_by_id($store, 21);

或者您可以同时过滤多个ID:

$store = filter_by_id($store, [21, 20076231]);