Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove keys from php array

Tags:

arrays

php

My array is:

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

I want to remove keys 2,3,4 and 5, so that it should become like this:

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

My actual array is:

    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
                                                )

                                        )

                                )

                        )

                )

        )

)

Is it possible,,please suggest

like image 296
user1858162 Avatar asked Sep 23 '14 08:09

user1858162


People also ask

How do I remove a key from an array?

Using unset() Function: The unset() function is used to remove element from the array. The unset function is used to destroy any other variable and same way use to delete any element of an array. This unset command takes the array key as input and removed that element from the array.

What is array_keys () used for in PHP?

The array_keys() function returns an array containing the keys.

How do you delete associative array in PHP?

Answer: Use the PHP unset() Function If you want to delete an element from an array you can simply use the unset() function. The following example shows how to delete an element from an associative array and numeric array.

How do you delete an array in PHP?

In order to remove an element from an array, we can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically automatically.


1 Answers

Although an array in PHP is "a type that associates values to keys" and some say it's not possible to remove the keys, you might, in a sense, remove the keys from an array either replacing the keys or replacing the array with another array.

I suppose you would like to change the array to an array equivalent to an array initialized without explicit keys. For instance, you might have this array:

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

But you'd like it to be equivalent to:

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

It's possible to do it using the function "array_values", like this:

$arr = array_values($arr);

Notice, though, the array still has keys: 0, 1, 2, and 3.

It's important to note that the integer keys are not the same as the array indexes. For instance, this piece of code:

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

will output:

3 4 1 2

The function "array_values" might be used to make sure the keys values and indexes are the same:

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

I explained how to "remove" the keys, but you might want to implement a class for the topics, make an interface for topic contents (animations, images, etc.) and check the SPL data structures.

like image 151
Pedro Amaral Couto Avatar answered Sep 22 '22 06:09

Pedro Amaral Couto