Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Quote:
its working when i use first letter like "c", "a" etc, but when i put any in between letters like considering orange as example if i put "an" letter in the object then it should display which all the elements contain "an" so is their any way to do this. Please let me if any one know this


What I have tried:

$veggies = array("Potato", "Cucumber", "Carrot", "Orange", "Green Beans", "Onion");
        $fruits  = array("Apple", "Banana", "Orange", "Pineapple", "Grapes", "Watermelon");
        $salad   = array_merge ($veggies, $fruits);
        $Object = 'C';
        $search = array_filter($salad, function($el) use ($Object) 
        {
        return ( strpos($el[0], $Object) !== FALSE );
        });
        print_r($search);
Posted
Updated 9-Apr-18 20:37pm
Comments
Kats2512 10-Apr-18 2:37am    
sounds like homework

1 solution

PHP
$veggies = array("Potato", "Cucumber", "Carrot", "Orange", "Green Beans", "Onion");
        $fruits  = array("Apple", "Banana", "Orange", "Pineapple", "Grapes", "Watermelon");
        $salad   = array_merge ($veggies, $fruits);
        $Object = 'C';
        $search = array_filter($salad, function($el) use ($Object) 
        {
        return ( strpos($el, $Object) !== FALSE );
        });
        print_r($search);

//This will check for first letter
strpos($el[0], $Object)
//If you want to compare the entire text then use
strpos($el, $Object)
 
Share this answer
 
Comments
Maciej Los 10-Apr-18 2:43am    
5ed!
GKP1992 10-Apr-18 3:45am    
+5
123456patil 10-Apr-18 4:43am    
Thank you sir for suggestion, it worked
Sunasara Imdadhusen 10-Apr-18 5:42am    
You are welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900