Question

I'm a (complete) php noob and need a little help with something.

I'm working on a project whereby a user can select up to 6 categories via a form (I'm using ExpressionEngine). The output is like this:

"Cat1","Cat2","Cat3"

...and so on. The user can select up to 6 categories on that form.

What I need: even if my result is 3 categories I need to have 6 'cells' - for want of a better word - even if they're empty:

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

I've tried this:

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

...but the output give me:

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

)

How do I get from that to this:

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

Does this make sense?

Thanks Martin

No correct solution

OTHER TIPS

If you have an array that contains 6 items, including 3 empty strings :

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

You can implode those into a string, this way :

echo '"' . implode('","', $arr) . '"';

And you'll get the following output :

"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.

And, as implode only put the separator between elements, we have to put an additionnal " at the beginning and at the end of the resulting string.


(Hope I understood what you meant...)



EDIT after the comment :

OK, if your $arr array doesn't contain six items at the beginning :

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

A possibility could be to create an array with empty elements ; like this, for example :

$count = count($arr);

// Create an array with empty elements
$padding = array_fill($count, 6-$count, '');
var_dump($padding);

And, then, add those to the $arr array :

// Add the empty elements to $arr
$arr = $arr + $padding;

(You could also use array_merge, I suppose)

And, now, back to our implode :

echo '"' . implode('","', $arr) . '"';

And, this time again, you'll get :

"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)

And, yes, you can use the + operator with arrays (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) . '"';

You can try:

$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","","",""

Works even when $org has 6 or less cats:

If $org = "Cat1","Cat2","Cat3","cat4","cat5"

$str will be "Cat1","Cat2","Cat3","cat4","cat5",""

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top