Php - Sum up the numbers in an array one by oneHow can I prevent SQL injection in PHP?Deleting an element from an array in PHPConvert HTML + CSS to PDF with PHP?startsWith() and endsWith() functions in PHPHow do I get PHP errors to display?How do you parse and process HTML/XML in PHP?Reference — What does this symbol mean in PHP?PHP array delete by value (not key)How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?
Confused with atmospheric pressure equals plastic balloon’s inner pressure
What would be the way to say "just saying" in German? (Not the literal translation)
Assigning function to function pointer, const argument correctness?
empApi with Lightning Web Components?
How durable are silver inlays on a blade?
Is it okay to have a sequel start immediately after the end of the first book?
Housemarks (superimposed & combined letters, heraldry)
The origin of the Russian proverb about two hares
How can one's career as a reviewer be ended?
Why isn't Bash trap working if output is redirected to stdout?
Could a person damage a jet airliner - from the outside - with their bare hands?
How can powerful telekinesis avoid violating Newton's 3rd Law?
Does a (nice) centerless group always have a centerless profinite completion?
What plausible reason could I give for my FTL drive only working in space
Why do some devices use electrolytic capacitors instead of ceramics for small value components?
How do we say "within a kilometer radius spherically"?
How to avoid typing 'git' at the begining of every Git command
Can the removal of a duty-free sales trolley result in a measurable reduction in emissions?
Suppose leased car is totalled: what are financial implications?
Trying to get (more) accurate readings from thermistor (electronics, math, and code inside)
How to write a convincing religious myth?
Difference between prepositions in "...killed during/in the war"
Is Lambda Calculus purely syntactic?
Who is "He that flies" in Lord of the Rings?
Php - Sum up the numbers in an array one by one
How can I prevent SQL injection in PHP?Deleting an element from an array in PHPConvert HTML + CSS to PDF with PHP?startsWith() and endsWith() functions in PHPHow do I get PHP errors to display?How do you parse and process HTML/XML in PHP?Reference — What does this symbol mean in PHP?PHP array delete by value (not key)How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have an array;
$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
I want to show them in graph. I want my Y-Axis as my elements of that array and X-Axis as sum up the numbers until that element. (x1,x1+x2,x1+x2+x3,...)
My graph will be;
Y-Axis : 1100,3150,4430,4430,5170,7450,7450,7450,8230
X-Axis : 1100,4250,8680,13110,18280,25730,33180,40630,48860
But I have no idea about how to do that. Is there anyone who can help me with it ? Thanks.
My entire code:
<?php
echo " ".'<br>'.'<br>';
$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
$arrtotal=0;
for($i=0; $i<=8; $i++)
if ($arr[$i]<100)
$arr[$i]=$arr[$i];
else
$arr[$i]=$arr[$i]/1000;
$arr[$i]=(string)$arr[$i];
function calculate($arr, $output)
switch($output)
case 'mean':
$count = count($arr)+1;
$sum = array_sum($arr);
$total = $sum / $count;
break;
case 'median':
rsort($arr);
$middle = (count($arr) / 2)+1;
$total = $arr[$middle-1];
break;
case 'mode':
$v = array_count_values($arr);
arsort($v);
foreach($v as $k => $v)$total = $k; break;
break;
return $total;
function sd_square($x, $total) return pow($x - $total,2);
function sd($arr)
return sqrt(array_sum(array_map("sd_square", $arr, array_fill(0,count($arr), (array_sum($arr) / count($arr)) ) ) ) / (count($arr)-1) );
echo ' '.'<br>';
echo "Values: ";
echo json_encode($arr).'<br>';
echo 'Mean: '.calculate($arr, 'mean').'<br>';
echo 'Median: '.calculate($arr, 'median').'<br>';
echo 'Mode: '.calculate($arr, 'mode').'<br>';
echo "Standart Derivation: ".sd($arr);
?>
<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload = function ()
var data = <?php echo json_encode($arr, JSON_NUMERIC_CHECK); ?>;
data = data.map(function (row, index)
return
x: index,
y: row
;
);
var chart = new CanvasJS.Chart("chartContainer",
title:
text: "Analysis"
,
axisY:
title: "Variables"
,
data: [
type: "line",
dataPoints: data
]
);
chart.render();
</script>
</head>
<body>
<div id="chartContainer" style="height: 250px; width: 50%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</body>
</html>
In this code my X-Axis is (0,1,2,3,4,5,6,7,8) and I don't want that.
I'm sorry if I couldn't explain well, English is not my native language.
php
add a comment |
I have an array;
$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
I want to show them in graph. I want my Y-Axis as my elements of that array and X-Axis as sum up the numbers until that element. (x1,x1+x2,x1+x2+x3,...)
My graph will be;
Y-Axis : 1100,3150,4430,4430,5170,7450,7450,7450,8230
X-Axis : 1100,4250,8680,13110,18280,25730,33180,40630,48860
But I have no idea about how to do that. Is there anyone who can help me with it ? Thanks.
My entire code:
<?php
echo " ".'<br>'.'<br>';
$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
$arrtotal=0;
for($i=0; $i<=8; $i++)
if ($arr[$i]<100)
$arr[$i]=$arr[$i];
else
$arr[$i]=$arr[$i]/1000;
$arr[$i]=(string)$arr[$i];
function calculate($arr, $output)
switch($output)
case 'mean':
$count = count($arr)+1;
$sum = array_sum($arr);
$total = $sum / $count;
break;
case 'median':
rsort($arr);
$middle = (count($arr) / 2)+1;
$total = $arr[$middle-1];
break;
case 'mode':
$v = array_count_values($arr);
arsort($v);
foreach($v as $k => $v)$total = $k; break;
break;
return $total;
function sd_square($x, $total) return pow($x - $total,2);
function sd($arr)
return sqrt(array_sum(array_map("sd_square", $arr, array_fill(0,count($arr), (array_sum($arr) / count($arr)) ) ) ) / (count($arr)-1) );
echo ' '.'<br>';
echo "Values: ";
echo json_encode($arr).'<br>';
echo 'Mean: '.calculate($arr, 'mean').'<br>';
echo 'Median: '.calculate($arr, 'median').'<br>';
echo 'Mode: '.calculate($arr, 'mode').'<br>';
echo "Standart Derivation: ".sd($arr);
?>
<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload = function ()
var data = <?php echo json_encode($arr, JSON_NUMERIC_CHECK); ?>;
data = data.map(function (row, index)
return
x: index,
y: row
;
);
var chart = new CanvasJS.Chart("chartContainer",
title:
text: "Analysis"
,
axisY:
title: "Variables"
,
data: [
type: "line",
dataPoints: data
]
);
chart.render();
</script>
</head>
<body>
<div id="chartContainer" style="height: 250px; width: 50%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</body>
</html>
In this code my X-Axis is (0,1,2,3,4,5,6,7,8) and I don't want that.
I'm sorry if I couldn't explain well, English is not my native language.
php
add a comment |
I have an array;
$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
I want to show them in graph. I want my Y-Axis as my elements of that array and X-Axis as sum up the numbers until that element. (x1,x1+x2,x1+x2+x3,...)
My graph will be;
Y-Axis : 1100,3150,4430,4430,5170,7450,7450,7450,8230
X-Axis : 1100,4250,8680,13110,18280,25730,33180,40630,48860
But I have no idea about how to do that. Is there anyone who can help me with it ? Thanks.
My entire code:
<?php
echo " ".'<br>'.'<br>';
$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
$arrtotal=0;
for($i=0; $i<=8; $i++)
if ($arr[$i]<100)
$arr[$i]=$arr[$i];
else
$arr[$i]=$arr[$i]/1000;
$arr[$i]=(string)$arr[$i];
function calculate($arr, $output)
switch($output)
case 'mean':
$count = count($arr)+1;
$sum = array_sum($arr);
$total = $sum / $count;
break;
case 'median':
rsort($arr);
$middle = (count($arr) / 2)+1;
$total = $arr[$middle-1];
break;
case 'mode':
$v = array_count_values($arr);
arsort($v);
foreach($v as $k => $v)$total = $k; break;
break;
return $total;
function sd_square($x, $total) return pow($x - $total,2);
function sd($arr)
return sqrt(array_sum(array_map("sd_square", $arr, array_fill(0,count($arr), (array_sum($arr) / count($arr)) ) ) ) / (count($arr)-1) );
echo ' '.'<br>';
echo "Values: ";
echo json_encode($arr).'<br>';
echo 'Mean: '.calculate($arr, 'mean').'<br>';
echo 'Median: '.calculate($arr, 'median').'<br>';
echo 'Mode: '.calculate($arr, 'mode').'<br>';
echo "Standart Derivation: ".sd($arr);
?>
<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload = function ()
var data = <?php echo json_encode($arr, JSON_NUMERIC_CHECK); ?>;
data = data.map(function (row, index)
return
x: index,
y: row
;
);
var chart = new CanvasJS.Chart("chartContainer",
title:
text: "Analysis"
,
axisY:
title: "Variables"
,
data: [
type: "line",
dataPoints: data
]
);
chart.render();
</script>
</head>
<body>
<div id="chartContainer" style="height: 250px; width: 50%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</body>
</html>
In this code my X-Axis is (0,1,2,3,4,5,6,7,8) and I don't want that.
I'm sorry if I couldn't explain well, English is not my native language.
php
I have an array;
$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
I want to show them in graph. I want my Y-Axis as my elements of that array and X-Axis as sum up the numbers until that element. (x1,x1+x2,x1+x2+x3,...)
My graph will be;
Y-Axis : 1100,3150,4430,4430,5170,7450,7450,7450,8230
X-Axis : 1100,4250,8680,13110,18280,25730,33180,40630,48860
But I have no idea about how to do that. Is there anyone who can help me with it ? Thanks.
My entire code:
<?php
echo " ".'<br>'.'<br>';
$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
$arrtotal=0;
for($i=0; $i<=8; $i++)
if ($arr[$i]<100)
$arr[$i]=$arr[$i];
else
$arr[$i]=$arr[$i]/1000;
$arr[$i]=(string)$arr[$i];
function calculate($arr, $output)
switch($output)
case 'mean':
$count = count($arr)+1;
$sum = array_sum($arr);
$total = $sum / $count;
break;
case 'median':
rsort($arr);
$middle = (count($arr) / 2)+1;
$total = $arr[$middle-1];
break;
case 'mode':
$v = array_count_values($arr);
arsort($v);
foreach($v as $k => $v)$total = $k; break;
break;
return $total;
function sd_square($x, $total) return pow($x - $total,2);
function sd($arr)
return sqrt(array_sum(array_map("sd_square", $arr, array_fill(0,count($arr), (array_sum($arr) / count($arr)) ) ) ) / (count($arr)-1) );
echo ' '.'<br>';
echo "Values: ";
echo json_encode($arr).'<br>';
echo 'Mean: '.calculate($arr, 'mean').'<br>';
echo 'Median: '.calculate($arr, 'median').'<br>';
echo 'Mode: '.calculate($arr, 'mode').'<br>';
echo "Standart Derivation: ".sd($arr);
?>
<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload = function ()
var data = <?php echo json_encode($arr, JSON_NUMERIC_CHECK); ?>;
data = data.map(function (row, index)
return
x: index,
y: row
;
);
var chart = new CanvasJS.Chart("chartContainer",
title:
text: "Analysis"
,
axisY:
title: "Variables"
,
data: [
type: "line",
dataPoints: data
]
);
chart.render();
</script>
</head>
<body>
<div id="chartContainer" style="height: 250px; width: 50%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</body>
</html>
In this code my X-Axis is (0,1,2,3,4,5,6,7,8) and I don't want that.
I'm sorry if I couldn't explain well, English is not my native language.
php
php
asked Mar 24 at 21:56
Tanay AcanTanay Acan
246
246
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The simplest way is just to loop over the values, adding the current array value to the previous output value to create the new output value. You can then convert that into an array of [x => y] values using array_combine
:
$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
$out = array($arr[0]);
for ($i = 1; $i < count($arr); $i++)
$out[$i] = $out[$i-1] + $arr[$i];
$arr = array_combine($out, $arr);
print_r($arr);
Output:
Array (
[1100] => 1100
[4250] => 3150
[8680] => 4430
[13110] => 4430
[18280] => 5170
[25730] => 7450
[33180] => 7450
[40630] => 7450
[48860] => 8230
)
Demo on 3v4l.org
ohh I was doing $arr[$i]+$arr[$i-1]... that was my mistake. Thanks Nick but I have one more question. Now I have 2 arrays, can I show them in same graph ? ($arr is y-axis and $out is x-axis)
– Tanay Acan
Mar 24 at 22:06
1
@TanayAcan judging by your code, you probably just need$arr = array_combine($out, $arr);
. That will give you an array that looks like[1100 => 1100, 4250 => 3150, 8680 => 4430 ...]
– Nick
Mar 24 at 22:09
I did array_combine but my array became "1100":1100,"3150":4250,"4430":13110,"5170":18280,"7450":40630,"8230":48860 it skipped repeated numbers but I need them, what can I do ?
– Tanay Acan
Mar 24 at 22:18
1
@TanayAcan looks like you didarray_combine($arr, $out)
instead ofarray_combine($out, $arr)
– Nick
Mar 24 at 22:18
1
@TanayAcan see my edit and updated demo
– Nick
Mar 24 at 22:20
add a comment |
to get that one array into 2 arrays of graph co-ordinates, you can do this:
$x = array();
$y = array();
$running_total = 0;
for($i = 0; $i < count($arr); $i++)
$y[$i] = $arr[$i];
$running_total += $arr[$i];
$x[$i] = $running_total;
This will give you two arrays; array $x that contains a list of your X-coordinates, and $y, that gives you a list of your Y-coordinates; and you will still have access to your original $arr array, should you need to do further calculations on it. Based on your question, I think that will get you what you need.
However, if you are saying you want 1 array where the X co-ordinates are the array indexes, and the value is the array value itself, for example $y[3150] = 4250, then that is impossible; because you have duplicates in your original list, you cannot use those values as array indexes without ending up overwriting them.
(At least, not without making each array value an array itself, but that is taking things an order of magnitude above where is probably necessary)
Thank you TheMouseMaster, your answer is really useful for me. I just have one more question. How can I show those $x and $y in the same graph ? I couldn't do it
– Tanay Acan
Mar 24 at 22:19
sadly, I cannot answer that. I am not familiar with theCanvasJS
package you are using :(
– TheMouseMaster
Mar 24 at 22:20
I understand, no problem you helped a lot. thank you :)
– Tanay Acan
Mar 24 at 22:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328954%2fphp-sum-up-the-numbers-in-an-array-one-by-one%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The simplest way is just to loop over the values, adding the current array value to the previous output value to create the new output value. You can then convert that into an array of [x => y] values using array_combine
:
$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
$out = array($arr[0]);
for ($i = 1; $i < count($arr); $i++)
$out[$i] = $out[$i-1] + $arr[$i];
$arr = array_combine($out, $arr);
print_r($arr);
Output:
Array (
[1100] => 1100
[4250] => 3150
[8680] => 4430
[13110] => 4430
[18280] => 5170
[25730] => 7450
[33180] => 7450
[40630] => 7450
[48860] => 8230
)
Demo on 3v4l.org
ohh I was doing $arr[$i]+$arr[$i-1]... that was my mistake. Thanks Nick but I have one more question. Now I have 2 arrays, can I show them in same graph ? ($arr is y-axis and $out is x-axis)
– Tanay Acan
Mar 24 at 22:06
1
@TanayAcan judging by your code, you probably just need$arr = array_combine($out, $arr);
. That will give you an array that looks like[1100 => 1100, 4250 => 3150, 8680 => 4430 ...]
– Nick
Mar 24 at 22:09
I did array_combine but my array became "1100":1100,"3150":4250,"4430":13110,"5170":18280,"7450":40630,"8230":48860 it skipped repeated numbers but I need them, what can I do ?
– Tanay Acan
Mar 24 at 22:18
1
@TanayAcan looks like you didarray_combine($arr, $out)
instead ofarray_combine($out, $arr)
– Nick
Mar 24 at 22:18
1
@TanayAcan see my edit and updated demo
– Nick
Mar 24 at 22:20
add a comment |
The simplest way is just to loop over the values, adding the current array value to the previous output value to create the new output value. You can then convert that into an array of [x => y] values using array_combine
:
$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
$out = array($arr[0]);
for ($i = 1; $i < count($arr); $i++)
$out[$i] = $out[$i-1] + $arr[$i];
$arr = array_combine($out, $arr);
print_r($arr);
Output:
Array (
[1100] => 1100
[4250] => 3150
[8680] => 4430
[13110] => 4430
[18280] => 5170
[25730] => 7450
[33180] => 7450
[40630] => 7450
[48860] => 8230
)
Demo on 3v4l.org
ohh I was doing $arr[$i]+$arr[$i-1]... that was my mistake. Thanks Nick but I have one more question. Now I have 2 arrays, can I show them in same graph ? ($arr is y-axis and $out is x-axis)
– Tanay Acan
Mar 24 at 22:06
1
@TanayAcan judging by your code, you probably just need$arr = array_combine($out, $arr);
. That will give you an array that looks like[1100 => 1100, 4250 => 3150, 8680 => 4430 ...]
– Nick
Mar 24 at 22:09
I did array_combine but my array became "1100":1100,"3150":4250,"4430":13110,"5170":18280,"7450":40630,"8230":48860 it skipped repeated numbers but I need them, what can I do ?
– Tanay Acan
Mar 24 at 22:18
1
@TanayAcan looks like you didarray_combine($arr, $out)
instead ofarray_combine($out, $arr)
– Nick
Mar 24 at 22:18
1
@TanayAcan see my edit and updated demo
– Nick
Mar 24 at 22:20
add a comment |
The simplest way is just to loop over the values, adding the current array value to the previous output value to create the new output value. You can then convert that into an array of [x => y] values using array_combine
:
$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
$out = array($arr[0]);
for ($i = 1; $i < count($arr); $i++)
$out[$i] = $out[$i-1] + $arr[$i];
$arr = array_combine($out, $arr);
print_r($arr);
Output:
Array (
[1100] => 1100
[4250] => 3150
[8680] => 4430
[13110] => 4430
[18280] => 5170
[25730] => 7450
[33180] => 7450
[40630] => 7450
[48860] => 8230
)
Demo on 3v4l.org
The simplest way is just to loop over the values, adding the current array value to the previous output value to create the new output value. You can then convert that into an array of [x => y] values using array_combine
:
$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
$out = array($arr[0]);
for ($i = 1; $i < count($arr); $i++)
$out[$i] = $out[$i-1] + $arr[$i];
$arr = array_combine($out, $arr);
print_r($arr);
Output:
Array (
[1100] => 1100
[4250] => 3150
[8680] => 4430
[13110] => 4430
[18280] => 5170
[25730] => 7450
[33180] => 7450
[40630] => 7450
[48860] => 8230
)
Demo on 3v4l.org
edited Mar 24 at 22:20
answered Mar 24 at 22:01
NickNick
48.2k142444
48.2k142444
ohh I was doing $arr[$i]+$arr[$i-1]... that was my mistake. Thanks Nick but I have one more question. Now I have 2 arrays, can I show them in same graph ? ($arr is y-axis and $out is x-axis)
– Tanay Acan
Mar 24 at 22:06
1
@TanayAcan judging by your code, you probably just need$arr = array_combine($out, $arr);
. That will give you an array that looks like[1100 => 1100, 4250 => 3150, 8680 => 4430 ...]
– Nick
Mar 24 at 22:09
I did array_combine but my array became "1100":1100,"3150":4250,"4430":13110,"5170":18280,"7450":40630,"8230":48860 it skipped repeated numbers but I need them, what can I do ?
– Tanay Acan
Mar 24 at 22:18
1
@TanayAcan looks like you didarray_combine($arr, $out)
instead ofarray_combine($out, $arr)
– Nick
Mar 24 at 22:18
1
@TanayAcan see my edit and updated demo
– Nick
Mar 24 at 22:20
add a comment |
ohh I was doing $arr[$i]+$arr[$i-1]... that was my mistake. Thanks Nick but I have one more question. Now I have 2 arrays, can I show them in same graph ? ($arr is y-axis and $out is x-axis)
– Tanay Acan
Mar 24 at 22:06
1
@TanayAcan judging by your code, you probably just need$arr = array_combine($out, $arr);
. That will give you an array that looks like[1100 => 1100, 4250 => 3150, 8680 => 4430 ...]
– Nick
Mar 24 at 22:09
I did array_combine but my array became "1100":1100,"3150":4250,"4430":13110,"5170":18280,"7450":40630,"8230":48860 it skipped repeated numbers but I need them, what can I do ?
– Tanay Acan
Mar 24 at 22:18
1
@TanayAcan looks like you didarray_combine($arr, $out)
instead ofarray_combine($out, $arr)
– Nick
Mar 24 at 22:18
1
@TanayAcan see my edit and updated demo
– Nick
Mar 24 at 22:20
ohh I was doing $arr[$i]+$arr[$i-1]... that was my mistake. Thanks Nick but I have one more question. Now I have 2 arrays, can I show them in same graph ? ($arr is y-axis and $out is x-axis)
– Tanay Acan
Mar 24 at 22:06
ohh I was doing $arr[$i]+$arr[$i-1]... that was my mistake. Thanks Nick but I have one more question. Now I have 2 arrays, can I show them in same graph ? ($arr is y-axis and $out is x-axis)
– Tanay Acan
Mar 24 at 22:06
1
1
@TanayAcan judging by your code, you probably just need
$arr = array_combine($out, $arr);
. That will give you an array that looks like [1100 => 1100, 4250 => 3150, 8680 => 4430 ...]
– Nick
Mar 24 at 22:09
@TanayAcan judging by your code, you probably just need
$arr = array_combine($out, $arr);
. That will give you an array that looks like [1100 => 1100, 4250 => 3150, 8680 => 4430 ...]
– Nick
Mar 24 at 22:09
I did array_combine but my array became "1100":1100,"3150":4250,"4430":13110,"5170":18280,"7450":40630,"8230":48860 it skipped repeated numbers but I need them, what can I do ?
– Tanay Acan
Mar 24 at 22:18
I did array_combine but my array became "1100":1100,"3150":4250,"4430":13110,"5170":18280,"7450":40630,"8230":48860 it skipped repeated numbers but I need them, what can I do ?
– Tanay Acan
Mar 24 at 22:18
1
1
@TanayAcan looks like you did
array_combine($arr, $out)
instead of array_combine($out, $arr)
– Nick
Mar 24 at 22:18
@TanayAcan looks like you did
array_combine($arr, $out)
instead of array_combine($out, $arr)
– Nick
Mar 24 at 22:18
1
1
@TanayAcan see my edit and updated demo
– Nick
Mar 24 at 22:20
@TanayAcan see my edit and updated demo
– Nick
Mar 24 at 22:20
add a comment |
to get that one array into 2 arrays of graph co-ordinates, you can do this:
$x = array();
$y = array();
$running_total = 0;
for($i = 0; $i < count($arr); $i++)
$y[$i] = $arr[$i];
$running_total += $arr[$i];
$x[$i] = $running_total;
This will give you two arrays; array $x that contains a list of your X-coordinates, and $y, that gives you a list of your Y-coordinates; and you will still have access to your original $arr array, should you need to do further calculations on it. Based on your question, I think that will get you what you need.
However, if you are saying you want 1 array where the X co-ordinates are the array indexes, and the value is the array value itself, for example $y[3150] = 4250, then that is impossible; because you have duplicates in your original list, you cannot use those values as array indexes without ending up overwriting them.
(At least, not without making each array value an array itself, but that is taking things an order of magnitude above where is probably necessary)
Thank you TheMouseMaster, your answer is really useful for me. I just have one more question. How can I show those $x and $y in the same graph ? I couldn't do it
– Tanay Acan
Mar 24 at 22:19
sadly, I cannot answer that. I am not familiar with theCanvasJS
package you are using :(
– TheMouseMaster
Mar 24 at 22:20
I understand, no problem you helped a lot. thank you :)
– Tanay Acan
Mar 24 at 22:21
add a comment |
to get that one array into 2 arrays of graph co-ordinates, you can do this:
$x = array();
$y = array();
$running_total = 0;
for($i = 0; $i < count($arr); $i++)
$y[$i] = $arr[$i];
$running_total += $arr[$i];
$x[$i] = $running_total;
This will give you two arrays; array $x that contains a list of your X-coordinates, and $y, that gives you a list of your Y-coordinates; and you will still have access to your original $arr array, should you need to do further calculations on it. Based on your question, I think that will get you what you need.
However, if you are saying you want 1 array where the X co-ordinates are the array indexes, and the value is the array value itself, for example $y[3150] = 4250, then that is impossible; because you have duplicates in your original list, you cannot use those values as array indexes without ending up overwriting them.
(At least, not without making each array value an array itself, but that is taking things an order of magnitude above where is probably necessary)
Thank you TheMouseMaster, your answer is really useful for me. I just have one more question. How can I show those $x and $y in the same graph ? I couldn't do it
– Tanay Acan
Mar 24 at 22:19
sadly, I cannot answer that. I am not familiar with theCanvasJS
package you are using :(
– TheMouseMaster
Mar 24 at 22:20
I understand, no problem you helped a lot. thank you :)
– Tanay Acan
Mar 24 at 22:21
add a comment |
to get that one array into 2 arrays of graph co-ordinates, you can do this:
$x = array();
$y = array();
$running_total = 0;
for($i = 0; $i < count($arr); $i++)
$y[$i] = $arr[$i];
$running_total += $arr[$i];
$x[$i] = $running_total;
This will give you two arrays; array $x that contains a list of your X-coordinates, and $y, that gives you a list of your Y-coordinates; and you will still have access to your original $arr array, should you need to do further calculations on it. Based on your question, I think that will get you what you need.
However, if you are saying you want 1 array where the X co-ordinates are the array indexes, and the value is the array value itself, for example $y[3150] = 4250, then that is impossible; because you have duplicates in your original list, you cannot use those values as array indexes without ending up overwriting them.
(At least, not without making each array value an array itself, but that is taking things an order of magnitude above where is probably necessary)
to get that one array into 2 arrays of graph co-ordinates, you can do this:
$x = array();
$y = array();
$running_total = 0;
for($i = 0; $i < count($arr); $i++)
$y[$i] = $arr[$i];
$running_total += $arr[$i];
$x[$i] = $running_total;
This will give you two arrays; array $x that contains a list of your X-coordinates, and $y, that gives you a list of your Y-coordinates; and you will still have access to your original $arr array, should you need to do further calculations on it. Based on your question, I think that will get you what you need.
However, if you are saying you want 1 array where the X co-ordinates are the array indexes, and the value is the array value itself, for example $y[3150] = 4250, then that is impossible; because you have duplicates in your original list, you cannot use those values as array indexes without ending up overwriting them.
(At least, not without making each array value an array itself, but that is taking things an order of magnitude above where is probably necessary)
answered Mar 24 at 22:09


TheMouseMasterTheMouseMaster
28510
28510
Thank you TheMouseMaster, your answer is really useful for me. I just have one more question. How can I show those $x and $y in the same graph ? I couldn't do it
– Tanay Acan
Mar 24 at 22:19
sadly, I cannot answer that. I am not familiar with theCanvasJS
package you are using :(
– TheMouseMaster
Mar 24 at 22:20
I understand, no problem you helped a lot. thank you :)
– Tanay Acan
Mar 24 at 22:21
add a comment |
Thank you TheMouseMaster, your answer is really useful for me. I just have one more question. How can I show those $x and $y in the same graph ? I couldn't do it
– Tanay Acan
Mar 24 at 22:19
sadly, I cannot answer that. I am not familiar with theCanvasJS
package you are using :(
– TheMouseMaster
Mar 24 at 22:20
I understand, no problem you helped a lot. thank you :)
– Tanay Acan
Mar 24 at 22:21
Thank you TheMouseMaster, your answer is really useful for me. I just have one more question. How can I show those $x and $y in the same graph ? I couldn't do it
– Tanay Acan
Mar 24 at 22:19
Thank you TheMouseMaster, your answer is really useful for me. I just have one more question. How can I show those $x and $y in the same graph ? I couldn't do it
– Tanay Acan
Mar 24 at 22:19
sadly, I cannot answer that. I am not familiar with the
CanvasJS
package you are using :(– TheMouseMaster
Mar 24 at 22:20
sadly, I cannot answer that. I am not familiar with the
CanvasJS
package you are using :(– TheMouseMaster
Mar 24 at 22:20
I understand, no problem you helped a lot. thank you :)
– Tanay Acan
Mar 24 at 22:21
I understand, no problem you helped a lot. thank you :)
– Tanay Acan
Mar 24 at 22:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328954%2fphp-sum-up-the-numbers-in-an-array-one-by-one%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown