I need to build a multidimensional array from records retrieved from my database.
Here is the code I have:
<?php
/*
...some code...
*/
foreach($data['colleges'] as $college)
{
$college_temp[] = $college->name;
$college_temp[] = $college->abbrev;
$college_temp[] = $college->long_name;
$college_temp[] = $college->long_abbrev;
$college_temp[] = $college->url;
$college_temp[] = $college->description;
}
All the records from the database are going one after the other in the array. I need to optimize this using a multidimensional array.
$college_temp = array();
foreach($data['colleges'] as $college)
{
$college_temp[] = $college;
}
// Echo the first one's name
echo $college_temp[0]['name'];
// Echo the second one's url
echo $college_temp[1]['url'];
$college_temp = array();
$i = 0;
foreach($data['colleges'] as $college)
{
$college_temp[$i]['name'] = $college->name;
$college_temp[$i]['abbrev'] = $college->abbrev;
$college_temp[$i]['long_name'] = $college->long_name;
$college_temp[$i]['long_abbrev'] = $college->long_abbrev;
$college_temp[$i]['url'] = $college->url;
$college_temp[$i]['description'] = $college->description;
$i++;
}
// var_dump($college_temp); // uncomment to check array contents
Related
With api I get a multidimensional array php. I want to extract data from it with a simple code php.
I extract data from an array this way:
var_dump ($obj['product']);
echo $prise = ($obj['product']['return']['ProductsItem']['0']['Article']);
echo $Brand = ($obj['product']['return']['ProductsItem']['0']['Brand']);
echo $Currency = ($obj['product']['return']['ProductsItem']['0']['Currency']);
echo $Name = ($obj['product']['return']['ProductsItem']['0']['Name']);
echo $StockItem = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['0']['Price']);
echo $StockItem = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['0']['TransferTime']);
echo $StockItem1 = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['0']['Count']);
echo $StockItem2 = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['1']['Count']);
How do I simplify the code? How can the data in the array change?
<?php
foreach ($obj['product']['return']['ProductsItem'] as $productsitem) {
$article = $productsitem['Article'];
$brand = $productsitem['Brand'];
$currency = $productsitem['Currency'];
$name = $productsitem['Name'];
foreach ($productsitem['Stock']['StockItem'] as $stockitem) {
$price = $stockitem['Price'];
$transfertime = $stockitem['TransferTime'];
$count = $stockitem['Count'];
}
}
I have a query that I execute, then I use
while($row = $result->fetch_assoc()){
$data[] = $row;
I know this is grotesque, but I'm unsure how to make this an object or a multidimensional array so I just made something like this..
$NUH2016 = array();
$NUH2017 = array();
$NUH2018 = array();
$NUH2019 = array();
$NUH2020 = array();
$NUH2021 = array();
$RBN2016 = array();
$RBN2017 = array(); ...
...$GDT2019 = array();
$GDT2020 = array();
$GDT2021 = array();
while($row = $result->fetch_assoc()){
$data[] = $row;
if($row['Location'] == 'NUH' && $row['Year'] == '2016'){
$NUH2016[] = $row['P_1'];
$NUH2016[] = $row['P_2'];
$NUH2016[] = $row['P_3'];
$NUH2016[] = $row['P_4'];
}
if($row['Location'] == 'NUH' && $row['Year'] == '2017'){
$NUH2017[] = $row['P_1'];
$NUH2017[] = $row['P_2'];
$NUH2017[] = $row['P_3'];
$NUH2017[] = $row['P_4'];
}
//...for all locations and years
...then
$content = array();
$results = array();
$NUH2016_total = array_sum($NUH2016);
$NUH2017_total = array_sum($NUH2017);
$NUH2018_total = array_sum($NUH2018);
$NUH2019_total = array_sum($NUH2019);
$NUH2020_total = array_sum($NUH2020);
$NUH2021_total = array_sum($NUH2021);
$results['NUH~2016'] = $NUH2016_total;
$results['NUH~2017'] = $NUH2017_total;
$results['NUH~2018'] = $NUH2018_total;
$results['NUH~2019'] = $NUH2019_total;
$results['NUH~2020'] = $NUH2020_total;
$results['NUH~2021'] = $NUH2021_total;
$RBN2016_total = array_sum($RBN2016);
$RBN2017_total = array_sum($RBN2017);
$RBN2018_total = array_sum($RBN2018);
$RBN2019_total = array_sum($RBN2019);
$RBN2020_total = array_sum($RBN2020);
$RBN2021_total = array_sum($RBN2021);
$results['RBN~2016'] = $RBN2016_total;
$results['RBN~2017'] = $RBN2017_total;
$results['RBN~2018'] = $RBN2018_total;
$results['RBN~2019'] = $RBN2019_total;
$results['RBN~2020'] = $RBN2020_total;
$results['RBN~2021'] = $RBN2021_total;
...etc
$content['Results'] = $results;
$response = json_encode($content);
echo $response;
So what I want to do is make an object or associative array that looks like this.
content {
produced hours{
nuh{ 2016:16,000
2017:8,000
...
}
rbn{ 2016:9,000
2017:whatever
....
}
man hours{
nuh{ 2016:4,000
2017:2,000
...
}
rbn{ 2016:1,000
2017:more stuff
....
}
but I'm pretty new to php objects. How can I refactor it so that I don't have to initialize every single empty array, have a bunch of if statements to make each entry from the query fit into an array to be summed, and then put it all into an object at the end to get back from an ajax success function?
There's no need to declare so many arrays. Simply change your while() loop in the following way,
$resultArr = array();
while($row = $result->fetch_assoc()){
$tmpArr = array($row['P_1'], $row['P_2'], $row['P_3'], $row['P_4']);
$sumTotal = array_sum($tmpArr);
$tmpArr['sumTotal'] = $sumTotal;
$resultArr[$row['Location']][$row['Year']][] = $tmpArr;
}
Later, apply json_encode() on the result array like this,
$json = json_encode($resultArr);
Sidenote: If you want to see the complete array structure, do var_dump($resultArr);
You can have an array of arrays, so you don't need to manually assign them all.
$LocationAndYears;
while($row = $result->fetch_assoc())
{
$LocationAndYears[$row['Location']] [$row['Year']] [0] = $row['P_1'];
$LocationAndYears[$row['Location']] [$row['Year']] [1] = $row['P_2'];
$LocationAndYears[$row['Location']] [$row['Year']] [2] = $row['P_3'];
$LocationAndYears[$row['Location']] [$row['Year']] [3] = $row['P_4'];
$LocationAndYears[$row['Location']] [$row['Year']] ['sum'] = array_sum($LocationAndYears[$row['Location']] [$row['Year']]);
}
echo $LocationAndYears['NUH']['2012'][0];
echo "Sum for 2012 at NUH:" . $LocationAndYears['NUH']['2012']['sum'];
print_r($LocationAndYears);
Without writing the whole thing, look into multi-dimensional arrays.
Something like this maybe...
But really I think the answer to this is to point you at multi-dimensional arrays as it's very vague
$data = array();
while($row = $result->fetch_assoc()){
$location = $row['Location'];
$year = $row['Year'];
$data[$location] = $data[$location] ? $data[$location] : array();
$data[$location][$year] = $data[$location][$year] ? $data[$location][$year] : array();
$data[$location][$year][] = $row['P_1'];
$data[$location][$year][] = $row['P_2'];
$data[$location][$year][] = $row['P_3'];
$data[$location][$year][] = $row['P_4'];
}
I have a problem with array push.I cant get the all data from the array array.I want to get like this format.
[["username","average"],["aa",2.34],["bb",6.7],["hh",9.8]]
here is my code
while($acc_rs = mysql_fetch_array($acc_qry))
{
$acc_cnt = $acc_rs['Total_login'];
$time_stamp = $acc_rs['last_logged'];
$avg_login = $acc_rs['avg'];
$name = $acc_rs['name'];
$ji = array();
$sal = array("username","average");
$kk = array($name,$avg_login);
array_push($ji,$sal,$kk);
}
array_push($da,$new,$average);
$result = array(array('username', 'average'));
while ($row = mysql_fetch_assoc($acc_qry)) {
$result[] = array($row['name'], $row['avg']);
}
echo json_encode($result);
do {
$pet_name = $row['pet_called'];
$ary = array($pet_name);
}while($row_pet = mysql_fetch_assoc($pet));
When called it must display the value, which can be done by the for loop etc.
echo $ary[0..x];
This is not working and how do i get to do this?
$ary = array();
while ($row = mysql_fetch_assoc($pet)) {
$ary[] = $row['pet_called'];
}
$ary[] = ... is the syntax to add an element to an array.
Don't use do...while. Nothing will be set to $row in the first iteration. This is why it doesn't work. Try just while:
$ary = array();
while($row_pet = mysql_fetch_assoc($pet))
{
$ary[] = $row_pet['pet_called'];
}
I have a while loop that loops through 3 results and echo's these out in a list. It will always be 3 results.
Here is my current PHP:
while($row = sqlsrv_fetch_array($res))
{
echo "<li>".$row['SessionValue']."</li>";
// prefer to store each value in its own variable
}
However I'd like to store the $row['SessionValue'] value in each loop in a new variable.
So....
first loop: $i0 = $row['SessionValue'];
second loop: $i1 = $row['SessionValue'];
third loop: $i2 = $row['SessionValue'];
How would I achieve this with PHP?
Many thanks for any pointers.
$lst_count = array();
while($row = sqlsrv_fetch_array($res))
$lst_count[] = $row["SessionValue"];
You just need another variable that gets incremented:
$count = 0;
while($row = sqlsrv_fetch_array($res))
{
${i.$count++} = $row['SessionValue'];
}
You can do this have SUM of all value:
$total = array();
while($row = sqlsrv_fetch_array($res))
{
$total[] = $row["SessionValue"]
} $sumAll = array_sum($total);