0

My array looks like this:

Array
(
[1] => Array
    (
        [0] => /webmail/
        [1] => 42024
        [2] => 246538196
        [3] => 72
        [4] => 70
    )

[2] => Array
    (
        [0] => /public/index.php
        [1] => 6022
        [2] => 2575777
        [3] => 40
        [4] => 153
    )

[3] => Array
    (
        [0] => /
        [1] => 293
        [2] => 5326453
        [3] => 184
        [4] => 76
    )

[4] => Array
    (
        [0] => /webmail/skins/larry/watermark.html
        [1] => 248
        [2] => 40225
        [3] => 0
        [4] => 2
    )

[5] => Array
    (
        [0] => /webmail/program/resources/blank.tif
        [1] => 182
        [2] => 29406
        [3] => 0
        [4] => 1
    )

and so on... When I want to remove the every element that ends with .tif/.js/.css/.woff I just use :

$key = array_search('*.tif',$sider); // for example for .tif
unset($sider[$key]);

but it doesn't work ! What the problem in my code and how to delete with multiple condition like for all the strings that end with .tif/.js/.css/.woff.

Thanks

4 Answers 4

4

array_search doesn't work, since each element of the array is an array, and an array doesn't match "*.tif". Besides, array_search does not support wildcards like *. An array filter would be the typical approach here:

$sider = array_filter($sider, function (array $element) {
    return !preg_match('/\.tif$/i', $element[0]);
});
Sign up to request clarification or add additional context in comments.

3 Comments

damn you, I was supposed to first on this one! my code woulda been longer though, nice
Don't be discouraged to post your answer... :)
naa, I already spent two minutes typing and then deleted it when I read you answer and I just got back to this page now, so it's whatever I guess, but thanks
1

Loop through the values, if the extension is in an array of disallowed extensions, unset the element.

$remove_extensions = array('tif', 'js');
foreach($arr as $key => $value) {
    if(in_array(pathinfo($value[0], PATHINFO_EXTENSION), $remove_extensions)) { unset($arr[$key]); }
}

Comments

1
<?php

function removeDeep($array, $pattern) {

    $r = array();

    if (!is_array($array)) {
        trigger_error(__FUNCTION__ . ' expected paramater 1 to be an array.', E_USER_WARNING);
        return false;
    }

    foreach ($array as $key => $value) {

        if (is_array($value)) {
            $r[$key] = call_user_func(__FUNCTION__, $value, $pattern);
            continue;
        }

        if (!preg_match($pattern, $value)) {
            $r[$key] = $value;
        }

    }

    return $r;

} 

$sider = removeDeep( $sider, "*.tif");
?> 

Comments

0

Try smth like

var_dump(
    array_filter(
        $array,
        function ($row) {
            return preg_match('/\.(tif|css|js|woff))$/i', $row[0]) === 1;
        }
    )
);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.