Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6742467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:50:49+00:00 2026-05-26T11:50:49+00:00

<?php function filter($fst, $arr){ $new_arr=array(); for($i=0;$i<=(count($arr)-1);$i++){ if(substr($arr[$i], 0, 1) == $fst){ $new_arr[] = $fst;

  • 0
<?php
function filter($fst, $arr){
$new_arr=array();

    for($i=0;$i<=(count($arr)-1);$i++){
        if(substr($arr[$i], 0, 1) == $fst){
            $new_arr[] = $fst;
        }
    }
    return $new_arr;
}



$list = Array("Apparel","Associations","Building/Grounds","Building/Materials",
              "Dispensing","Disposables","Distributors");

$new_list[]=filter("A", $list);

for($i=0;$i<=(count($new_list)-1);$i++){ 
    echo '<li><a href="?id='.$i.'">'.$new_list[$i].'</a></li>'; 
}

?>

I have created a function named filter() to filter the contents of an array that starts with a character like “a”. It does not work at the moment.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:50:49+00:00Added an answer on May 26, 2026 at 11:50 am

    First of all I assume that you want to print the word that was OK and not the character you was checking with? $new_arr[] = $fst; should be $new_arr[] = $arr[$i];

    Secondly you’re adding the resulting array of the function into a new array instead of assigning the array to your variable. $new_list[] = should be $new_list =.

    Here’s an updated version of your code. I’ve marked where I’ve made changes..

    function filter($fst, $arr){
    $new_arr=array();
    
        for($i=0;$i<=(count($arr)-1);$i++){
            if(substr($arr[$i], 0, 1) == $fst){
                $new_arr[] = $arr[$i];         <----- Changed here
            }
        }
        return $new_arr;
    }
    
    
    
    $list = Array("Apparel","Associations","Building/Grounds","Building/Materials",
                  "Dispensing","Disposables","Distributors");
    
    $new_list=filter("A", $list);  <----- And changed here
    
    for($i=0;$i<=(count($new_list)-1);$i++){ 
        echo '<li><a href="?id='.$i.'">'.$new_list[$i].'</a></li>'; 
    }
    

    Output:

    <li><a href="?id=0">Apparel</a></li>
    <li><a href="?id=1">Associations</a></li>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently call a function to filter some HTML in PHP like this FilterHTML($string)
Below is an old PHP function I have for a dropdown list for users
In PHP 5.2 there was a nice security function added called input_filter, so instead
I'm using the PHP function imagettftext() to convert text into a GIF image. The
Is there a Php function to determine if a string consist of only ASCII
I have a php function I wrote that will take a text file and
in function need key to encrypt string without mcrypt libraly in php function encrypt($str,
Is there a php function that someone can use to automatically detect if an
I have written a PHP function to take a video embed code that has
I have a PHP function that takes a variable number of arguments (using func_num_args()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.