1

I have this array that I want to search and return the 'names' where the 'project_id' equal a predefined value, for example from the array below:

array(2) 
{ 
    [0]=> array(28) 
        { 
            ["id"]=> int(3794) 
            ["name"]=> string(30) "Value #1" 
            ["milestone_id"]=> int(280) 
            ["project_id"]=> int(41) 
        } 
    [1]=> array(28) 
        { 
            ["id"]=> int(3795) 
            ["name"]=> string(30) "Value #2" 
            ["milestone_id"]=> int(261) 
            ["project_id"]=> int(41) 
        }
    [2]=> array(28) 
        { 
            ["id"]=> int(37966) 
            ["name"]=> string(30) "Value #3" 
            ["milestone_id"]=> int(250) 
            ["project_id"]=> int(40) 
        }
    [3]=> array(28) 
        { 
            ["id"]=> int(3797) 
            ["name"]=> string(30) "Value #4" 
            ["milestone_id"]=> int(250) 
            ["project_id"]=> int(42) 
        }
    [4]=> array(28) 
        { 
            ["id"]=> int(3798) 
            ["name"]=> string(30) "Value #5" 
            ["milestone_id"]=> int(280) 
            ["project_id"]=> int(41) 
        }           
}

I would like to return all of the names for any of the records that have the project_id of 41 - in this case:

Value #1
Value #2
Value #5

To be honest, I'm new to PHP, and I've been stumbling around the interwebs trying to make some sense of various code snippets & instructions - none of which seem to do what I am after without significant fettling which I unfortunately lack the knowhow to do so any help would really be appreciated.

Many thanks

3
  • 1
    Just iterate all the subarrays Commented Oct 16, 2017 at 13:49
  • In the simplest form, you would simply iterate over the array in a simple loop, and inside check whether the project_id of the current item equals 41 - and if so, output whatever else you like from the current item. This is quite a trivial thing, if you read up on the basics of data structures and loops ... so at least show us some of your attempts. Commented Oct 16, 2017 at 13:51
  • use php.net/manual/en/function.array-filter.php function Commented Oct 16, 2017 at 13:51

2 Answers 2

2

You can solve this with array_filter()

$filteredArray = array_filter($array, function($row) {
    return $row['project_id'] == 41;
});

This will leave you with a $filteredArray holding all the items with project_id == 41.

Now you can iterate over these values, and print out the names, or what you want to do:

foreach ($filteredArray as $row) {
    echo $row['name'], PHP_EOL;
}

Or, if you just want an an array, you can do this with array_column

array_column($filteredArray, 'name');

Or simply (in one step)

$names = array_column(
    array_filter(
        $array,
        function($row) {
            return $row['project_id'] == 41;
        }
    ),
    'name'
);

Which leaves you with the final array

$names Array (
    (string) "Value #1"
    (string) "Value #2"
    (string) "Value #3"
)
Sign up to request clarification or add additional context in comments.

Comments

0

you can iterate through the sub arrays and look for the specific condition like this

     $customArray = array();
     foreach($outerArry as $subArray){

       if($subArray["project_id"]==41){

         $customArray[] = $subArray;
         }  
      }
      print_r($customArray);

Comments

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.