Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I got multidimensional array. From each subarray, I would like to remove / unset values with index 1. My array $data.

Array
(
    [3463] => Array
        (
            [0] => 1
            [1] => 2014
            [context] => 'aaa'
        )

    [3563] => Array
        (
            [0] => 12
            [1] => 2014
            [context] => 'aaa'
        )       

    [2421] => Array
        (
            [0] => 5
            [1] => 2014
            [context] => 'zzz'
        )               
)

I would like to remove every element with index '1' from subarrays. Desired output is:

Array
(
    [3463] => Array
        (
            [0] => 1
            [context] => 'aaa'
        )

    [3563] => Array
        (
            [0] => 12
            [context] => 'aaa'
        )       

    [2421] => Array
        (
            [0] => 5
            [context] => 'zzz'
        )               
)

Why this does not work?

foreach ($data as $subArr) {
   foreach ($subArr as $key => $value) {
       if ($key == '1') {
        unset($subArr[$key]);
       }

   }
}

I'm sorry if this problem is trivial for you guys.

share|improve this question
2  
it is because foreach makes a copy of the element and does not work on the original array you can avoid this by assigning by reference. In order to be able to directly modify array elements within the loop precede $subArr with &. In that case the value will be assigned by reference. – Benjamin Dec 15 '14 at 11:26
up vote 3 down vote accepted

easy way!? you can do this just with one foreach!

foreach ($data as $key => $subArr) {
    unset($subArr['1']);
    $data[$key] = $subArr;  
}
share|improve this answer
    
I am ashamed ;-) Thank you! – suz Dec 15 '14 at 11:32

you are making changes in subarray instead of main one try this may help

foreach ($data as $key => $subArr) { 
    unset($data[$key][1]);      
}
share|improve this answer

try this:

<?php 
    $data = Array
    (
        '3463' => Array
            (
                '0' => 1,
                '1' => 2014,
                'context' => 'aaa'
            ),

        '3563' => Array
            (
                '0' => 12,
                '1' => 2014,
                'context' => 'aaa'
            ),       

        '2421' => Array
            (
                '0' => 5,
                '1' => 2014,
                'context' => 'zzz'
            )               
    );

    foreach ($data as $k=>$subArr) {
        foreach ($subArr as $key => $value) {

            if ($key == '1') {
                unset($data[$k][$key]);
            }

        }
    }
print_r($data);// display the output
share|improve this answer
    
So pretty. Thank you! I will accept your answer in 7 minutes. – suz Dec 15 '14 at 11:26
    
@suz your welcome...... – Suchit Dec 15 '14 at 11:56
    
I've accepted answer from 'Pouya Darabi' - it's shorter. Thanks anyway for a quick help! – suz Dec 15 '14 at 12:07
    
no problem.your welcome. – Suchit Dec 15 '14 at 12:07

It does not work because $subArr from the outer foreach contains copies of the values of $data and the inner foreach modifies these copies, leaving $data not touched.

You can fix that by telling PHP to make $subArr references to the original values stored in $data:

foreach ($data as &$subArr) {
   foreach ($subArr as $key => $value) {
       if ($key == '1') {
        unset($subArr[$key]);
       }
   }
}

Another option is to use function array_map(). It uses a callback function that can inspect (and modify) each value of $data and it returns a new array.

$clean = array_map(
    function (array $elem) {
        unset($elem['1']);        // modify $elem
        return $elem;             // and return it to be put into the result
    },
    $data
);

print_r($clean);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.