tags:

views:

141

answers:

8
foreach ($myarray as $value)
  {
        if (strpos($value,'mysearchstring'))
             //add this $value to new array here
  }

Also where would the second array need to be declared and how?

+4  A: 
$newarray = array();

foreach ($myarray as $value) {
    if(strpos($value, 'mysearchstring') !== false) {
        $newarray[] = $value;
    }
}
Tatu Ulmanen
Beat me to it by 15 seconds!
Dave
+7  A: 

To avoid warnings, the second array should be declared like so

$newArray = array();

This could be incorporated into your code like so

$newArray = array();
foreach ($myarray as $value){
    if (strpos($value,'mysearchstring') !== false ){
         $newArray[] = $value;
    }
}

Your original code contained an error. strpos returns and false if it doesn't contain the needle, else it returns the index of the occurrence. So strpos($value,'mysearchstring') will return 0 if $value starts with 'mysearchstring' . As PHP will convert 0 to false and visa versa, it will not get added to the array if we use the standard comparisons, so we need to explicitly check that it is false without type conversion (converting 0 to false for example). To do this we use !== (Note the two =)s. This is represented in the code above.

For more information on comparison operators in php, see the documentation.

EDIT
Regarding the commend and usage of [], it ads the new element to the end of the array

The documentation states that:

if no key is specified [in the square brackets], the maximum of the existing integer indices is taken, and the new key will be that maximum value plus 1. If no integer indices exist yet, the key will be 0 (zero).

So if we have an array

$arr = array(5 => 1, 12 => 2);

//doing this
$arr[] = 56;

//is exactly the same as doing
$arr[13] = 56

//As the maximum integer key is 12, so the new key is 13
Yacoby
+1 for explaining the usage of `!== false`
Felix Kling
Does the $newArray[] part append to the beginning or end of the array? Was having some problems with it and fixed it by$newArray[count($newArray)] = $value; this appends to end of array because count is 1 based and array is 0 based
stormist
+1  A: 
$array1 = array("jonathan", "joe", "david", "sampson");
$array2 = array();

foreach ($array1 as $name) {
  if (condition)
    $array2[] = $name;
}
Jonathan Sampson
+1  A: 

You need to declare your second array outside the foreach. Otherwise it would be re-initiated with every iteration.

$matches = array();
foreach ($myarray as $value) {
    if (strpos($value,'mysearchstring') !== false) {
        $matches[] = $value;
    }
}

Also make sure that you use a typesafe comparison with false since strpos may return 0 if the needle is at the begin of the string.

Gumbo
+1  A: 

You can declare the second array right before the foreach. Like this:

$secArray = array();
foreach ($myarray as $value)
{
    if (strpos($value,'mysearchstring')) {
        $secArray[] = $value;
    }
}

The []-Operator is an alias for array_push.

Best wishes,
Fabian

halfdan
+1  A: 

Declare the new array before the loop with the array() constructor. You need to use !== false because strpos will return 0 (not boolean false) if the search string occurs at the beginning of the string. You might also consider using stripos to do a case insensitive search.

$new_array = array();
foreach ($myarray as $value)
  {
        if (strpos($value,'mysearchstring') !== false )
            $new_array[] = $value;
  }
sakabako
+4  A: 

Instead of looping like in everyone else's solution, how about using array_filter to get all the matching elements? array_filter returns only elements that return true when passed into a callback function:

function matches_string($value)
{
  return (strpos($value, 'mysearchstring') !== false);
}

$filtered_array = array_filter($my_array, "matches_string");
Daniel Vandersluis
+1 for the clever answer that can help him to learn something new... and that can reuse again without looping all the times!
DaNieL
+1  A: 

You could even use array_filter and a callback function like so:

$original = array('one','two','three','four','five');

$only_containing_o = array_filter($original, create_function('$string','return strpos($string, "o") !== false;'));

print_r($only_containing_o);
FlorianH