2

for example have such a array:

Array
(
    [ID] => 4
    [DATE_CREATE] => 04.10.2013 20:47:52
    [NAME] => BCAA
    [DEPTH_LEVEL] => 1
    [DESCRIPTION] => 
    [SEARCHABLE_CONTENT] => BCAA
    [CODE] => bcaa
    [DETAIL_PICTURE] =>
)

how to remove some of the array keys to eventually receive the following:

Array
(
    [ID] => 4
    [DATE_CREATE] => 04.10.2013 20:47:52
    [NAME] => BCAA
    [DEPTH_LEVEL] => 1
)

Thanks in advance.

3
  • He's not asking how they got removed, he wants to remove them and is asking how to do it. Commented Dec 29, 2013 at 10:16
  • Won't unset($arr['description']); etc. do what you want? Commented Dec 29, 2013 at 10:17
  • How did you get the result array? Which keys are you trying to remove? (based on what condition?) Can you show us your code? Commented Dec 29, 2013 at 10:21

7 Answers 7

7

Use unset:

unset($array['DESCRIPTION']);
unset($array['SEARCHABLE_CONTENT'];
... and so on
Sign up to request clarification or add additional context in comments.

1 Comment

Hmmmm unset($array['DESCRIPTION'], $array['SEARCHABLE_CONTENT'], ...). The same goes for isset(), it supports multiple variables as parameter :)
5

Well, there is a function named array_diff_key():

$result = array_diff_key(
    $array,
    [
        'DESCRIPTION' => 0,
        'SEARCHABLE_CONTENT' => 0,
        'CODE' => 0,
        'DETAIL_PICTURE' => 0
    ]
);

UPDv1:

There is a way, mentioned by @BluePsyduck:

$result = array_diff_key(
    $array,
    array_flip([ 'DESCRIPTION', 'SEARCHABLE_CONTENT', 'CODE', 'DETAIL_PICTURE' ])
);

Comments

3

Just use array_slice function, its perfect for this situation.

$sliced = array_slice($input, 0, 3);

Comments

3

Use unset().

unset($array['SEARCHABLE_CONTENT']);

In this way unset the keys you don't need.

Comments

3

Make use of array_slice , You don't need to unset the keys here as you are assigning the array returned from your array_slice() directly to your same array.

<?php
$arr=Array
(
    'ID' => 4,
    'DATE_CREATE' => '04.10.2013 20:47:52',
    'NAME' => 'BCAA',
    'DEPTH_LEVEL' => 1,
    'DESCRIPTION' => NULL,
    'SEARCHABLE_CONTENT' => 'BCAA',
    'CODE' => 'bcaa',
    'DETAIL_PICTURE' => ''
);
$arr = array_slice($arr, 0, 4);

print_r($arr);

OUTPUT:

Array
(
    [ID] => 4
    [DATE_CREATE] => 04.10.2013 20:47:52
    [NAME] => BCAA
    [DEPTH_LEVEL] => 1
)

Comments

2

To remove from array some value by key just:

  $someKey= array_search('someKey', $array);
    unset($array[$someKey]);

Or just:

 unset($array[$someKey])

For example :

unset($array['DESCRIPTION']);
unset($array['SEARCHABLE_CONTENT'];

Or in your case:

$result = array_slice($input, 0, 3);

Comments

1

If you want to define the keys to be removed, you may use this:

$array = array('foo' => 'bar', 'hello' => 'world', 'blue' => 'psyduck', 'abc' => 'def');
$reducedArray = array_diff_key($array, array_flip(array('blue', 'abc')));

If you want to define the keys to be kept, you may use this:

$array = array('foo' => 'bar', 'hello' => 'world', 'blue' => 'psyduck', 'abc' => 'def');
$reducedArray = array_intersect_key($array, array_flip(array('foo', 'hello')));

In both examples, the resulting array is:

var_dump($reducedArray);
// array(2) { ["foo"]=> string(3) "bar" ["hello"]=> string(5) "world" }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.