Reputation: 851
I do not know if I am doing the right way, but I need the $explode2
comma separates the result.
Tried using implode, but I did.
Someone would have to give me a hint?
<?php
$variavel = "2:2014-07-13,4:2014-08-13,6:2014-08-13,7:2014-08-13";
$var = explode(",", $variavel);
$total_var = count($var);
$n = 0;
while($n < $total_var){
$var[$n];
while($n < $total_var){
$explode2 = explode(":", $var[$n]);
$explode2 = $explode2[0];
print $explode2; //Result: 2467 | How would: 2,4,6,7
$n++;
}
$n++;
}
?>
Upvotes: 0
Views: 76
Reputation: 541
Here is the solution:
$variavel = "2:2014-07-13,4:2014-08-13,6:2014-08-13,7:2014-08-13";
$var = explode(",", $variavel);
$total_var = count($var);
$n = 0;
while($n < $total_var){
$explode2 = explode(":", $var[$n]);
$tempArray[] = $explode2[0];
$n++;
}
$tempString=implode(',',$tempArray);
echo $tempString;
ADDITIONAL DETAILS ON YOUR CODE
1) You do not need the first while loop. I do not know what you are intending to do there but it is redundant.
2) The reason why you are getting 2467
instead of 2,4,6,7
is because you are printing out in the while loop with no line breaks or white space. Your value for $explode2
per loop iteration will be as follow:
2
4
6
7
and when you print them out all together it will be displayed as 2467
Upvotes: 0
Reputation: 107
If you need only 2,4,6,7
then why don't you get the index of ':'
then get the character or substring index - 1
?
Upvotes: 0
Reputation: 78994
With the current code (I'm not going to verify it) just save each number to an array element and then implode
at the end:
$variavel = "2:2014-07-13,4:2014-08-13,6:2014-08-13,7:2014-08-13";
$var = explode(",", $variavel);
$total_var = count($var);
$n = 0;
while($n < $total_var){
//$var[$n]; //what???
while($n < $total_var){
$explode2 = explode(":", $var[$n]);
$explode2 = $explode2[0];
$result[] = $explode2; //save to an array
$n++;
}
$n++;
}
echo implode(",", $result); //implode with ,
Edit: I was bored:
$variavel = "2:2014-07-13,4:2014-08-13,6:2014-08-13,7:2014-08-13";
preg_match_all('/([^:]+):([^,]+),/', $variavel, $matches);
$result = array_combine($matches[1], $matches[2]);
echo implode(',', array_keys($result));
echo implode(',', $result);
Upvotes: 1
Reputation: 20469
The outer loop is pointless, simply use one loop, save the values into an array, then implode:
$variavel = "2:2014-07-13,4:2014-08-13,6:2014-08-13,7:2014-08-13";
$var = explode(",", $variavel);
$values = array();
foreach ($var as $value) {
$exp = explode(':', $value);
$values[]=$exp[0];
}
echo implode(',', $values);
Upvotes: 0
Reputation: 91744
I'm not sure what you need exactly, but I would generate a result array where the numbers are the keys and the dates the values. Then you can use array_keys()
to get the keys if you need them:
$variavel = "2:2014-07-13,4:2014-08-13,6:2014-08-13,7:2014-08-13";
$var = explode(",", $variavel);
$results = array();
foreach ($var as $value)
{
$tmp = explode(':', $value);
$results[$tmp[0]] = $tmp[1];
}
// the string you want
$str = implode(',', array_keys($results));
This would of course only work if the numbers / keys are unique.
Upvotes: 0
Reputation: 26
You can try something like this (in the loop)
$explode2 .= $explode2[0],',';
But you have to check when loop is going to end to eliminate last comma. And at the end you can add:
echo $explode2;
Upvotes: 0