17

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

Potherca
  • 13,207
  • 5
  • 76
  • 94
user1858162
  • 297
  • 1
  • 2
  • 6
  • 3
    no you cannot remove the keys, arrays will always have a key – Kevin Sep 23 '14 at 08:58
  • There is no reason why you would want to remove the keys of an array. What do you want to do? Maybe we can provide a good solution for your problem then. – Xatenev Sep 23 '14 at 09:04
  • An array is a collection of key/value pairs.... you can't remove a key without removing the value as well, unless you simply want a string instead: `$myData = implode(",\n", $myArray);` – Mark Baker Sep 23 '14 at 09:04
  • Removing keys doesn't make any sense, that's the point of an array, otherwise how would you iterate it ? If what you want to get is Array ( Title: => Warmly...., Snippet: Testing, etc...) Then you should loop your strings, Explode them on ":" and create a new array with the first part of each of these strings as keys and the second part as values. –  Sep 23 '14 at 09:04
  • You don't need to remove the keys from the array. Just use it without them, for example: `foreach ($myArray as $value){/*do anything you wish the $value, it's your array element without the key*/}` – beerwin Sep 23 '14 at 09:04
  • I have updated my question with actual array,, i want to fetch data using foreach loop ,but not nested foor loop – user1858162 Sep 23 '14 at 09:15

5 Answers5

42

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.

Pedro Amaral Couto
  • 2,056
  • 1
  • 13
  • 15
0

Is it possible.? No its not possible.You cannot remove the keys from arrays.If you want a string like that then you can try with implode function in php like this.

<?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);
?>

Which will produce an output like this

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

You can check the documentation of implode here for more info.

웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
0

No, arrays are a way of holding key/value pairs. If you just want to output each of those lines of text, iterate through the array using foreach.

danmullen
  • 2,556
  • 3
  • 20
  • 28
0

A dirty method, if you want to just want to remove the keys, don't care about the particular order of things afterwards (or do want them sorted by the function) and just need things listed in an array starting at 0, then you can use sort() on the array.

I say dirty because this forces it to sort things as well as just removing the array keys and reassigning them, but it's any easy way to get it done on a small array.

Eric
  • 476
  • 2
  • 8
  • 20
-2

If you want to define the keys yourself, create an array using this method:

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

This will give you

Array ( [title] => Warmly... [snippet] => Testing [category] => Member )
Andy C
  • 119
  • 1
  • 9