adding elements to the array creates a new array instead of adding - php

i have been trying to add two elements to my array which is always been created. but after i add those elements it creates new element instead adding to the existing one.
code
<?php
Continues from top....
while($finalRes = mysql_fetch_assoc($excute))
{
$tables[] = $finalRes;
}
if(mysql_num_rows($excute) != 0){
$report = new Report();
$ID = substr($table,11);
$log = $report->projecteden($ID);
$a=0;
$b=0;
if(property_exists($log, 'counts')){
foreach ($log->counts as $m)
{
$UNIQUES = $m->count;
$NON-UNIQUES = $m->ucount;
$a += $UNIQUES;
$b += $NON-UNIQUES;
}
$tables['UNIQUES'] = $a;
$tables[] = $tables['UNIQUES'];
$tables['NON-UNIQUES'] = $b;
$tables[] = $tables['NON-UNIQUES'];
}
}
var_dump($tables);
?>
var_dump result
array (size=3)
0 =>
array (size=8)
'ID' => string '105' (length=3)
'name' => string 'R158' (length=11)
'accountname' => string 'DDD' (length=3)
'accountID' => string '1' (length=1)
'stat' => string '2' (length=1)
'total_impr' => string '207' (length=3)
'min(a.timestamp)' => string '2014-05-16 05:38:01' (length=19)
'max(a.timestamp)' => string '2015-01-22 05:50:41' (length=19)
'UNIQUES' => int 45
'NON-UNIQUES' => int 13
as u can see UNIQUES and NON-UNIQUES are not aligned, can any help me to add UNIQUES and NON-UNIQUES to the same $tables array

Assign index like this
your code....
$tables[0]['UNIQUES'] = $a;
$tables[0]['NON-UNIQUES'] = $b;
your code ....
if you want to reemove 0 index then
$tables = $tables[0];

Try with -
while($finalRes = mysql_fetch_assoc($excute)){
$temp = $finalRes;
if(mysql_num_rows($excute) != 0){
$report = new Report();
$ID = substr($table,11);
$log = $report->projecteden($ID);
$a=0;
$b=0;
if(property_exists($log, 'counts')){
foreach ($log->counts as $m)
{
$UNIQUES = $m->count;
$NON-UNIQUES = $m->ucount;
$a += $UNIQUES;
$b += $NON-UNIQUES;
}
array_merge($temp ,array('UNIQUES' => $a));
array_merge($temp ,array('NON-UNIQUES' => $b));
$tables[] = $temp;
}
}
}
var_dump($tables);

1) you can create new temp array and add element to that array.
2) use array_merge to merge element of temp array to our main array with key & value pair.
3) use can use below code. it is working fine.
while($finalRes = mysql_fetch_assoc($excute))
{
$tables[] = $finalRes;
}
if(mysql_num_rows($excute) != 0){
$report = new Report();
$ID = substr($table,11);
$log = $report->projecteden($ID);
$a=0;
$b=0;
if(property_exists($log, 'counts')){
foreach ($log->counts as $m){
$UNIQUES = $m->count;
$NON-UNIQUES = $m->ucount;
$a += $UNIQUES;
$b += $NON-UNIQUES;
}
$temp = array('UNIQUES' => $a,'NON-UNIQUES' => $b);
$tables = array_merge($tables,$temp);
}
}
var_dump($tables);

Related

PHP - add values to already existing array

I have a already defined array, containing values just like the one below:
$arr = ['a','b','c'];
How could one add the following using PHP?
$arr = [
'a' => 10,
'b' => 5,
'c' => 21
]
I have tried:
$arr['a'] = 10 but it throws the error: Undefined index: a
I am surely that I do a stupid mistake.. could someone open my eyes?
Full code below:
$finishes = []; //define array to hold finish types
foreach ($projectstages as $stage) {
if ($stage->finish_type) {
if(!in_array($stage->finish_type, $finishes)){
array_push($finishes, $stage->finish_type);
}
}
}
foreach ($projectunits as $unit) {
$data[$i] = [
'id' => $unit->id,
'project_name' => $unit->project_name,
'block_title' => $unit->block_title,
'unit' => $unit->unit,
'core' => $unit->core,
'floor' => $unit->floor,
'unit_type' => $unit->unit_type,
'tenure_type' => $unit->tenure_type,
'floors' => $unit->unit_floors,
'weelchair' => $unit->weelchair,
'dual_aspect' => $unit->dual_aspect
];
$st = array();
$bs = '';
foreach ($projectstages as $stage) {
$projectmeasure = ProjectMeasure::select('measure')
->where('project_id',$this->projectId)
->where('build_stage_id', $stage->id)
->where('unit_id', $unit->id)
->where('block_id', $unit->block_id)
->where('build_stage_type_id', $stage->build_stage_type_id)
->first();
$st += [
'BST-'.$stage->build_stage_type_id => ($projectmeasure ? $projectmeasure->measure : '0')
];
if (($stage->is_square_meter == 0) && ($stage->is_draft == 0)) {
$height = ($stage->height_override == 0 ? $unit->gross_floor_height : $stage->height_override); //08.14.20: override default height if build stage type has it's own custom height
$st += [
'BST-sqm-'.$stage->build_stage_type_id => ($projectmeasure ? $projectmeasure->measure * $height: '0')
];
if ($stage->finish_type) {
$finishes[$stage->finish_type] += ($projectmeasure ? $projectmeasure->measure * $height: '0') * ($stage->both_side ? 2 : 1); //error is thrown at this line
}
} else {
if ($stage->finish_type) {
$finishes[$stage->finish_type] += ($projectmeasure ? $projectmeasure->measure : '0');
}
}
}
$data[$i] = array_merge($data[$i], $st);
$data[$i] = array_merge($data[$i], $finishes[$stage->finish_type]);
$i++;
}
The above code is used as is and the array $finishes is the one from the first example, called $arr
You're using += in your real code instead of =. That tries to do maths to add to an existing value, whereas = can just assign a new index with that value if it doesn't exist.
+= can't do maths to add a number to nothing. You need to check first if the index exists yet. If it doesn't exist, then assign it with an initial value. If it already exists with a value, then you can add the new value to the existing value.
If you want to convert the array of strings to a collection of keys (elements) and values (integers), you can try the following:
$arr = ['a','b','c'];
$newVals = [10, 5, 21];
function convertArr($arr, $newVals){
if(count($arr) == count($newVals)){
$len = count($arr);
for($i = 0; $i < $len; $i++){
$temp = $arr[$i];
$arr[$temp] = $newVals[$i];
unset($arr[$i]);
}
}
return $arr;
}
print_r(convertArr($arr, $newVals));
Output:
Array ( [a] => 10 [b] => 5 [c] => 21 )

set array key pointer inside a foreach according to a variable php [duplicate]

Im trying to build a little site using XML instead of a database.
I would like to build a next and prev button which will work relative to the content I have displayed.
I found the php function next() and prev() as well as current() but I do not know how to set the pointer to a specific position to be able to navigate relative to the current page.
$list=array('page1','page2','page3')
eg if im displaying contents of page2 how could I tell php i am at $list[1] so that next($list) shows page3?
Thanks in advance
If your array is always indexed consistently (eg. 'page1' is always at index '0'), it's fairly simple:
$List = array('page1', 'page2', 'page3', 'page4', 'page5');
$CurrentPage = 3; // 'page4'
while (key($List) !== $CurrentPage) next($List); // Advance until there's a match
I personally don't rely on automatic indexing because there's always a chance that the automatic index might change. You should consider explicitly defining the keys:
$List = array(
'1' => 'page1',
'2' => 'page2',
'3' => 'page3',
);
EDIT: If you want to test the values of the array (instead of the keys), use current():
while (current($List) !== $CurrentPage) next($List);
Using the functions below, you can get the next and previous values of the array.
If current value is not valid or it is the last (first - for prev) value in the array, then:
the function getNextVal(...) returns the first element value
the function getPrevVal(...) returns the last element value
The functions are cyclic.
function getNextVal(&$array, $curr_val)
{
$next = 0;
reset($array);
do
{
$tmp_val = current($array);
$res = next($array);
} while ( ($tmp_val != $curr_val) && $res );
if( $res )
{
$next = current($array);
}
return $next;
}
function getPrevVal(&$array, $curr_val)
{
end($array);
$prev = current($array);
do
{
$tmp_val = current($array);
$res = prev($array);
} while ( ($tmp_val != $curr_val) && $res );
if( $res )
{
$prev = current($array);
}
return $prev;
}
Using the functions below, you can get the next and previous KEYs of the array.
If current key is not valid or it is the last (first - for prev) key in the array, then:
the function getNext(...) returns 0 (the first element key)
the function getPrev(...) returns the key of the last array element
The functions are cyclic.
function getNext(&$array, $curr_key)
{
$next = 0;
reset($array);
do
{
$tmp_key = key($array);
$res = next($array);
} while ( ($tmp_key != $curr_key) && $res );
if( $res )
{
$next = key($array);
}
return $next;
}
function getPrev(&$array, $curr_key)
{
end($array);
$prev = key($array);
do
{
$tmp_key = key($array);
$res = prev($array);
} while ( ($tmp_key != $curr_key) && $res );
if( $res )
{
$prev = key($array);
}
return $prev;
}
The internal array pointer is mainly used for looping over an array within one PHP script. I wouldn't recommend using it for moving from page to page.
For that, just keep track of the page number and the page size (number of items per page). Then, when you're loading another page, you can use them to decide which array items to show. For example:
$pageNum = $_GET["pageNum"];
$pageSize = 10;
$startIndex = ($pageNum - 1) * $pageSize;
$endIndex = ($startIndex + $pageSize) - 1;
(or something similar)
another aproach without loops or search.
list($prev,$next) = getPrevNext($oObjects,$sCurrentKey);
function getPrevNext($aArray,$key){
$aKeys = array_keys($aArray); //every element of aKeys is obviously unique
$aIndices = array_flip($aKeys); //so array can be flipped without risk
$i = $aIndices[$key]; //index of key in aKeys
if ($i > 0) $prev = $aArray[$aKeys[$i-1]]; //use previous key in aArray
if ($i < count($aKeys)-1) $next = $aArray[$aKeys[$i+1]]; //use next key in aArray
return array($prev,$next);
}
I use this code for set internal pointer with key of array.
reset($List);
while (key($List) !== $id && key($List) !== null) next($List);
if(key($List) === null) end($List);
After that you can use prev() or next().
Update follow notice from #VaclavSir
Try this
public function getNextVal(&$array, $curr_val){
foreach($array as $k=>$v){
if($v['your_key'] == $curr_val){
if(isset($array[$k+1]))
return $array[$k+1];
else
return $array[0];
}
}
}
public function getPrevVal(&$array, $curr_val){
foreach($array as $k=>$v){
if($v['your_key'] == $curr_val){
if(isset($array[$k-1]))
return $array[$k-1];
else
return end($array);
}
}
}
for array like this:
array (size=3)
0 =>
array (size=11)
'id' => string '21' (length=2)
'cat' => string '1' (length=1)
'gal' => string '1' (length=1)
'type' => string 'image' (length=5)
'name' => string 'chalk_art_dies-irea_2nd_pic' (length=27)
'permalink' => string 'chalk-art-dies-irea-2nd-pic' (length=27)
'url' => string 'rxNsPoEiJboJQ32.jpg' (length=19)
'content' => string '' (length=0)
'date' => string '1432076359' (length=10)
'order' => string '20' (length=2)
'views' => string '0' (length=1)
1 =>
array (size=11)
'id' => string '10' (length=2)
'cat' => string '1' (length=1)
'gal' => string '1' (length=1)
'type' => string 'image' (length=5)
'name' => string '3dchalkart' (length=10)
'permalink' => string '3dchalkart' (length=10)
'url' => string 's57i5DKueUEI4lu.jpg' (length=19)
'content' => string '' (length=0)
'date' => string '1432076358' (length=10)
'order' => string '9' (length=1)
'views' => string '0' (length=1)
2 =>
current and next functions are deprecated for objects since PHP 8.1
Here is a way to do it using ArrayIterator
Keys
$list = new ArrayIterator(array('page1', 'page2', 'page3'));
$currentPage = 1;
$list->seek($currentPage);
echo $list->current(); // 'page2'
Values
$list = new ArrayIterator(array('page1', 'page2', 'page3'));
while ($list->current() !== 'page2') {
$list->next();
}
echo $list->current(); // 'page2'
Here's the complete #tjunglc 's approach with looping:
protected function getPrevNext($aArray,$key)
{
$aKeys = array_keys($aArray); //every element of aKeys is obviously unique
$aIndices = array_flip($aKeys); //so array can be flipped without risk
$i = $aIndices[$key]; //index of key in aKeys
if ($i > 0) $prev = $aArray[$aKeys[$i-1]]; //use previous key in aArray
if ($i < count($aKeys)-1) $next = $aArray[$aKeys[$i+1]]; //use next key in aArray
if (!isset($prev)) $prev = end($aArray);
if (!isset($next)) $next = reset($aArray);
return array($prev,$next);
}
Oh and thankx #tjunglc for this :)

Splitting a Associative Array Based off if Condition of Value

I'm combining an array based off a MySQL Query:
$newskus = array();
$newqtys = array();
$sql1 = $db->query("//QUERY//");
while($row = $sql1->fetch_assoc()) {
$newskus[] = $row['SKU'];
$newqtys[] = $row['Quantity'];
}
$newskusandqtys = array_combine($newskus, $newqtys);
This gives me an associative array like so:
Array
(
[Product 1] => -1
[Product 2] => 0
[Product 3] => 3
)
I'm looking to split this array based off the condition of the integer value.
That is, if the value is above 0, it'll put the contents into a variable $newqtysabovezero
Ending with an expected output of
Array
(
[Product 3] => 3
)
And the other way around, if the value is greater than or equal to 0, put this into an array variable $newqtysbelowzero with an expected output of
Array
(
[Product 1] => -1
[Product 2] => 0
)
I'm able to do this with a numerical array, but it of course is not passing the key:
foreach( $newqtys as $item ) {
if ( $item > '0' ) {
$newqtysabovezero[] = $item;
} elseif ( $item <= '0') {
$newqtysbelowzero[] = $item;
}
}
How would I modify this to get my expected output?
Here is one way:
foreach( $newqtys as $key => $item ) {
if ( $item > '0' ) {
$newqtysabovezero[$key] = $item;
} elseif ( $item <= '0') {
$newqtysbelowzero[$key] = $item;
}
}
And here is a method using array_filter():
$in = array(
'Product 1' => -1,
'Product 2' => 0,
'Product 3' => 3
);
function at_and_below($v) {
return $v <= 0;
}
function above($v) {
return $v > 0;
}
$newqtysabovezero = array_filter($in, 'above');
$newqtysbelowzero = array_filter($in, 'at_and_below');
At first, to get all values from the 'Quantity' field as integers you should cast them to integer type:
...
while($row = $sql1->fetch_assoc()) {
$newskus[] = $row['SKU'];
$newqtys[] = (int) $row['Quantity'];
}
$newskusandqtys = array_combine($newskus, $newqtys);
Then, you can easily separate values which are greater than OR equal/less than 0:
foreach (newskusandqtys as $k => $item) {
if ($item > 0) {
$newqtysabovezero[$k] = $item;
} else {
$newqtysbelowzero[$k] = $item;
}
}
You can skip the array_combine part, and just read the values into the final arrays as you fetch them.
while($row = $sql1->fetch_assoc()) {
if ($row['Quantity'] > 0) {
$newqtysabovezero[$row['SKU']] = $row['Quantity'];
} else {
$newqtysbelowzero[$row['SKU']] = $row['Quantity'];
}
}

Convert multidimensional array to nested set array

im having a little problem i need some help with.
Im trying to convert a multidimensional array into a flatten array with nested set values right and left like so:
$array = {
'id' => 1
'name' => 'john'
'childs' => array(
array(
'id' => 1
'name' => 'jane'
)
)
}
to
$array = {
array(
'id' => 1,
'name' => 'john'
'left' => '1'
'right' => '4'
),
array(
'id' => 1,
'name' => 'jane'
'left' => '2'
'right' => '3'
)
}
Any help is appreciated!
I asked a very similar question and got a result, so thought I'd send it on for you. I realise this is quite an old topic, but still worth getting an answer. I've included my data, but would be easily adapted for yours.
$JSON = '[{"id":1,"children":[{"id":2,"children":[{"id":3},{"id":4}]},{"id":5}]}]';
$cleanJSON = json_decode($JSON,true);
$a_newTree = array();
function recurseTree($structure,$previousLeft)
{
global $a_newTree; // Get global Variable to store results in.
$indexed = array(); // Bucket of results.
$indexed['id'] = $structure['id']; // Set ID
$indexed['left'] = $previousLeft + 1; // Set Left
$lastRight = $indexed['left'];
$i_count = 0;
if ($structure['children'])
{
foreach ($structure['children'] as $a_child)
{
$lastRight = recurseTree($structure['children'][$i_count],$lastRight);
$i_count++;
}
}
$indexed['right'] = $lastRight + 1; // Set Right
array_push($a_newTree,$indexed); // Push onto stack
return $indexed['right'];
}
recurseTree($cleanJSON[0],0);
print_r($a_newTree);
function restructRecursive($array, $left = 1) {
if (isset($array['childs'])) {
$result = array();
foreach ($array['childs'] as $child) {
$result = array_merge($result, restructRecursive($child, $left+1));
}
unset($array['childs']);
}
$array['left'] = $left;
return array_merge(array($array), $result);
}
$newStruct = restructRecursive($oldStruct);
For anyone coming here and looking for a solution, loneTraceur's solution works fine. However, I needed one in OOP, so here is my version of it.
<?php
class NestedSet
{
protected $tree = [];
public function deconstruct($tree, $left = 0)
{
$this->flattenTree($tree, $left);
return $this->tree;
}
protected function flattenTree($tree, $left)
{
$indexed = [];
$indexed['id'] = $tree['id'];
$indexed['_lft'] = $left + 1;
$right = $indexed['_lft'];
if (isset($tree['children']) && count($tree['children'])) {
foreach ($tree['children'] as $child) {
$right = $this->flattenTree($child, $right);
}
}
$indexed['_rgt'] = $right + 1;
$this->tree[] = $indexed;
return $indexed['_rgt'];
}
}
You would run it like this:
$NestedSet = new NestedSet;
$flat = $NestedSet->deconstruct($tree):
This code is based on the other answer.

How to set an Arrays internal pointer to a specific position? PHP/XML

Im trying to build a little site using XML instead of a database.
I would like to build a next and prev button which will work relative to the content I have displayed.
I found the php function next() and prev() as well as current() but I do not know how to set the pointer to a specific position to be able to navigate relative to the current page.
$list=array('page1','page2','page3')
eg if im displaying contents of page2 how could I tell php i am at $list[1] so that next($list) shows page3?
Thanks in advance
If your array is always indexed consistently (eg. 'page1' is always at index '0'), it's fairly simple:
$List = array('page1', 'page2', 'page3', 'page4', 'page5');
$CurrentPage = 3; // 'page4'
while (key($List) !== $CurrentPage) next($List); // Advance until there's a match
I personally don't rely on automatic indexing because there's always a chance that the automatic index might change. You should consider explicitly defining the keys:
$List = array(
'1' => 'page1',
'2' => 'page2',
'3' => 'page3',
);
EDIT: If you want to test the values of the array (instead of the keys), use current():
while (current($List) !== $CurrentPage) next($List);
Using the functions below, you can get the next and previous values of the array.
If current value is not valid or it is the last (first - for prev) value in the array, then:
the function getNextVal(...) returns the first element value
the function getPrevVal(...) returns the last element value
The functions are cyclic.
function getNextVal(&$array, $curr_val)
{
$next = 0;
reset($array);
do
{
$tmp_val = current($array);
$res = next($array);
} while ( ($tmp_val != $curr_val) && $res );
if( $res )
{
$next = current($array);
}
return $next;
}
function getPrevVal(&$array, $curr_val)
{
end($array);
$prev = current($array);
do
{
$tmp_val = current($array);
$res = prev($array);
} while ( ($tmp_val != $curr_val) && $res );
if( $res )
{
$prev = current($array);
}
return $prev;
}
Using the functions below, you can get the next and previous KEYs of the array.
If current key is not valid or it is the last (first - for prev) key in the array, then:
the function getNext(...) returns 0 (the first element key)
the function getPrev(...) returns the key of the last array element
The functions are cyclic.
function getNext(&$array, $curr_key)
{
$next = 0;
reset($array);
do
{
$tmp_key = key($array);
$res = next($array);
} while ( ($tmp_key != $curr_key) && $res );
if( $res )
{
$next = key($array);
}
return $next;
}
function getPrev(&$array, $curr_key)
{
end($array);
$prev = key($array);
do
{
$tmp_key = key($array);
$res = prev($array);
} while ( ($tmp_key != $curr_key) && $res );
if( $res )
{
$prev = key($array);
}
return $prev;
}
The internal array pointer is mainly used for looping over an array within one PHP script. I wouldn't recommend using it for moving from page to page.
For that, just keep track of the page number and the page size (number of items per page). Then, when you're loading another page, you can use them to decide which array items to show. For example:
$pageNum = $_GET["pageNum"];
$pageSize = 10;
$startIndex = ($pageNum - 1) * $pageSize;
$endIndex = ($startIndex + $pageSize) - 1;
(or something similar)
another aproach without loops or search.
list($prev,$next) = getPrevNext($oObjects,$sCurrentKey);
function getPrevNext($aArray,$key){
$aKeys = array_keys($aArray); //every element of aKeys is obviously unique
$aIndices = array_flip($aKeys); //so array can be flipped without risk
$i = $aIndices[$key]; //index of key in aKeys
if ($i > 0) $prev = $aArray[$aKeys[$i-1]]; //use previous key in aArray
if ($i < count($aKeys)-1) $next = $aArray[$aKeys[$i+1]]; //use next key in aArray
return array($prev,$next);
}
I use this code for set internal pointer with key of array.
reset($List);
while (key($List) !== $id && key($List) !== null) next($List);
if(key($List) === null) end($List);
After that you can use prev() or next().
Update follow notice from #VaclavSir
Try this
public function getNextVal(&$array, $curr_val){
foreach($array as $k=>$v){
if($v['your_key'] == $curr_val){
if(isset($array[$k+1]))
return $array[$k+1];
else
return $array[0];
}
}
}
public function getPrevVal(&$array, $curr_val){
foreach($array as $k=>$v){
if($v['your_key'] == $curr_val){
if(isset($array[$k-1]))
return $array[$k-1];
else
return end($array);
}
}
}
for array like this:
array (size=3)
0 =>
array (size=11)
'id' => string '21' (length=2)
'cat' => string '1' (length=1)
'gal' => string '1' (length=1)
'type' => string 'image' (length=5)
'name' => string 'chalk_art_dies-irea_2nd_pic' (length=27)
'permalink' => string 'chalk-art-dies-irea-2nd-pic' (length=27)
'url' => string 'rxNsPoEiJboJQ32.jpg' (length=19)
'content' => string '' (length=0)
'date' => string '1432076359' (length=10)
'order' => string '20' (length=2)
'views' => string '0' (length=1)
1 =>
array (size=11)
'id' => string '10' (length=2)
'cat' => string '1' (length=1)
'gal' => string '1' (length=1)
'type' => string 'image' (length=5)
'name' => string '3dchalkart' (length=10)
'permalink' => string '3dchalkart' (length=10)
'url' => string 's57i5DKueUEI4lu.jpg' (length=19)
'content' => string '' (length=0)
'date' => string '1432076358' (length=10)
'order' => string '9' (length=1)
'views' => string '0' (length=1)
2 =>
current and next functions are deprecated for objects since PHP 8.1
Here is a way to do it using ArrayIterator
Keys
$list = new ArrayIterator(array('page1', 'page2', 'page3'));
$currentPage = 1;
$list->seek($currentPage);
echo $list->current(); // 'page2'
Values
$list = new ArrayIterator(array('page1', 'page2', 'page3'));
while ($list->current() !== 'page2') {
$list->next();
}
echo $list->current(); // 'page2'
Here's the complete #tjunglc 's approach with looping:
protected function getPrevNext($aArray,$key)
{
$aKeys = array_keys($aArray); //every element of aKeys is obviously unique
$aIndices = array_flip($aKeys); //so array can be flipped without risk
$i = $aIndices[$key]; //index of key in aKeys
if ($i > 0) $prev = $aArray[$aKeys[$i-1]]; //use previous key in aArray
if ($i < count($aKeys)-1) $next = $aArray[$aKeys[$i+1]]; //use next key in aArray
if (!isset($prev)) $prev = end($aArray);
if (!isset($next)) $next = reset($aArray);
return array($prev,$next);
}
Oh and thankx #tjunglc for this :)

Categories