1

I got this array in PHP:

$arr =('1-1.jpg','1-2.jpg','11-3.jpg', '1-4.jpg', '3-5.jpg', '41-5.jpg','1-3.jpg','4-5.jpg','14-5.jpg','54-5.jpg','64-5.jpg','14-5.jpg', '1-5.jpg');

I need this array, but I have PHP 5.27 version on the server :(

$newarray=('1-1.jpg','1-2.jpg','1-3.jpg', '1-4.jpg', '1-5.jpg');

Forget the server version, the criteria is "1-". How do I get all elements that is starting only with "1-"?

4
  • 2
    It's not obvious how to reduce the big array to the small one. What is the criteria? PS: "but i have php 5.27 version on server :(" --- anything particularly wrong with 5.2.7? Commented Aug 13, 2012 at 20:48
  • @zerkms It is obvious for me. (I think 5.2.7 doesn't have [...], you have to use array(...).) Commented Aug 13, 2012 at 20:49
  • Like zerkms, you need to give us substantially more information than you've given us. Commented Aug 13, 2012 at 20:49
  • Do a string comparison on each element. If it matches the pattern you're looking for, add it to the second array. It's really not that complicated. Commented Aug 13, 2012 at 20:53

4 Answers 4

4

Use this code:

<?php
$arr = array('1-1.jpg','1-2.jpg','11-3.jpg', '1-4.jpg', '3-5.jpg', '41-5.jpg','1-3.jpg','4-5.jpg','14-5.jpg','54-5.jpg','64-5.jpg','14-5.jpg', '1-5.jpg');
$newarray = array();
foreach($arr as $item) {
    if(substr($item, 0, 2) == '1-') $newarray[] = $item;
}
sort($newarray); // Add this to sort the array
?>

You can use the sort function after the foreach to sort the array.

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

4 Comments

It's not every day the first two answers have exactly the same technique :)
"exactly the same technique" -- what amazing is in using substr for getting substring? The same answers happen a lot every day (for such trivial questions)
Tough crowd today. I was observing that @ShaquinTrifonoff had basically the same code within a couple seconds of each other.
@David - Normally with an easy question like this I will end up with the 1 New answer has been posted message when I am typing my answer.
1

Use preg_grep

$arr = array('1-1.jpg','1-2.jpg','11-3.jpg', '1-4.jpg', '3-5.jpg', '41-5.jpg','1-3.jpg','4-5.jpg','14-5.jpg','54-5.jpg','64-5.jpg','14-5.jpg', '1-5.jpg');

print_r(preg_grep('#^1-#', $arr));

demo: http://codepad.org/ipDmYEBI

6 Comments

I'm trying to learn regex but I can't figure out what the # symbols are for. Wouldn't preg_grep('/^1-/', $arr) work, without the hashtags? Thanks! This seems much nicer than doing it by substring.
@AlexKalicki, in php(and maybe perl too) the way you specify the regex pattern, you use pattern delimiters to separate the pattern from pattern option flags. In general, resources that teach regex don't talk much about this because its not common to all regex implementations in various programming languages. But yes, it would work correctly using / instead of #, but I use # out of habit because / often occurs as a litterla character in the pattern, and then you need to escape it with a backslash....and then its ugly to read.
Ah, got it. I knew about using pattern delimiters in PHP, but I didn't know there were more options for what you use than just forward slashes. :)
Some info: On average, when measured with microtime(true);, this method takes 4.6968460083008E-5 amount of time, the substr(); method (like my answer) takes 4.1007995605469E-5.
@ShaquinTrifonoff, interesting. I bet you though, that preg_grep pulls ahead on larger arrays. regex in php has a one-time overhead per pattern to compile the regex, but after that, everything executes in optimized c code, while your method is constantly switching between user interpreted php, and c code.
|
1

Another way would be to use PHP's array_filter method:

$arr = array('1-1.jpg','1-2.jpg','11-3.jpg', '1-4.jpg', '3-5.jpg', '41-5.jpg','1-3.jpg','4-5.jpg','14-5.jpg','54-5.jpg','64-5.jpg','14-5.jpg', '1-5.jpg');
$newArr = array_filter($arr, "filterArray"); // stores the filtered array

function filterArray($value){
    return (substr($value, 0, 2) == "1-");
}

1 Comment

I was just about to post another answer with this :-)
0
<?php
$new_array = array();
foreach ($old_array as $line) {
   if (substr($line, 0, 2) == "1-") {
      $new_array[] = $line;
   }
}
?>

This checks to see if the first two characters of each element is 1-, and if it is, adds it to the new array.

3 Comments

After this i got this Array ( [0] => 2-1.jpg [1] => 3-1.jpg [2] => 4-1.jpg [3] => 5-1.jpg [4] => 6-1.jpg )
@MiomirDancevic That's because this method adds every value that does not start with 1-. See my answer.
@David - The end result should be Array('1-1.jpg','1-2.jpg','1-3.jpg', '1-4.jpg', '1-5.jpg').

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.