Search code examples
phparraysarray-push

Make new array from specifed string?


I have this kind of simple array:

$puctures=array('1_noname.jpg','2_new.jpg','1_ok.jpg','3_lets.jpg','1_stack.jpg','1_predlog.jpg','3_loli.jpg');

I want to make new array that i will only have elements thats starts with 1_ Example

$new=array('1_noname.jpg','1_ok.jpg','1_stack.jpg','1_predlog.jpg');

Something like array_pop but how?


Solution

  • A simple loop will do.

    foreach ($pictures as $picture) {
        if (substr($picture, 0, 2) == "1_") {
            $new[] = $picture;
        }
    }