diziler ile ilgili php soru

2 Cevap

Ben (tam) php çaylak değilim ve bir şey ile biraz yardıma ihtiyacım var.

Bir kullanıcı (ben ExpressionEngine kullanıyorum) bir form aracılığıyla 6 kategoride seçebilirsiniz sayede ben bir proje üzerinde çalışıyorum. Çıktısı şu şekildedir:

"Cat1","Cat2","Cat3"

... Vb. Kullanıcı bu form üzerinde 6 kategoride seçebilirsiniz.

Ben gerekenler: - Daha iyi bir kelime istiyorum - benim sonucu 3 kategoride olsa bile ben 6 'hücre' olması gereken onlar boş olsanız bile:

"Cat1","Cat2","Cat3","","",""

Bu denedim:

<?php
$str = '(output goes here)';
print_r(explode(',', $str, 6));
?>

... Ama çıkış bana verin:

Array
(
[0] => Cat1
[1] => Cat2
[2] => Cat3
[3] =>
[4] =>
[5] =>

)

Bunu nasıl bu kadar alabilirim:

"Cat1","Cat2","Cat3","","",""

Bu mantıklı mı?

Thanks Martin

2 Cevap

Eğer 3 boş dizeler dahil 6 öğeleri içeren bir dizi varsa:

$arr = array('cat1', 'cat2', 'cat3', '', '', '');

You can implode 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) . '"';

Sen deneyebilirsiniz:

$org = '"Cat1","Cat2","Cat3"';
$arr = explode(',',$org);
$arr = array_merge($arr,array_fill(count($arr),6-count($arr),'""'));
$str = implode(',',$arr);
echo($str); // prints "Cat1","Cat2","Cat3","","",""

Çalışır bile $org den 6 veya daha az kediler:

Eğer $org = "Cat1","Cat2","Cat3","cat4","cat5"

$str olacak "Cat1","Cat2","Cat3","cat4","cat5",""