0

I want to pass the value in an array with (:). I have values to I need to pass the value in array index and I need to use : and then encode/decode it but I am not able to do.

what I am trying but it not working for me:

$countArray = array("21F:22M:23F","31M:32F:33M","41F:42M:43M");

I want to pass the value like above line

$val1 = 21F;
$val2 = 22M;
$val3 = 23F;

$val4 = 31M;
$val5 = 32F;
$val6 = 33M;

$val7 = 41F;
$val8 = 42M;
$val9 = 42M;

I want something like:

$countArray = 
array("$val1:$val2:$val3","$val4:$val5:$val6","$val7:$val8:$val9");

So that I can get the output like:

21F:22M:23F,31M:32F:33M,41F:42M:43M
1
  • 3
    Have you tried it? Commented Oct 19, 2018 at 14:23

2 Answers 2

2

You could use implode:

https://secure.php.net/manual/en/function.implode.php

$countArray = array(
    implode(':', array($val1, $val2, $val3))
    ... etc ...
)

print_r($countArray); # will output desired results
Sign up to request clarification or add additional context in comments.

Comments

0

You can also concatenate the values with "." as they are strings, for example:

$val1 = 21F;
$val2 = 22M;
$val3 = 23F;

$countArray=array($val1.":".$val2.":".$val3);

You can also do an iteration and parse the values when i%3==0, adding them to the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.