如何从php数组中删除密钥


How to remove keys from php array

我的数组是:

Array(
    2 => 'Title: Warmly little before cousin sussex entire set Blessing it ladyship.',
    3 => 'Snippet: Testing',
    4 => 'Category: Member',
    5 => 'Tags: little, before, entire',
)

我想删除键2,3,4和5,这样它应该变成这样:

Array(
    'Title: Warmly little before cousin sussex entire set Blessing it ladyship.',
    'Snippet: Testing',
    'Category: Member',
    'Tags: little before entire',
)

我的实际阵列是:

    Array
    (
    [0] => Array
        (
            [0] => Array
                (
                    [chapter_id] => 7
                    [version_id] => 1
                    [chapter_number] => 1
                    [Topics] => Array
                        (
                            [0] => Array
                                (
                                    [topic_id] => 8
                                    [content] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [content] => 
Far concluded not his something extremity. Want four we face an he gate. On he of played he ladies answer little though nature. Blessing oh do pleasure as so formerly. Took four spot soon led size you. Outlived it received he material. Him yourself joy moderate off repeated laughter outweigh screened. 

                                                    [video] => /videos/5420acec0c33a1411427564.mp4
                                                )
                                        )
                                )
                            [1] => Array
                                (
                                    [topic_id] => 8
                                    [content] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [content] => 
Improve him believe opinion offered met and end cheered forbade. Friendly as stronger speedily by recurred. Son interest wandered sir addition end say. Manners beloved affixed picture men ask. Explain few led parties attacks picture company. On sure fine kept walk am in it. Resolved to in believed desirous unpacked weddings together. Nor off for enjoyed cousins herself. Little our played lively she adieus far sussex. Do theirs others merely at temper it nearer. 

                                                    [animation] => /animation/5420ad255dccc1411427621.png
                                                )
                                        )
                                )
                        )
                )
            [1] => Array
                (
                    [chapter_id] => 7
                    [version_id] => 1
                    [chapter_number] => 1
                    [Topics] => Array
                        (
                            [0] => Array
                                (
                                    [topic_id] => 9
                                    [content] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [content] => 
Full he none no side. Uncommonly surrounded considered for him are its. It we is read good soon. My to considered delightful invitation announcing of no decisively boisterous. Did add dashwoods deficient man concluded additions resources. Or landlord packages overcame distance smallest in recurred. Wrong maids or be asked no on enjoy. Household few sometimes out attending described. Lain just fact four of am meet high. 

                                                    [image] => /images/5420ac306fb831411427376.JPG
                                                )
                                        )
                                )
                            [1] => Array
                                (
                                    [topic_id] => 9
                                    [content] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [content] => 
Post no so what deal evil rent by real in. But her ready least set lived spite solid. September how men saw tolerably two behaviour arranging. She offices for highest and replied one venture pasture. Applauded no discovery in newspaper allowance am northward. Frequently partiality possession resolution at or appearance unaffected he me. Engaged its was evident pleased husband. Ye goodness felicity do disposal dwelling no. First am plate jokes to began of cause an scale. Subjects he prospect elegance followed no overcame possible it on. 

                                                    [image] => /images/5420ac48aa7201411427400.jpg
                                                )
                                        )
                                )
                        )
                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [chapter_id] => 8
                    [version_id] => 1
                    [chapter_number] => 2
                    [Topics] => Array
                        (
                            [0] => Array
                                (
                                    [topic_id] => 10
                                    [content] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [content] => 
Him boisterous invitation dispatched had connection inhabiting projection. By mutual an mr danger garret edward an. Diverted as strictly exertion addition no disposal by stanhill. This call wife do so sigh no gate felt. You and abode spite order get. Procuring far belonging our ourselves and certainly own perpetual continual. It elsewhere of sometimes or my certainty. Lain no as five or at high. Everything travelling set how law literature. 

Mr oh winding it enjoyed by b

                                                    [image] => /images/5420d951b117b1411438929.jpg
                                                )
                                        )
                                )
                            [1] => Array
                                (
                                    [topic_id] => 10
                                    [content] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [content] => 
Is education residence conveying so so. Suppose shyness say ten behaved morning had. Any unsatiable assistance compliment occasional too reasonably advantages. Unpleasing has ask acceptance partiality alteration understood two. Worth no tiled my at house added. Married he hearing am it totally removal. Remove but suffer wanted his lively length. Moonlight two applauded conveying end direction old principle but. Are expenses distance weddings perceive strongly who age domestic. 

                                                    [video] => /videos/5420d99cb852e1411439004.mp4
                                                )
                                        )
                                )
                        )
                )
            [1] => Array
                (
                    [chapter_id] => 8
                    [version_id] => 1
                    [chapter_number] => 2
                    [Topics] => Array
                        (
                            [0] => Array
                                (
                                    [topic_id] => 11
                                    [content] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [content] => 
Wrote water woman of heart it total other. By in entirely securing suitable graceful at families improved. Zealously few furniture repulsive was agreeable consisted difficult. Collected breakfast estimable questions in to favourite it. Known he place worth words it as to. Spoke now noise off smart her ready. 

                                                    [image] => /images/5420d9cfa0a5f1411439055.jpg
                                                )
                                        )
                                )
                        )
                )
        )
)

有可能吗,请建议

尽管PHP中的数组是"将值与键相关联的类型";有些人说不可能删除键,从某种意义上说,你可能会从一个数组中删除键,要么替换键,要么用另一个数组替换数组。

我想您希望将数组更改为等效于在没有显式键的情况下初始化的数组的数组。例如,您可能有以下数组:

$arr = array(2 => 'a', 3 => 'b', 4 => 'c');

但你希望它相当于:

$arr = array('a', 'b', 'c');

可以使用函数";array_values";,像这样:

$arr = array_values($arr);

不过,请注意,数组仍然有键:0、1、2和3。

需要注意的是,整数键与数组索引不同。例如,这段代码:

<?php
$arr = array(3 => '', 4 => '', 1 => '', 2 => '');
foreach($arr as $k => $v):
    echo "$k ";
endforeach;

将输出:

3 4 1 2

函数";array_values";可以用来确保键值和索引相同:

<?php
    $arr = array(3 => '', 4 => '', 1 => '', 2 => '');
    foreach(array_values($arr) as $k => $v):
        echo "$k ";
    endforeach;
    // outputs: 0, 1, 2, 3

我解释了如何";删除";键,但您可能希望为主题实现一个类,为主题内容(动画、图像等)创建一个接口,并检查SPL数据结构。

Is it possible.?不,这是不可能的。不能从数组中删除键。如果您想要这样的字符串,那么您可以尝试在php中使用implode函数,如下所示。

<?php
$arr = array(
    2 => 'Title: Warmly little before cousin sussex entire set Blessing it ladyship.',
    3 => 'Snippet: Testing',
    4 => 'Category: Member',
    5 => 'Tags: little, before, entire'
);
echo implode(",'n", $arr);
?>

它将产生类似的输出

标题:温馨小之前表亲苏塞克斯整套祝福它女士。,代码段:测试,类别:成员,标签:小,之前,整个

你可以在这里查看内爆的文档以了解更多信息。

不,数组是保存键/值对的一种方式。如果您只想输出每一行文本,请使用foreach遍历数组。

一个脏方法,如果你只想删除键,不在乎后面的东西的特定顺序(或者确实想按函数排序),只需要在0开始的数组中列出东西,那么你可以对数组使用sort()

我之所以说脏,是因为这会迫使它对事物进行排序,同时只删除数组键并重新分配它们,但这是在小数组上完成这项工作的任何简单方法。

如果您想自己定义键,请使用以下方法创建一个数组:

$myArray = array ("title" => "Warmly...", "snippet" => "Testing", "category" => "Member");

这会给你

Array ( [title] => Warmly... [snippet] => Testing [category] => Member )