By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,183 Members | 1,112 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,183 IT Pros & Developers. It's quick & easy.

Removing all elements from an array that have a specific word in them

100+
P: 158
What I need to do is take a multi dimensional array and delete all elements with the word "Total" in [all elements][3]. I think i would use array_filter but dont know exactly how. Could some one please help me.


The Code I have now is:
Expand|Select|Wrap|Line Numbers
  1. function total_out ( $v ) 
  2. {
  3.   for ( $i=0; $i<$end; $i++ ) 
  4.     {
  5.       if ( $data[$i][2] == "Total" )
  6.            {
  7.              return false;
  8.            }
  9.       }
  10.     return true;
  11. }
  12.  
  13. $data = ( array_filter ( $data , "total_out" ) );
Jun 27 '07 #1
Share this Question
Share on Google+
2 Replies


100+
P: 158
can any one help?? Its real important
Jun 27 '07 #2

pbmods
Expert 5K+
P: 5,821
Heya Kardon.

Create a function that checks for the word 'Total' and returns false if it finds it:

Expand|Select|Wrap|Line Numbers
  1. function filterTotal($var) {
  2.     return (strpos($var, 'Total') === false)
  3. }
  4.  
Then you call array_filter on your array:

Expand|Select|Wrap|Line Numbers
  1. $newArray = array_filter($array, 'filterTotal');
  2.  
Jun 27 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.