3

I'm sure I'm only facing a logical problem here.

I have an array like this :

var_export($myarray);

array (
  0 => 
  array (
    'inf:name' => 'aaa',
    'inf:value' => '111',
    'inf:children' => NULL,
  ),
  1 => 
  array (
    'inf:name' => 'bbb',
    'inf:value' => '222',
    'inf:children' => NULL,
  ),
  2 => 
  array (
    'inf:name' => 'ccc',
    'inf:value' => '333',
    'inf:children' => NULL,
  ),
  3 => 
  array (
    'inf:name' => 'bob',
    'inf:children' => 
    array (
      0 => 
      array (
        'inf:name' => 'ddd',
        'inf:value' => '444',
        'inf:children' => NULL,
      ),
    ),
  ),
  4 => 
  array (
    'inf:name' => 'data',
    'inf:children' => 
    array (
      0 => 
      array (
        'inf:name' => 'eee',
        'inf:value' => '555',
        'inf:children' => NULL,
      ),
      1 => 
      array (
        'inf:name' => 'steve',
        'inf:value' => 'bar1',
        'inf:children' => NULL,
      ),
      2 => 
      array (
        'inf:name' => 'john',
        'inf:value' => 'bar2',
        'inf:children' => NULL,
      ),
      3 => 
      array (
        'inf:name' => 'peter',
        'inf:value' => 'bar3',
        'inf:children' => 
        array (
          0 => 
          array (
            'inf:name' => 'fff',
            'inf:value' => '666',
            'inf:children' => NULL,
          ),
        ),
      ),
    ),
  ),
)


//Or using print_r()
print_r($myarray);

Array
(
    [0] => Array
        (
            [inf:name] => aaa
            [inf:value] => 111
            [inf:children] => 
        )

    [1] => Array
        (
            [inf:name] => bbb
            [inf:value] => 222
            [inf:children] => 
        )

    [2] => Array
        (
            [inf:name] => ccc
            [inf:value] => 333
            [inf:children] => 
        )

    [3] => Array
        (
            [inf:name] => bob
            [inf:children] => Array
                (
                    [0] => Array
                        (
                            [inf:name] => ddd
                            [inf:value] => 444
                            [inf:children] => 
                        )

                )

        )

    [4] => Array
        (
            [inf:name] => data
            [inf:children] => Array
                (
                    [0] => Array
                        (
                            [inf:name] => eee
                            [inf:value] => 555
                            [inf:children] => 
                        )

                    [1] => Array
                        (
                            [inf:name] => steve
                            [inf:value] => bar1
                            [inf:children] => 
                        )

                    [2] => Array
                        (
                            [inf:name] => john
                            [inf:value] => bar2
                            [inf:children] => 
                        )

                    [3] => Array
                        (
                            [inf:name] => peter
                            [inf:value] => bar3
                            [inf:children] => Array
                                (
                                    [0] => Array
                                        (
                                            [inf:name] => fff
                                            [inf:value] => 666
                                            [inf:children] => 
                                        )

                                )

                        )

                )

        )
)

I would like to remove some keys when inf:name matches the given names.

Here is my test function :

function array_cleanup( $array, $todelete )
{
    foreach( $array as $key => $value )
    {
        if( is_array( $value ) )
        {
            $array[$key] = array_cleanup( $array[ $key ], $todelete );
        } else {
            if( sizeOf( $todelete ) > 0 )
            {
                if ( in_array( $value[ 'inf:name' ], $todelete ) )
                    unset( $array[ $key ] );
            }
        }
    }
    return $array;
}

$newarray = array_cleanup( $myarray, array("aaa", "peter", "ccc") );

The desired output must be :

var_dump($newarray);

array (
  0 => 
  array (
    'inf:name' => 'bbb',
    'inf:value' => '222',
    'inf:children' => NULL,
  ),
  2 => 
  array (
    'inf:name' => 'bob',
    'inf:children' => 
    array (
      0 => 
      array (
        'inf:name' => 'ddd',
        'inf:value' => '444',
        'inf:children' => NULL,
      ),
    ),
  ),
  4 => 
  array (
    'inf:name' => 'data',
    'inf:children' => 
    array (
      0 => 
      array (
        'inf:name' => 'eee',
        'inf:value' => '555',
        'inf:children' => NULL,
      ),
      1 => 
      array (
        'inf:name' => 'steve',
        'inf:value' => 'bar1',
        'inf:children' => NULL,
      ),
      2 => 
      array (
        'inf:name' => 'john',
        'inf:value' => 'bar2',
        'inf:children' => NULL,
      ),
    ),
  ),
)

//or using print_r();
print_r($newarray);
Array
(
    [1] => Array
        (
            [inf:name] => bbb
            [inf:value] => 222
            [inf:children] => 
        )

    [2] => Array
        (
            [inf:name] => bob
            [inf:children] => Array
                (
                    [0] => Array
                        (
                            [inf:name] => ddd
                            [inf:value] => 444
                            [inf:children] => 
                        )

                )

        )

    [3] => Array
        (
            [inf:name] => data
            [inf:children] => Array
                (
                    [0] => Array
                        (
                            [inf:name] => eee
                            [inf:value] => 555
                            [inf:children] => 
                        )

                    [1] => Array
                        (
                            [inf:name] => steve
                            [inf:value] => bar1
                            [inf:children] => 
                        )

                    [2] => Array
                        (
                            [inf:name] => john
                            [inf:value] => bar2
                            [inf:children] => 
                        )


                )

        )
)

The $newarray keys must be reset. Because I might face other problems later if they aren't reset.

Thank you guys.

3
  • How many levels deep can the inf:children be nested? Commented Dec 15, 2010 at 20:37
  • @stereofrog : check update, @thirtydot : It shouldn't be more then four levels but I believe the function should be called recursively so it can be adapted to any inputs. Commented Dec 15, 2010 at 20:56
  • @Ajreal - Yes indeed it's XML data converted into PHP Array. Commented Dec 15, 2010 at 22:01

2 Answers 2

1

This seems to work:

<?php

$myarray = array (
  0 => 
  array (
    'inf:name' => 'aaa',
    'inf:value' => '111',
    'inf:children' => NULL,
  ),
  1 => 
  array (
    'inf:name' => 'bbb',
    'inf:value' => '222',
    'inf:children' => NULL,
  ),
  2 => 
  array (
    'inf:name' => 'ccc',
    'inf:value' => '333',
    'inf:children' => NULL,
  ),
  3 => 
  array (
    'inf:name' => 'bob',
    'inf:children' => 
    array (
      0 => 
      array (
        'inf:name' => 'ddd',
        'inf:value' => '444',
        'inf:children' => NULL,
      ),
    ),
  ),
  4 => 
  array (
    'inf:name' => 'data',
    'inf:children' => 
    array (
      0 => 
      array (
        'inf:name' => 'eee',
        'inf:value' => '555',
        'inf:children' => NULL,
      ),
      1 => 
      array (
        'inf:name' => 'steve',
        'inf:value' => 'bar1',
        'inf:children' => NULL,
      ),
      2 => 
      array (
        'inf:name' => 'john',
        'inf:value' => 'bar2',
        'inf:children' => NULL,
      ),
      3 => 
      array (
        'inf:name' => 'peter',
        'inf:value' => 'bar3',
        'inf:children' => 
        array (
          0 => 
          array (
            'inf:name' => 'fff',
            'inf:value' => '666',
            'inf:children' => NULL,
          ),
        ),
      ),
    ),
  ),
);





function array_cleanup($array, $todelete ) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            if (in_array($value['inf:name'], $todelete)) {
                unset($array[$key]);
            } else {
                $array[$key] = array_cleanup($array[$key], $todelete);
            }
        }
    }
    return $array;
}



$newarray = array_cleanup($myarray, array("aaa", "peter", "ccc"));
$newarray = array_values($newarray);

echo '<pre>';
var_export($newarray);
echo '</pre>';

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, this seems to work. Thank you. I will give it further tests tonight. :)
0
class Arr
{
    /**
     * Removes all occurences of {needle} in {subject}
     *
     * @return array
     */
    public static function purgeKey(string $needle, array $subject) : array
    {
        foreach ($subject as $key => $value) {
            if (is_array($value)) {
                $subject[$key] = static::purgeKey($needle, $value);
            } else if ($key === $needle) {
                unset($subject[$needle]);
            }
        }

        return $subject;
    }
}

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.