Jump to content


[Solved] Calculating with Implode?


5 replies to this topic

#1 Mr. Jay

    Young Padawan

  • Members
  • Pip
  • 81 posts

Posted 02 September 2007 - 04:32 AM

I was wondering if theirs a way to calculate athe data in a array with implode or something?

I tried the following 3 but it didn't work

implode("+", $datanum) put the numbers together with +'s. ex: 9231+532+781+34
implode(+, $datanum) gave me an error.
implode($datanum) put the numbers together without spaces. ex: 923153278134

Thanks, Mr. Jay

Edited by Mr. Jay, 02 September 2007 - 08:40 PM.


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 02 September 2007 - 03:50 PM

Naturally implode can't preform calculations. It is meant to do one thing, but you'd be surprised at how powerful that one function can be. You already figured out half of it, the other half lies in eval() (in your case at least).

#3 Mr. Jay

    Young Padawan

  • Members
  • Pip
  • 81 posts

Posted 02 September 2007 - 06:24 PM

Thanks but it didn't work the way I did it :biggrin:

Error: It returns blank

			while($need >= $num)
			{
				$datanum[] = $parser[2][$num];
			}
			eval('return implode("+", $datanum);');

and

Error: It returns blank
			while($need >= $num)
			{
				$datanum[] = $parser[2][$num];
			}
			eval('return $datanum;');

Thanks, Mr. Jay

Edited by Mr. Jay, 02 September 2007 - 07:48 PM.


#4 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 02 September 2007 - 08:05 PM

I notice you're adding the values.


What about array_sum()?

#5 Mr. Jay

    Young Padawan

  • Members
  • Pip
  • 81 posts

Posted 02 September 2007 - 08:39 PM

View PostSpatialVisionary, on Sep 2 2007, 08:05 PM, said:

I notice you're adding the values.


What about array_sum()?
Tyvm SpatialVisionary :P

#6 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 02 September 2007 - 11:57 PM

Lol, been in to much of a hurry to thing of ways around the task at hand. But i do feel an explaination is needed.

The things you may have over looked in your code:
1. implode() returns a string. So you effectively made eval() return that same string.
2. Your second bit just returned an array...
3. eval() itself doesn't output anything (unless, like with regular php, it is instructed to).

eval('echo '.implode('+', $datanum).';');
That may produce a desireable result, but now that it's been mentioned, i would recommend just sticking to array_sum().





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users