Eğer 3 boş dizeler dahil 6 öğeleri içeren bir dizi varsa:
$arr = array('cat1', 'cat2', 'cat3', '', '', '');
You can implode a> dizeye olanlar, bu şekilde:
echo '"' . implode('","', $arr) . '"';
Ve şu çıktıyı alırsınız:
"cat1","cat2","cat3","","",""
Basically, implode allows you to put all items of the array into a string, using a separator -- here, the separator is "," which is what you can between your strings.
Implode sadece elemanları arasındaki ayırıcı koymak Ve, biz başında ve elde edilen dizenin sonunda bir additionnal " koymak zorunda.
(Hope I understood what you meant...)
EDIT after the comment :
Tamam, $arr dizisi başında altı öğeleri içermiyorsa:
$arr = array('cat1', 'cat2', 'cat3');
Bir olasılık boş unsurları ile bir dizi oluşturmak için olabilir; aşağıdaki gibi, örneğin:
$count = count($arr);
// Create an array with empty elements
$padding = array_fill($count, 6-$count, '');
var_dump($padding);
Ve sonra, $arr diziye de eklendiğinde:
// Add the empty elements to $arr
$arr = $arr + $padding;
(You could also use array_merge , I suppose)
Ve, şimdi, geri bizim için implode:
echo '"' . implode('","', $arr) . '"';
Ve, bu sefer yine alırsınız:
"cat1","cat2","cat3","","",""
Here's what the var_dump($padding); gives, for information :
array
3 => string '' (length=0)
4 => string '' (length=0)
5 => string '' (length=0)
Ve, evet, dizilerle + operatörünü kullanabilirsiniz (quoting):
The + operator appends elements of
remaining keys from the right handed
array to the left handed, whereas
duplicated keys are NOT overwritten.
If your $arr could be longer than 6 elements, you could use array_slice to remove the un-wanted elements :
$arr = array('cat1', 'cat2', 'cat3', '', '', '', '');
$arr = array_slice($arr, 0, 6);
echo '"' . implode('","', $arr) . '"';