I have pulled out data from my database of eps of companies for last 2 years I wanted to sum up eps by using a small formula what happened here is I pulled out data in the form of array now I want to put formula like this `
`
$curr_eps - $old_eps / $old_eps * 100;
With the out put I am getting I am unable to put formula for every company and get separate calculated values
My output data is like this
foreach($data3 as $key => $pr_data) {
$prof_data[] = $pr_data;
}
Array
(
[0] => Array
(
[eps] => -0.28
[year] => 2015
)
[1] => Array
(
[eps] => 3.33
[year] => 2014
)
[2] => Array
(
[eps] => 0.90
[year] => 2015
)
[3] => Array
(
[eps] => 0.81
[year] => 2014
)
[4] => Array
(
[eps] => 1.05
[year] => 2016
)
[5] => Array
(
[eps] => 3.71
[year] => 2015
)
[6] => Array
(
[eps] => 1.61
[year] => 2016
)
[7] => Array
(
[eps] => -0.49
[year] => 2015
)
)
I am wondering to put data here as of without loop this is the output coming on
$prof_data[] = $data3 //This contains array value;
Out Put`
Array
(
[0] => Array
(
[0] => Array
(
[eps] => -0.28
[year] => 2015
[company_id] => 348
)
[1] => Array
(
[eps] => 3.33
[year] => 2014
[company_id] => 348
)
)
[1] => Array
(
[0] => Array
(
[eps] => 0.90
[year] => 2015
[company_id] => 351
)
[1] => Array
(
[eps] => 0.81
[year] => 2014
[company_id] => 351
)
)
[2] => Array
(
[0] => Array
(
[eps] => 1.05
[year] => 2016
[company_id] => 356
)
[1] => Array
(
[eps] => 3.71
[year] => 2015
[company_id] => 356
)
)
[3] => Array
(
[0] => Array
(
[eps] => 1.61
[year] => 2016
[company_id] => 366
)
[1] => Array
(
[eps] => -0.49
[year] => 2015
[company_id] => 366
)
)
[4] => Array
(
)
)
Now can anybody help me out to solve this issue I am not good while making to understand what I wam trying to do please let e know if you have queries
As I understand you can perform calculation as
foreach($data3 as $data){
$net = $currenteps - $data['eps'];
}
You haven't defined what are the currenteps and oldeps, however using $data['eps'] and $data['year'], you can access each eps value and respective year of array
Related
well this must a easy one but can't seem to figure it out I have this field range in an array that prints an id which repeats it self on array and would like to convert it to a sequence count starting from 0.
Live example: https://3v4l.org/Sf4nI#v7.0.33
Current output:
Array
(
[0] => Array
(
[range] => 336
[year] => 2020
[month] => 222
)
[1] => Array
(
[range] => 336
[year] => 2020
[month] => 222
)
[2] => Array
(
[range] => 390
[year] => 2020
[month] => 222
)
[3] => Array
(
[range] => 390
[year] => 2021
[month] => 222
)
)
Output I need:
Array
(
[0] => Array
(
[range] => 0
[year] => 2020
[month] => 222
)
[1] => Array
(
[range] => 0
[year] => 2020
[month] => 222
)
[2] => Array
(
[range] => 1
[year] => 2020
[month] => 222
)
[3] => Array
(
[range] => 1
[year] => 2021
[month] => 222
)
)
I think the simple solution here is to extract all range values from the array and remove the duplicated values, to make it start from 0 we will use array_values and flip it to can get the id by range value
$idList = array_flip(array_values(array_unique(array_column($array, 'range'))));
now you can get the id for any range you want like this
$idList[336];
// or
$idList[390];
https://3v4l.org/kUa8D#v7.0.33
hope it's helpful
I'm trying to optimize my app. I need to know if what I wan to do is possible or not.
I made some try based on indexBy whitout success.
Currentely I am able to change the structure of the results array like I need via PHP.
I wan to know if QueryBuilder is able to make that for me and how (if possible).
Here is the query building :
return $this->createQueryBuilder('a')
->select('SUBSTRING(a.creationDate, 1,4) year, TRIM(LEADING \'0\' FROM SUBSTRING(a.creationDate, 6,2)) month, COUNT(a) number')
->where('a.creationDate > :limitDate')
->groupBy('year')
->addGroupBy('month')
->orderBy('year', 'ASC')
->addOrderBy('month', 'ASC')
->setParameter('limitDate', new \DateTime("2000-01-01 00:00:00") )
->getQuery()
->getArrayResult();
Here is the actual result of the query :
Array (
[0] => Array ( [year] => 2016 [month] => 10 [number] => 96 )
[1] => Array ( [year] => 2016 [month] => 11 [number] => 159 )
[2] => Array ( [year] => 2016 [month] => 12 [number] => 118 )
[3] => Array ( [year] => 2016 [month] => 9 [number] => 47 )
[4] => Array ( [year] => 2017 [month] => 1 [number] => 44 )
[5] => Array ( [year] => 2017 [month] => 10 [number] => 47 )
...
)
Here is PHP code I use to change the array like I wan :
$myRestructuredArray = [];
foreach($myArray as $line)
{
$myRestructuredArray [$line['year']][$line['month']] = $line['number'];
}
Here is the result I need :
Array (
[2016] => Array (
[10] => 96
[11] => 159
[12] => 118
[9] => 47 )
[2017] => Array (
[1] => 44
[10] => 47
...
)
After generating group array from an associative array, now the array is in the following format. And to make this group array I have used the following code:
foreach($upcoming_data_arr as $key => $item)
{
$arr[$item['year']][$key] = $item;
}
Array
(
[2018] => Array
(
[0] => Array
(
[id] => 6
[year] => 2018
[month] => 11
)
[1] => Array
(
[id] => 5
[year] => 2018
[month] => 12
)
[2] => Array
(
[id] => 4
[year] => 2018
[month] => 11
)
[3] => Array
(
[id] => 3
[year] => 2018
[month] => 11
)
)
)
Now I need another group array according to month within year group. And in this case group data array will be like following:
Array
(
[2018] => Array
(
[11] => Array
(
[0] => Array
(
[id] => 6
[year] => 2018
[month] => 11
)
[1] => Array
(
[id] => 4
[year] => 2018
[month] => 11
)
[2] => Array
(
[id] => 3
[year] => 2018
[month] => 11
)
)
[12] => Array
(
[0] => Array
(
[id] => 5
[year] => 2018
[month] => 12
)
)
)
)
How we will get this output?
Add one more "dimension" when you are creating an array identified by $item['month'] and let php decide last "dimension" key by []:
foreach($upcoming_data_arr as $key => $item) {
$arr[$item['year']][$item['month']][] = $item;
}
really need your help with this array :
Array
(
[status] => 200
[error] =>
[resource] => Array
(
[type] => stats
[data] => Array
(
[0] => Array
(
[date] => Array
(
[year] => 2015
[month] => 12
)
[currency] => USD
[stats] => Array
(
[count] => 2
[total] => 2.53
[average] => 1.265
)
)
[1] => Array
(
[date] => Array
(
[year] => 2016
[month] => 1
)
[currency] => USD
[stats] => Array
(
[count] => 2
[total] => 15
[average] => 7.5
)
)
[2] => Array
(
[date] => Array
(
[year] => 2016
[month] => 1
)
[currency] => AUD
[stats] => Array
(
[count] => 1
[total] => 15
[average] => 15
)
)
[3] => Array
(
[date] => Array
(
[year] => 2016
[month] => 2
)
[currency] => AUD
[stats] => Array
(
[count] => 7
[total] => 1419.02
[average] => 202.71714285714
)
)
[4] => Array
(
[date] => Array
(
[year] => 2016
[month] => 2
)
[currency] => USD
[stats] => Array
(
[count] => 8
[total] => 2186.4
[average] => 273.3
)
)
[5] => Array
(
[date] => Array
(
[year] => 2016
[month] => 3
)
[currency] => USD
[stats] => Array
(
[count] => 3
[total] => 865
[average] => 288.33333333333
)
)
[6] => Array
(
[date] => Array
(
[year] => 2016
[month] => 3
)
[currency] => AUD
[stats] => Array
(
[count] => 19
[total] => 127279
[average] => 6698.8947368421
)
)
)
)
)
How i can get $value of [month] in the loop??
Help me please!
Do you mean you're trying to access the "month" index within date?
If so, you could try something like this:
Let's say $array is your array.
foreach($array['resource']['data'] as $data) {
echo $data['date']['month']; // Prints every month.
echo "<br/>";
}
Hope this helps.
Try array_column function (available since PHP 5.5)
// $arr is the initial array
$months = array_column($arr, 'month');
http://php.net/manual/ru/function.array-column.php
I Have an array which looks like this:
Array
(
[0] => Array
(
[0] => Array
(
[product] => 2003
[date] => 2010-09-15 13:27:35
[status] => 3
)
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 6
)
)
[1] => Array
(
[0] => Array
(
[product] => 2003
[date] => 2010-09-12 13:27:35
[status] => 1
)
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 4
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
)
[2] => Array
(
[0] =>
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
)
I want to "collapse" each second dimension array and obtain the max DATE value and the max status value.So the first index would return 2010-09-18 13:27:35 and '6' etc.
The problem is further complicated by the empty array in the last index. I would like to use this empty array and report it as the MAX date and status.
Thank in advance!
Thanks for looking everybody. I figured it out.
$date=array();
$status=array();
$availability=array();
foreach($set as $key => $value)
{
foreach($value as $value2)
{
if(isset($value2[1]))
{
$date[$key][]=$value2[1];
$status[$key][]=$value2[2];
}
else
{
$date[$key][]='2022-09-18 13:27:35';
$status[$key][]='0';
}
}
$availability[$key]=array(max($date[$key]),min($status[$key]));
}