Hello everyone, my code shows that I can add an array element to one set but not another and I am not seeing where is the problem.
I am using the nulls as a separator in my graph.
//---------------------------------------------------------------------------------------
// reindex array and insert nulls
//---------------------------------------------------------------------------------------
// $i element counter
// $j array sequence counter
// $k differentiates array sets
// first time only - insert new elements into array to correct positioning of array
if ($i = 1) {
// Set the index number from three
$New_start_index = 1; // set index starting number or letter
$arr = $data_array; // copy data_array to new array
// perform the reindex
$arr = array_combine(range(2, count($arr) + (1)), array_values($arr));
// copy reindexed array to data_array
$data_array = $arr;
// insert new first record into data_array
array_splice( $data_array, 0, 0, "null");
} // end if
// insert new null record throughout array
foreach($data_array as $key => $value) {
if ($i % 7 == 0) {
array_splice( $data_array, $i, 0, "null"); // insert new elements at intervals
}
$i++; $j++;
} // end foreach
// append null to last record of array
$y = "null"; array_splice($data_array, count($data_array), 0, array($y) );
//$data_array_count = count($data_array); echo $data_array_count; echo "<br /><br />"."\n";
//echo "<pre>"; print_r($data_array); echo "</pre>"; echo "<br /><br />"."\n";
//---------------------------------------------------------------------------------------
// assign canvasjs indexLabel and append to array element where needed
//---------------------------------------------------------------------------------------
/* test data before nulls
5,8,21,32,35,3,
4,8,12,31,36,3,
6,13,16,20,45,3
*/
/* test data after inserting nulls
null,5,8,21,32,35,3,
null,4,8,12,31,36,3,
null,6,13,16,20,45,3,
null
*/
$i = 1; $j = 1; $k = 1; // reset counters
foreach($data_array as $key => $value) {
// display values before loop for debugging
// displays array set, element counter, sequence, key, value
//echo $k.", ".$i.", ".$j.", ".$key.", ".$value; echo "<br />"."\n";
$j = $k; // differentiates array sets
if ($i % 7 === 0) { $k = $k + 1; } // increments k array set count
// use modulus to assign array elements to points
if ($i % 7 === 0) { $points = array( "name" => $j, "x" => $key, "y" => $value ); }
if ($i % 7 === 1) { $points = array( "name" => $j, "x" => $key, "y" => $value ); }
if ($i % 7 === 2) { $points = array( "name" => $j, "x" => $key, "y" => $value ); }
if ($i % 7 === 3) { $points = array( "name" => $j, "x" => $key, "y" => $value ); }
if ($i % 7 === 4) { $points = array( "name" => $j, "x" => $key, "y" => $value ); }
if ($i % 7 === 5) { $points = array( "name" => $j, "x" => $key, "y" => $value ); }
// this displays the extra assigned element indexLabel
if ($i % 7 === 6) {
$points = array( "name" => $j, "x" => $key, "y" => $value, "indexLabel" => "displays" ); }
// The problem is that it does not display the extra assigned element here
// assign canvasjs indexLabel and append to array element
if ($i % 7 === 7) {
$points = array( "name" => $j, "x" => $key, "y" => $value, "indexLabel" => "no display" ); }
/*
various attempts using alternate methods
if ($i % 7 === 7) {
//$points = array( "name" => $j, "x" => $key, "y" => $value );
//$points = array( "name" => $j, "x" => $key, "y" => $value, "indexLabel" => "hit" );
//$replacement = array( "name" => $j, "x" => $key, "y" => $value, "indexLabel" => "hit" );
//$points = array_replace($points, $replacement);
}
*/
array_push($dataPoints, $points); // populate array set with separators
$i++; // increase element counter
} // end foreach
echo '<h2 style="margin-left: 10px; text-align: left;">json_encode points for debugging: </h2>';
echo json_encode($dataPoints); echo "<br /><br />";
//$data_array_count = count($dataPoints); echo $data_array_count; echo "<br /><br />"."\n";
//echo "<pre>"; print_r($dataPoints); echo "</pre>"; echo "<br /><br />"."\n";
//echo "<pre>"; var_dump($dataPoints); echo "</pre>"; echo "<br />"."\n";
// Arithmetic Operators integer modulo
// https://www.php.net/manual/en/language.operators.arithmetic.php
**output from the above**
json_encode points for debugging:
[{"name":1,"x":0,"y":"null"},{"name":1,"x":1,"y":5},{"name":1,"x":2,"y":8},{"name":1,"x":3,"y":21},{"name":1,"x":4,"y":32},{"name":1,"x":5,"y":35,"indexLabel":"displays"},{"name":1,"x":6,"y":3},{"name":2,"x":7,"y":"null"},{"name":2,"x":8,"y":4},{"name":2,"x":9,"y":8},{"name":2,"x":10,"y":12},{"name":2,"x":11,"y":31},{"name":2,"x":12,"y":36,"indexLabel":"displays"},{"name":2,"x":13,"y":3},{"name":3,"x":14,"y":"null"},{"name":3,"x":15,"y":6},{"name":3,"x":16,"y":13},{"name":3,"x":17,"y":16},{"name":3,"x":18,"y":20},{"name":3,"x":19,"y":45,"indexLabel":"displays"},{"name":3,"x":20,"y":3},{"name":4,"x":21,"y":"null"}]