Jump to content

searching array


tonax99

Recommended Posts

Hi, I'm trying to figure out a way to search my array for any instance of 'images/smilies/' and remove that key from the array. I tried using search_array but it seems it only looks for exact matches, which wont work because the key will have 'images/smilies/rofl.gif' for example.

 

here's an example of my array output. it's a multidimensional array so these are the results of my print_r (imgmatches[1]);

 

Array
(
    [0] => http://i25.photobucket.com/albums/c62/becominglucid/Snapshot_20090529.jpg
)
<br>Array
(
    [0] => images/smilies/frown.gif
)
<br>Array
(
    [0] => http://i25.photobucket.com/albums/c62/becominglucid/Snapshot_20090529_7.jpg
    [1] => images/smilies/frown.gif
)

 

it seems so simple but my knowledge is pretty limited

Link to comment
Share on other sites

Try this:

 

<?php
$arr = Array("images/smilies/rofl.gif", "don't remove", Array("images/smilies/something.gif", "don't remove", "don't remove"));

function removeRec(&$arr)
{
foreach($arr as $key => &$part)
{
	if(is_array($part))
		removeRec($part);
	else if(!stristr($part, "images/smilies") === false)
		unset($arr[$key]);
}
}
echo "<pre>";
print_r($arr);
removeRec($arr);
print_r($arr);
echo "</pre>";
?>

 

Output:

 

Array
(
    [0] => images/smilies/rofl.gif
    [1] => don't remove
    [2] => Array
        (
            [0] => images/smilies/something.gif
            [1] => don't remove
            [2] => don't remove
        )

)

Array
(
    [1] => don't remove
    [2] => Array
        (
            [1] => don't remove
            [2] => don't remove
        )

)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.