Solved

Smoothing Data Array PHP

Posted on 2009-07-10
3
336 Views
Last Modified: 2013-12-13
I have an array:
Array
(
    [1] => 25
    [2] => 50
    [3] => 25
)
I would like to make it into:
Array
(
    [1] => 50
    [2] => 50
)
To do this I split the middle value between 1 and 3. This is the simplest example, where the split is 50,50. I would like to be able to take a 15 element array down to 6 elements.

Any ideas?


Additional Examples

[10, 15, 20, 25] Reduced to two elements: 25(10 + 15),45(20 + 25)
[10, 10, 10, 10, 11] Reduced to two elements: 25(10 + 10 + (10/2)),26((10/2) + 10 + 11)
0
Comment
Question by:nbcit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Expert Comment

by:xBellox
ID: 24825652
This should work just fine:

<script language="javascript">
function makeItTwo(arr) {
	var retArr = new Array(0, 0); // return array
	if ((arr.length % 2)){
		var middle = Math.floor(arr.length/2); // middle element
		retArr[0] += arr[middle]/2; // add value
		retArr[1] += arr[middle]/2; // add value
	};
	last = arr.length-1; // Last element
	for (i=0; i<last; i++) { // first and last, first+1 and last-1...first+n and last-n
		retArr[0] += arr[i];
		retArr[1] += arr[last];
		last--;
	};
	return retArr;	
};
 
// Testing
var arrTest1 = new Array(10,15,15,25,25,20);
var arrTest2 = new Array(10,15,14,10,20,10,22,25,20);
alert(makeItTwo(arrTest1));
alert(makeItTwo(arrTest2));
</script>

Open in new window

0
 

Author Comment

by:nbcit
ID: 24825801
Mind posting a php solution :D. Also I used 2 for ease of explanation, it could be any whole number.
0
 
LVL 9

Accepted Solution

by:
xBellox earned 500 total points
ID: 24826032
Sorry, I didn't see that is in php, I had done various JavaScript function here today :)

Please tell how this array should result with 3 and 5 elements:

array(10,15,14,10,20,10,22,25,20);



In advance for two elements in php will be something like this:

<?
function makeIt($arr) {
	$retArr = array_fill(0, 2, 0); // return array
	
	if ((count($arr) & 1)){
		$middle = floor(count($arr)/2); // middle element
		$retArr[0] += $arr[$middle]/2; // add value
		$retArr[1] += $arr[$middle]/2; // add value
	};
	$last = count($arr)-1; // Last element
	for ($i=0; $i<$last; $i++) { // first and last, first+1 and last-1...first+n and last-n
		$retArr[0] += $arr[$i];
		$retArr[1] += $arr[$last];
		$last--;
	};
	return $retArr;	
};
 
// Testing
$arrTest1 = array(10,15,15,25,25,20);
$arrTest2 = array(10,15,14,10,20,10,22,25,20);
print_r(makeIt($arrTest1));
print_r(makeIt($arrTest2));
 
?>

Open in new window

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question