2

I'm using the below code to separate odd and even and store it in different variable. When there are only 2 value available then it works fine but when the number value increases then it doesn't. I want to make it dynamic so that n number of values can be separated and stored correctly.

Example: If the value of

$final_array = "PNL testing 1,10,PNL testing 2,35,";

It prints nicely:

$teams = "PNL testing 1, PNL testing 2";

$amount = "10, 35";

But when it increases from

$final_array = "PNL testing 1,10,PNL testing 2,35,";

to

$final_array = "PNL testing 1,10,PNL testing 2,35,Team 3,95,";

Then also it prints

$teams = "PNL testing 1, PNL testing 2";

$amount = "10, 35"; 

Please guide me through on where I am going wrong.

$res = array();
$result = preg_match_all("{([\w\s\d]+),(\d+)}", $final_array, $res);
$teams = join(', ', $res[1]); //will display teams
$amount = join(', ', $res[2]); //will display amount every team have
echo $teams . "<br />" . $amount;
4
  • 5
    Don't really see how this relates to even and odd numbers? Commented Apr 26, 2013 at 8:31
  • 2
    Might find what your looking for here.. stackoverflow.com/questions/738168/filter-array-odd-even?rq=1 Commented Apr 26, 2013 at 8:33
  • 1
    The code you mentioned printed: PNL testing 1, PNL testing 2, Team 3 as $teams, and 10, 35, 95 as $amount, not what you said it printed. Commented Apr 26, 2013 at 8:37
  • So basically, you want to drop 1 team if the number of teams is odd? Commented Apr 26, 2013 at 8:42

3 Answers 3

3

I think you can totally drop the REGEX in favor of good old explode/implode with some logic in it:

$teams = array();
$amount = array();

$a = explode(',', trim(trim($final_array), ','));
foreach ($a as $i => $v)
    if (($i % 2) == 0) $teams[] = trim($a);
    else $amount[] = trim($a);

$teams = implode(', ', $teams);
$amount = impode(', ', $amount);

In the above code $tms and $amn are temporary arrays. In the foreach we take the exploded values from the string and we store them in those two arrays sorting them by key (if it's even then it's a team otherwise it's an amount).

At the end we just implode the new values into your output variables $teams and $amount.

Sign up to request clarification or add additional context in comments.

Comments

0

It will much easier I think to use explode:

$result = explode(',', $final_array);

$teams = array();
$amount = array();

foreach ($result as $key => $value) {
    if ($key % 2 == 0) {
        $teams[] = $value;
    } else {
        $amount[] = $value;
    }
}

$teams  = implode(', ', $teams); //will display teams
$amount = implode(', ', $amount); //will display amount every team have
echo $teams."<br />".$amount;

3 Comments

I think $key % 2 == 1 should be $key % 2 == 0.
I would do that, too. Maybe start with rtrim($final_array, ',')..?
@Jueecy.new you're absolutely right. I had a moment of madness when I thought that 0 % 2 = 1. It scares me a bit now ;) @nbrogi - that is more of fine-tuning the code but of course is a valuable advice :)
0

I would change this part of Michal Trojanowski for more efficiency

foreach ($result as $key => $value) {
    if ($key % 2 == 0) {
        $teams[] = $value;
    } else {
        $amount[] = $value;
    }
}

you see it has an extra condition we can remove it by like this

$length = count($result);//cache count result
for ($i = 0; $i < $length; $i += 2) {
    $teams[] = $result[$i];
}

for ($i = 1; $i < $length; $i += 2) {
    $amount[] = $result[$i];
}

Here the loop is running same but it just removes the the condition.

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.