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 437359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:34:13+00:00 2026-05-12T20:34:13+00:00

I have an array of output from a database. I am wondering what the

  • 0

I have an array of output from a database. I am wondering what the cleanest way to filter the values is

example array

Array
(
    [0] => Array
        (
            [title] => title 1
            [cat_title] => Drawings
            [sec_title] => Portfolio
        )

    [1] => Array
        (
            [title] => title 2
            [cat_title] => Paintings
            [sec_title] => Portfolio
        )

    [2] => Array
        (
            [title] => title 3
            [cat_title] => Drawings
            [sec_title] => Portfolio
        )
)

As an example what would be the cleanest way to make all of the cat_title to uppercase and all of the sec_title’s to htmlspecialchars?

I was thinking if i sorted the array improperly that I could use the array map function. like this

improper array

Array
(
    [title] => Array
        (
            [0] => title 1
            [1] => title 2
            [2] => title 3
        )

    [cat_title] => Array
        (
            [0] => Drawings
            [1] => Paintings
            [2] => Drawings
        )

)

Then I could do something handy like:

array_map('strtoupper', $array['cat_title']);

and make all of the cat_titles uppercase in one go. Something like that would sure beat this, which is what I have going on now.

$count = count($array);
for($i=0; $i < $count; $i++) { 
    //filter list output
    if (isset($array[$i]['cat_title'])) {
        $array[$i]['cat_title'] = strtoupper($array[$i]['cat_title']);
    }
}

Do you guys know of anyway I could callback functions on numbered array’s a little bit more elegantly then above? Hopefully without sorting the array incorrectly? something like array_map or array_walk?

  • 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-12T20:34:14+00:00Added an answer on May 12, 2026 at 8:34 pm

    You can do this with array_walk, it would be something like:

    function cleanData( &$item, $key )
    {
      $item['cat_title'] = strtoupper( $item['cat_title'] );
      $item['sec_title'] = htmlspecialchars( $item['sec_title'] );
    }
    
    array_walk( $arr, 'cleanData' );
    

    However, functions like htmlspecialchars are really only intended for the very last moment when you’re actually sending content to the browser. Before then, you want to work with the raw text and just have <?=htmlspecialchars($var)?> in your template/view file. Otherwise you can end up with encoding problems if you, for example, send that encoded data to a database, then retrieve it and run htmlspecialchars again.


    UPDATE: Here’s the code I used to test this; it works fine for me. I’m on PHP 5.2 but there isn’t any difference in the function from version 4+.

    $arr = array(
        array( 'title' => 'title 1', 'cat_title' => 'Drawings', 'sec_title' => 'Portfolio' ),
        array( 'title' => 'title 2', 'cat_title' => 'Paintings', 'sec_title' => 'Portfolio' ),
        array( 'title' => 'title 3', 'cat_title' => 'Drawings2', 'sec_title' => 'Portfolio' )
    );
    
    
    array_walk( $arr, 'cleanData' );
    echo '<pre>', print_r($arr, true), '</pre>';
    

    Outputs:

    Array
    (
        [0] => Array
            (
                [title] => title 1
                [cat_title] => DRAWINGS
                [sec_title] => Portfolio
            )
    
        [1] => Array
            (
                [title] => title 2
                [cat_title] => PAINTINGS
                [sec_title] => Portfolio
            )
    
        [2] => Array
            (
                [title] => title 3
                [cat_title] => DRAWINGS2
                [sec_title] => Portfolio
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of tags that I'm pulling from a database, I am
I have a dynamically generated array from a database. Basically it's an array of
I want insert in the row from database, array of values as that each
I have a two dimensional array with the following output Array ( [0] =>
I have have an array as follows: $row when I output the entire array
I have a array with users sorted by score. I only want to output
i have array unknown size i want to transfer to matrix[n][2].Example; D[c]=1,2,3,4,5 D[c/2][2]= 1
I have a large string variable that is assigned from a database column of
I have a problem with retrieving data from a database using PHP statements. At
I have this script to output everything from table called Articles. If I load

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.