开发者

Replace array key integers with string

$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 * FRO开发者_如何学JAVAM ('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.


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.


That won't work. Your array keys have to be unique, or subsequent additions will simply overwrite the previous key.


As the others said, keys have to be unique. Otherwise, which element should be returned if you access $arr['tag']? If you now say "all of them", then create a nested array:

$array = array();
$array['tag'] = str_getcsv($string);

The value $array['tag'] will be another array (the one you already have) with numerical keys. This makes, because you have a list of tags and lists can be represented as arrays too.

Understanding arrays is very important if you want to work with PHP, so I suggest to read the array manual.


Assuming you know the size of your array beforehand

$tags = array("tag1","tag2","tag3");
$data = array("php","photoshop","css");

$myarray = array();

for ($i=0; $i<count($data); $i++) {
    $myarray[$i] = array($data[$i], $tags[$i]);
}

Then

echo $myarray[0][0] . ", " . $myarray[0][1];

Outputs:

php, tag1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜