PHP Array results - assign to their own variable - php

Hi here are my query results
transsum
-19121111
-17432222
-19873333
-22404444
-21955555
-19716666
I need to place each one of the results into it's own variable
I have this but I don't think it's right
$arr_results = odbc_exec($TD_DB_RESOURCE, $query);
foreach ($row = odbc_fetch_array($arr_results) )
{
$price0 = $row[0];
$price1 = $row[1];
$price2 = $row[2];
$price3 = $row[3];
$price4 = $row[4];
$price5 = $row[5];
}
Updated code
$TD_DB_RESOURCE = open_teradata_resource();
$arr_results = odbc_exec($TD_DB_RESOURCE, $query);
$rows = array();
$i=0;
while ($myRow = odbc_fetch_array($arr_results) )
{
$rows[$i] = $myRow;
$i++;
}
outputs
Array ( [0] => Array ( [TOTAL] => -19126241 ) [1] => Array ( [TOTAL] => -17439360 ) [2] => Array ( [TOTAL] => -19871999 ) [3] => Array ( [TOTAL] => -22409254 ) [4] => Array ( [TOTAL] => -21950605 ) [5] => Array ( [TOTAL] => -19710526 ) )

If what you want is an array with totals, you can use this:
$prices = [];
while ($myRow = odbc_fetch_array($arr_results)) {
$prices[] = $myRow['TOTAL'];
}
Afterwards, the contents of $prices should be:
[-19126241, -17439360, ...]

You can dynamically create variables with curly braces:
$arr_results = odbc_exec($TD_DB_RESOURCE, $query);
$count = 0;
foreach ($row = odbc_fetch_array($arr_results)) {
$price{$count} = $row;
$count++;
}

You can do something like:
<?php
$arr_result ...
$index=0;
foreach($row = odbc_fetch_array($arr_results)){
${'price'.$index++} = $row;
}

$arr_results = odbc_exec($TD_DB_RESOURCE, $query);
$i=0;
$prices = array(); //Make a dynamic array of elements
foreach ($row = odbc_fetch_array($arr_results) )
{
$prices[$i] = $row[$i];
$i++;
}
Then use the array elements anywhere in you calculations like
$c = $price[0]+$price[1];

Related

implode arrays in loop foreach but the result is double php

I want to get data from an array according to the code of the item above. but the array that I made actually produces a double result. the data array that I can only fit is the item
$query = "SELECT IDBRG,Disc FROM $RTL.tmasterbarang1 WHERE IDBRG IN (1167646,1170635,1170634)";
// echo $query;
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{
$a = $row['IDBRG'];
$b = $row['Disc'];
$arr1 = str_split($b);
foreach($arr1 as $x=>$x_value)
{
echo "<br>";
$queryz = "SELECT CAT_DISC_NBR FROM $RTL.cat_disc_test WHERE FLR_NBR = 1 AND CAT_DISC_CD = '".$x_value."' ";
// echo $queryz;
$resultz = mysqli_query($connect, $queryz);
$rowz = mysqli_fetch_array($resultz);
$elements[] = $rowz['CAT_DISC_NBR'];
}
echo $all = implode(',', $elements);
}
result of print_r($arr1)."<br>";
1. Array ( [0] => Y [1] => 4 [2] => S [3] => V )
2. Array ( [0] => Y [1] => 4 [2] => S [3] => V )
3. Array ( [0] => Y [1] => G [2] => 3 )
// and the each of array have value Y=69, 4=39, S=66, V=66, G=51, 3=38
echo $all = implode(',', $elements);
69,39,63,66
69,39,63,66,69,39,63,66
69,39,63,66,69,39,63,66,69,51,38
// i want it should be
69,39,63,66
69,39,63,66
69,51,38
When you add the data at
$elements[] = $rowz['CAT_DISC_NBR'];
this just keeps on adding data and the $elements array is never cleared out, so the next loop will just keep on adding data to the results of the last loop.
You need something like...
$a = $row['IDBRG'];
$b = $row['Disc'];
$arr1 = str_split($b);
$elements = []; // Reset list
This happens because you are adding data to elements every time and you never set it to empty again. You should empty the elements array before each foreach loop:
$elements=[];
foreach($arr1 as $x=>$x_value)
{
echo "<br>";
$queryz = "SELECT CAT_DISC_NBR FROM $RTL.cat_disc_test WHERE FLR_NBR = 1 AND CAT_DISC_CD = '".$x_value."' ";
// echo $queryz;
$resultz = mysqli_query($connect, $queryz);
$rowz = mysqli_fetch_array($resultz);
$elements[] = $rowz['CAT_DISC_NBR'];
}

Combine same indexes of array

I have an array which as dynamic nested indexes in e.g. I am just using 2 nested indexes.
Array
(
[0] => Array
(
[0] => 41373
[1] => 41371
[2] => 41369
[3] => 41370
)
[1] => Array
(
[0] => 41378
[1] => 41377
[2] => 41376
[3] => 41375
)
)
Now I want to create a single array like below. This will have 1st index of first array then 1st index of 2nd array, 2nd index of first array then 2nd index of 2nd array, and so on. See below
array(
[0] =>41373
[1] => 41378
[2] => 41371
[3] => 41377
[4] => 41369
[5] => 41376
[6] => 41370
[7] => 41375
)
You can do something like this:
$results = [];
$array = [[1,2,3,4], [1,2,3,4], [1,2,3,4]];
$count = 1;
$size = count($array)-1;
foreach ($array[0] as $key => $value)
{
$results[] = $value;
while($count <= $size)
{
$results[] = $array[$count][$key];
$count++;
}
$count = 1;
}
I think you need something like this:
function dd(array $arrays): array
{
$bufferArray = [];
foreach($arrays as $array) {
$bufferArray = array_merge_recursive($bufferArray, $array);
}
return $bufferArray;
}
$array1 = ['41373','41371','41369','41370'];
$array2 = ['41378','41377', '41376', '41375'];
$return = array();
$count = count($array1)+count($array2);
for($i=0;$i<($count);$i++){
if($i%2==1){
array_push($return, array_shift($array1));
}
else {
array_push($return, array_shift($array2));
}
}
print_r($return);
first count the arrays in the given array, then count the elements in the first array, than loop over that. All arrays should have the same length, or the first one should be the longest.
$laArray = [
['41373','41371','41369','41370'],
['41378', '41377', '41376', '41375'],
['43378', '43377', '43376', '43375'],
];
$lnNested = count($laArray);
$lnElements = count($laArray[0]);
$laResult = [];
for($lnOuter = 0;$lnOuter < $lnElements; $lnOuter++) {
for($lnInner = 0; $lnInner < $lnNested; $lnInner++) {
if(isset($laArray[$lnInner][$lnOuter])) {
$laResult[] = $laArray[$lnInner][$lnOuter];
}
}
}
this would be the simplest solution:
$firstarr = ['41373','41371','41369','41370'];
$secondarr = ['41378','41377','41376','41375'];
$allcounged = count($firstarr)+count($secondarr);
$dividedintotwo = $allcounged/2;
$i = 0;
while ($i<$dividedintotwo) {
echo $firstarr[$i]."<br>";
echo $secondarr[$i]."<br>";
$i++;
}

Passed multidimensional array to view in codeigniter

I'm trying to create foreach statement using multidimensional array.
Controller:
function index()
{
$index1 = 0;
$index2 = 0;
$index3 = 0;
$index4 = 0;
$result1 = $this->data->get_test('kdprogram','kdprogram');
foreach($result1 as $row1){
$array_temp[$index1] = $row1;
$result2 = $this->data->get_test('kdgiat','kdgiat','kdprogram = '.$row1['kdprogram']);
foreach($result2 as $row2){
$array_temp[$index1][$index2] = $row2;
$result3 = $this->data->get_test('kdoutput','kdoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat']);
foreach($result3 as $row3){
$array_temp[$index1][$index2][$index3] = $row3;
$result4 = $this->data->get_test('kdsoutput','kdsoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat'] .' and kdoutput = '.$row3['kdoutput']);
foreach($result4 as $row4){
$array_temp[$index1][$index2][$index3][$index4] = $row4;
$index4++;
}
$index3 ++;
}
$index2 ++;
}
$index1 ++;
}
//print_r($array_temp);
$data['damn'] = $array_temp;
$this->load->view('report/laporan_output', $data);
}
$data contains:
Array
(
[0] => Array
(
[kdprogram] => 06
[0] => Array
(
[kdgiat] => 3400
[0] => Array
(
[kdoutput] => 001
[0] => Array
(
[kdsoutput] => 001
)
[1] => Array
(
[kdsoutput] => 006
)
)
[1] => Array
(
[kdoutput] => 008
[2] => Array
(
[kdsoutput] => 001
)
)
)
)
)
How to echo each array (kdprogram, kdgiat, etc) on view especially with html table?
Am i doing it right?
Thanks
it looks kinda ugly and i would use some sort of recursive function but here is your way
in controller ( i assume the arrays have numeric index otherwise you have to use some sort of counter like you did in your code )
foreach($result1 as $a_counter=>$row1)
{
$array_temp[$a_counter] = array( 'parent'=>$row1 , 'child'=>array());
$result2 = $this->data->get_test('kdgiat','kdgiat','kdprogram = '.$row1['kdprogram']);
foreach($result2 as $b_counter=> $row2)
{
$array_temp[$a_counter]['child'][$b_counter] = array( 'parent'=>$row2 , 'child'=>array());
$result3 = $this->data->get_test('kdoutput','kdoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat']);
foreach($result3 as $c_counter=>$row3)
{
$array_temp[$a_counter]['child'][$b_counter]['child'][$c_counter] = array( 'parent'=>$row3 , 'child'=>array());
$result4 = $this->data->get_test('kdsoutput','kdsoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat'] .' and kdoutput = '.$row3['kdoutput']);
foreach($result4 as $row4)
{
$array_temp[$a_counter]['child'][$b_counter]['child'][$c_counter]['child'][] = $row;
}
}
}
}
in the view
foreach($result as $a )
{
// show a
foreach($a['child'] as $b )
{
// show b
foreach($b['child'] as $c )
{
// show c
foreach($c['child'] as $d )
{
// show d
}
}
}
}

PHP Insert Into MultiDimensional Array

I have a functon that is passed an array of url's. I am extracting data from each webpage and then assigning each piece of data to an array. Here's my function:
function getitems ($urls) {
$iteminfo = array();
foreach($urls as $link) {
$circdl = my_curl($link);
$circqp = htmlqp($circdl,'body');
$itemtitle = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('title');
$itemlink = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('src');
$itemdesc = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('alt');
$iteminfo[][] = $itemtitle;
//$iteminfo[$itemtitle][] = $itemlink;
//$iteminfo[$itemtitle][] = $itemdesc;
}
return $iteminfo;
}
I want the array to look like this:
Array ( [0] => Array ( [0] => title [1] => link [2] => desc ) [1] => Array ( [0] => title [1] => link [2] => desc ) [2] => Array ( [0] => title [1] => link [2] => desc ) )
But I can't wrap my head around how to additional fields to the sub-arrays.
try something like this
function getitems ($urls) {
$iteminfo = array();
$i = 0;
foreach($urls as $link) {
$circdl = my_curl($link);
$circqp = htmlqp($circdl,'body');
$itemtitle = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('title');
$itemlink = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('src');
$itemdesc = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('alt');
$iteminfo[$i][] = $itemtitle;
$iteminfo[$i][] = $itemlink;
$iteminfo[$i][] = $itemdesc;
$i++;
}
return $iteminfo;
}
Everything is ok, you just have to assign index to each of your rows.
If i understand you correctly...
$iteminfo[] = array($itemtitle, $itemlink, $itemdesc);
function getitems ($urls) {
$iteminfo = array();
foreach($urls as $link) {
$subInfo = array();
$circdl = my_curl($link);
$circqp = htmlqp($circdl,'body');
$subInfo[] = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('title');
$subInfo[] = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('src');
$subInfo[] = $circqp->branch()->find('div[class="col-item"]')->children('img')->attr('alt');
$iteminfo[] = $subInfo;
}
return $iteminfo;
}
You could easily replace
$iteminfo[][] = $itemtitle;
//$iteminfo[$itemtitle][] = $itemlink;
//$iteminfo[$itemtitle][] = $itemdesc;
with
$iteminfo = array($itemtitle, $itemlink, $itemdesc);
You can do this because the syntax
$array = $element; // where $array = array();
is just another way to add element to an array in PHP and $element can be an array() as well.

Creating An Associative Multi Dimensional Array from loop in PHP

How can I create an array like the following in PHP from a database result set using a loop:
Array
(
[T] => Array
(
[0] => Array
(
[id] => 1
[name] => Timer
)
[1] => Array
(
[id] => 2
[name] => Tub
)
)
[P] => Array
(
[0] => Array
(
[id] => 3
[name] => Paper
)
[1] => Array
(
[id] => 4
[name] => Puppy
)
)
)
You will notice that the array keys are a letter, which is taken from the 'name' value in the result set. The loop will be something like this:
while($result = $db->fetch($query) {
$key = $result['name']{0};
// your answer :-)
}
I think something like this should do it:
$sql = 'SELECT id, name FROM table';
$result = mysql_query( $sql);
$answer = array();
while( $row = mysql_fetch_assoc( $result))
{
$answer[ strtoupper($row['name'][0]) ][] = $row;
}
mysql_free_result( $result);
var_dump( $answer);
OR, to be more specific (if your query is returning more columns than just id and name):
while( $row = mysql_fetch_assoc( $result))
{
$answer[ strtoupper($row['name'][0]) ][] = array(
'id' => $row['id'],
'name' => $row['name']
);
}
$indexArray = array(); // Array from Example
while($result = $db->fetch($query) {
$key = $result['name']{0};
if(!isset($indexArray[$key])) {
$indexArray[$key] = array();
}
array_push($indexArray[$key], $result);
}
$results = array();
while($result = $db->fetch($query)) {
$key = strtoupper($result['name'][0]);
if(!isset($results[$key]))
$results[$key] = array();
$results[$key][] = $result;
}

Categories