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

  • Home
  • SEARCH
  • 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 6094595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:41:52+00:00 2026-05-23T12:41:52+00:00

$string = php, photoshop, css; I’m producing an array from the comma separated values

  • 0

$string = "php, photoshop, css";

I’m producing an array from the comma separated values above using the str_getcsv() function:

$array = str_getcsv($string);

Result:

Array ( [0] => php [1] => photoshop [2] => css ) 

How can I replace the key integers with a string tag for all elements like seen below?

Array ( [tag] => php [tag] => photoshop [tag] => css ) 

Edit: if not possible what alternative can I apply? I need the array keys to be identical for a dynamic query with multiple OR clauses

e.g.

SELECT * FROM ('posts') WHERE 'tag' LIKE '%php% OR 'tag' LIKE '%photoshop% OR 'tag' LIKE '%css%'

I’m producing the query via a function that uses the array key as a column name and value as criteria.

  • 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-23T12:41:53+00:00Added an answer on May 23, 2026 at 12:41 pm

    That is not possible. You can have only one item per key. But in your example, the string “tag” would be the key of every item.

    The other way arround would work. So having an array like this:

    array('php' => 'tag', 'photoshop' => 'tag', 'css' => 'tag');
    

    This might help you, if you want to save the “type” of each entry in an array. But as all the entries of your array seems to be from the same type, just forget about the “tag” and only store the values in a numeric array.

    Or you can use a multidimensional array within the numeric array to save the type:

    array(
        0 => array( 'type' => 'tag', 'value' => 'php' ), 
        1 => array( 'type' => 'tag', 'value' => 'photoshop' ),
        2 => array( 'type' => 'tag', 'value' => 'css' )
    );
    

    But still using just an numeric array should be fine if all the entries have the same type. I can even think of a last one:

    array(
       'tag' => array('php', 'photoshop', 'css')
    );
    

    But even if I repeat myself: Just use an ordinary array and name it something like $tag!

    BTW: explode(', ', %string) is the more common function to split a string.

    To build SQL statement you might do something like this:

    // ... inside you build function
    if(is_array($value)){
      $sql .= "'".$key."' LIKE '%."implode("%' OR '".$key."' LIKE '%", $value)."%'";
    } else {
      $sql .= "'".$key."' LIKE '%".$value."%'";
    }
    

    This might look confusing but it’s much cleaner than runnig into two foreach-loops building the query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any function that makes string from PHP SimpleXMLElement ?
I am returning a JSON string from PHP: <?php $results = array( result =>
I am writing a query in php using a string sent from a android
In other languages you can use strings as keys - PHP: $array['string'] = 50;
PHP has the habit of evaluating (int)0 and (string)0 as empty when using the
PHP has a great function called htmlspecialcharacters() where you pass it a string and
I saw this comment.... http://www.php.net/manual/en/function.mysql-real-escape-string.php#93005 And began to wonder why this would be a
I have a string in PHP (came from some data source), which represents a
Is there a PHP string function that transforms a multi-line string into a single-line
I want split a string using PHP to get a individual date info. For

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.