Am working on some set of PHP array. Am trying to loop through each of them and check
the array whose name is equal to Josw Acade. Am using a for loop but I get zero
after extracting the data. I want to store the data in an array.
Array
array:6 [
0 => array:4 [
"id" => 1
"name" => "Josw Acade"
"value" => "Unlimited"
"plan_type" => "Superior"
]
1 => array:4 [
"id" => 2
"name" => "Verbal"
"value" => "true"
"plan_type" => "Superior"
]
2 => array:4 [
"id" => 12
"name" => "Josw Acade"
"value" => "$1,500,00"
"plan_type" => "Classic"
]
3 => array:4 [
"id" => 13
"name" => "Leon"
"value" => "true"
"plan_type" => "Classic"
]
4 => array:4 [
"id" => 14
"name" => "One Time"
"value" => "true"
"plan_type" => "Classic"
]
5 => array:4 [
"id" => 15
"name" => "Deat"
"value" => "$25,000"
"plan_type" => "Classic"
]
6 => array:4 [
"id" => 23
"name" => "Josw Acade"
"value" => "$100,000"
"plan_type" => "Essential"
]
]
Logic
$Inst = [];
for($med = 0; $med < count($array); $med++){
if($med['name'] == "Josw Acade"){
$Inst = $med['value'];
}
}
dd($Inst);
Your variables is not corretly set in the for loop, you are setting $med = 0 and acessing $med as an array.
Use filter, that runs a condition on each element and returns the items that satisfy that condition.
array_filter($array, function ($item) {
return $item['name'] === 'Josw Acade';
});
In general you don't have to make old school arrays anymore, foreach does the same.
$results = [];
foreach($array as $item)
{
if ($item['name'] === 'Josw Acade') {
$results[] = $item['value'];
}
}
You can use array_filter with callback
$filtered = array_filter($array, function($v){ return $v['name'] == 'Josw Acade'})
print_r($filtered);
You are looping through array; so on each iteration to get values; you need to pass index value and you are missing that. You are using $med as index.
Here is code.
$Inst = [];
for($med = 0; $med < count($array); $med++){
if($array[$med]['name'] == "Josw Acade"){
$Inst[] = $array[$med]['value'];
}
}
there is many way to do this but according to me the best way to use array_filer()
array_filter($array, function ($item) {
return $item['name'] === 'Josw Acade';
});
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 3 years ago.
I have an eloquent object with data from the model which I need to convert to an array and have all indexes from the relations at the same array depth.
I tried array_flatten(), map(), filter() and other PHP methods but couldn't get it working correctly.
How I get my relations:
$data = Person::whereIn('id', $this->id[0])->where('school_id', '=',
$this->school)->with(['personinfo'=>function ($query) {
$query->select(
'person_id',
'general_info',
);
}, 'trades'=>function ($query) {
$query->select('person_id', 'trade_id')->with('trades');
}, 'measurements'=>function ($query) {
$query->select(
'person_id',
'measuring_point_1',
'measuring_point_1_date',
);
}])->get();
return $data->toArray();
What results in the array below, this is as close as I could get using different methods.
This is the outcome of the return function:
array:3 [
1 => array:17 [
"school_id" => 6
"birth_date" => null
"sex_id" => 1
"phone_number" => 62452676867897
"mobile_number" => 62398356786787
"email" => "example#example.com"
"personinfo" => array:5 [
"person_id" => 21
"general_info" => null
"health_info" => null
]
"trades" => array:3 [
"person_id" => 21
"trade_id" => 2
"trades" => array:8 [
"school_id" => 2
"name" => "blablabla"
]
]
"measurements" => array:7 [
"person_id" => 21
"measuring_point_1" => null
"measuring_point_1_date" => null
]
]
];
I need the array to be like this:
array:3 [
1 => array:17 [
"school_id" => 6
"birth_date" => null
"sex_id" => 1
"phone_number" => 624176676867897
"mobile_number" => 649498356786787
"email" => "example#example.com"
"person_id" => 21
"general_info" => null
"health_info" => null
"person_id" => 21
"trade_id" => 2
"school_id" => 2
"name" => "blablabla"
"person_id" => 21
"measuring_point_1" => null
"measuring_point_1_date" => null
]
]
];
Basically, I need to convert the multidimensional array to a zero depth array.
Any help would be appreciated.
You can use custom of array flatten with merging the inner with array-merge as:
function arrayFlatten($array) {
$res = array();
foreach ($array as $k => $v) {
if (is_array($v)) $return = array_merge($res, array_flatten($v)); // add recursively
else $res[$k] = $v; // just add
}
return $res;
}
Now just return arrayFlatten($data->toArray());
Simple live example
How i made it work using dWinder's answer
$data = Person::whereIn('id', $this->id[0])->where('school_id', '=', $this->school)->with(['personinfo'=>function ($query) {
$query->select(
'person_id',
'general_info',
);
}, 'trades'=>function ($query) {
$query->select('person_id', 'trade_id')->with('trades');
}, 'measurements'=>function ($query) {
$query->select(
'person_id',
'measuring_point_1',
'measuring_point_1_date',
);
}])->get();
function array_flatten($array)
{
$return = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$return = array_merge($return, array_flatten($value));
} else {
$return[$key] = $value;
}
}
return $return;
}
foreach ($data as $value) {
$result[] = array_flatten($value->toArray());
}
return $result;
I am trying to create a chart from data returned by a databse. The data is stored in a database and there can be multiple charts in this table. Identified by a unique value. (rigname)
array:102 [▼
0 => array:6 [▼
"id" => 2002
"user_id" => "1"
"rigname" => "home"
"data" => "{"hashrate":12.364,"pool":"eu1.ethermine.org:4444","shares":{"valid":"2","invalid":"0"},"gpus":[{"temperature":"56","fanSpeed":"56","hashrates":12.364}]}"
"created_at" => "2018-02-23 10:24:50"
"updated_at" => "2018-02-23 10:24:50"
],
1 => array:6 [▼
"id" => 2019
"user_id" => "1"
"rigname" => "server"
"data" => "{"hashrate":76.612,"pool":"eu1.ethermine.org:4444","shares":{"valid":"736","invalid":"2"},"gpus":[{"temperature":"65","fanSpeed":"67","hashrates":19.804},{"temp ▶"
"created_at" => "2018-02-23 10:25:56"
"updated_at" => "2018-02-23 10:25:56"
]
As you can see there are two possible charts according to the rigname. I want to display the hashrate value of the two rigs on a chart. This works, but, when one rig stops there is no data being submitted to the database from that rig.
The chart displays no 0 value for that rig, the last known data is being used. (duh)
I solved this by modifying my query like this:
$res[$value] = json_decode(MinerStatistics::where('rigname', $value)
->where('created_at','>=', \Carbon\Carbon::now()->subMinutes(20))
->orderBy('created_at', 'desc')
->get()
->toJson(), true
);
Now when the rig is stopped and I query the database, I only get the data from the past x minutes. This is done in a foreach that builds a new multidimensional array with the data sorted per rigname.
This rises a new problem, when I restart the rig and it starts reporting to the database. The values on the chart continue not at the current timestamp, they continue where the rig has stopped. Normally I would compare the timestamp of the last reported data and the previous reported data with the data of the rig that is still running and return 0 every time a running rig reports, but they report different timestamps ( all within 8 sec of each other)
How can I modify the returned mysql data that I end up with something like this:
array:2 [▼
"home" => array:150 [
0 => array:6 [▼
"id" => 2300
"user_id" => "1"
"rigname" => "home"
"data" => "{"hashrate":12.359,"pool":"eu1.ethermine.org:4444","shares":{"valid":"5","invalid":"0"},"gpus":[{"temperature":"57","fanSpeed":"57","hashrates":12.359}]}"
"created_at" => "2018-02-23 10:44:42"
"updated_at" => "2018-02-23 10:44:42"
]
1 => array:6 [▼
"id" => 2298
"user_id" => "1"
"rigname" => "home"
"data" => "{"hashrate":12.367,"pool":"eu1.ethermine.org:4444","shares":{"valid":"5","invalid":"0"},"gpus":[{"temperature":"57","fanSpeed":"57","hashrates":12.367}]}"
"created_at" => "2018-02-23 10:44:34"
"updated_at" => "2018-02-23 10:44:34"
]
2 => array:6 [▼
"id" => 2296
"user_id" => "1"
"rigname" => "home"
"data" => "{"hashrate":12.367,"pool":"eu1.ethermine.org:4444","shares":{"valid":"5","invalid":"0"},"gpus":[{"temperature":"57","fanSpeed":"57","hashrates":12.367}]}"
"created_at" => "2018-02-23 10:44:26"
"updated_at" => "2018-02-23 10:44:26"
]
...
Server stops, no data, should continue like this
3 => [false] ,
4 => [false] ,
5 => [false]
...
rig reports again to the database
36 => array:6 [▼
"id" => 2296
"user_id" => "1"
"rigname" => "home"
"data" => "{"hashrate":12.367,"pool":"eu1.ethermine.org:4444","shares":{"valid":"5","invalid":"0"},"gpus":[{"temperature":"57","fanSpeed":"57","hashrates":12.367}]}"
"created_at" => "2018-02-23 10:44:26"
"updated_at" => "2018-02-23 10:44:26"
],
],
"server" => array:150 [▼ // This keeps running, or visa versa
0 => array:6 [▼
"id" => 2301
"user_id" => "1"
"rigname" => "server"
"data" => "{"hashrate":76.62,"pool":"eu1.ethermine.org:4444","shares":{"valid":"749","invalid":"2"},"gpus":[{"temperature":"64","fanSpeed":"67","hashrates":19.778},{"tempe ▶"
"created_at" => "2018-02-23 10:44:44"
"updated_at" => "2018-02-23 10:44:44"
]
1 => array:6 [▼
"id" => 2299
"user_id" => "1"
"rigname" => "server"
"data" => "{"hashrate":76.68,"pool":"eu1.ethermine.org:4444","shares":{"valid":"749","invalid":"2"},"gpus":[{"temperature":"64","fanSpeed":"65","hashrates":19.815},{"tempe ▶"
"created_at" => "2018-02-23 10:44:36"
"updated_at" => "2018-02-23 10:44:36"
]
2 => array:6 [▼
"id" => 2297
"user_id" => "1"
"rigname" => "server"
"data" => "{"hashrate":76.644,"pool":"eu1.ethermine.org:4444","shares":{"valid":"749","invalid":"2"},"gpus":[{"temperature":"64","fanSpeed":"65","hashrates":19.793},{"temp ▶"
"created_at" => "2018-02-23 10:44:28"
"updated_at" => "2018-02-23 10:44:28"
]
3 => array:6 [▼
"id" => 2295
"user_id" => "1"
"rigname" => "server"
"data" => "{"hashrate":76.105,"pool":"eu1.ethermine.org:4444","shares":{"valid":"749","invalid":"2"},"gpus":[{"temperature":"64","fanSpeed":"65","hashrates":19.793},{"temp ▶"
"created_at" => "2018-02-23 10:44:20"
"updated_at" => "2018-02-23 10:44:20"
]
4 => array:6 [▼
"id" => 2293
"user_id" => "1"
"rigname" => "server"
"data" => "{"hashrate":76.557,"pool":"eu1.ethermine.org:4444","shares":{"valid":"749","invalid":"2"},"gpus":[{"temperature":"64","fanSpeed":"65","hashrates":19.78},{"tempe ▶"
"created_at" => "2018-02-23 10:44:12"
"updated_at" => "2018-02-23 10:44:12"
]
5 => array:6 [▼
"id" => 2291
"user_id" => "1"
"rigname" => "server"
"data" => "{"hashrate":76.562,"pool":"eu1.ethermine.org:4444","shares":{"valid":"749","invalid":"2"},"gpus":[{"temperature":"64","fanSpeed":"65","hashrates":19.794},{"temp ▶"
"created_at" => "2018-02-23 10:44:04"
"updated_at" => "2018-02-23 10:44:04"
]
]
]
The current code I have written:
public function index()
{
// Get the statistics from the database reported by the miner
// Group them by the rig name
$stats = MinerStatistics::where('user_id', Auth::user()->id)
->where('created_at','>=', Carbon::now()->subMinutes(20))
->orderBy('created_at', 'desc')
->take(300)
->get()
->groupBy('rigname');
// Get the rig names
$rigs = (object) [];
foreach($stats as $key => $value) {
$rigs->$key = $key;
}
// For each rig, get the last known stats between now and 10 seconds ago
$table = (object) [];
foreach($rigs as $key => $value) {
$tableData = $stats[$value]
->where('created_at','>=', Carbon::now()->subSeconds(10));
if (isset($tableData[0]) && $tableData !== []) {
$table->$key = (object) $tableData[0];
$table->$key->data = json_decode($tableData[0]['data'], true);
}
}
return view('rigs', [
'rigs' => $table,
'chart' => $this->chart($stats)
]);
}
public function chart($data)
{
$p = [];
foreach($data as $key => $value) {
$x = [];
$time = [];
foreach($value as $k => $v) {
$time[] = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $v['created_at'])->format('H:i:s');
$x[] = json_decode($v['data'], true);
}
$hashrate = [];
foreach($x as $k => $v){
$hashrate[] = $v['hashrate'];
}
$p[$key] = [ 'time' => $time, 'hashrate' => $hashrate ];
}
$datasets = [];
$n = 0;
foreach($p as $key => $value) {
$colors = [
"0, 188, 212",
"244, 67, 54",
"255, 152, 0"
];
$datasets[] = $this->chartData('Hashrate: ' . $key, array_reverse($value['hashrate']), $colors[$n++]);
}
dd($time);
$chart = app()->chartjs
->name('lineChartTest')
->type('line')
->size(['width' => '100%', 'height' => '20%'])
->labels(array_reverse($time))
->datasets($datasets)
->optionsRaw("{
scales: {
yAxes: [{
display: true,
ticks: {
suggestedMin: 0, // minimum will be 0, unless there is a lower value.
beginAtZero: true // minimum value will be 0.
}
}]
}
}");
return $chart;
}
public function chartData($label, $data, $color)
{
return [
"label" => $label,
'backgroundColor' => "rgba(" . $color . ", 0)",
'borderColor' => "rgba(" . $color . ", 0.7)",
"pointBorderColor" => "rgba(" . $color . ", 0, 0.7)",
"pointBackgroundColor" => "rgba(" . $color . ", 0.7)",
"pointHoverBackgroundColor" => "#fff",
"pointHoverBorderColor" => "rgba(220,220,220,1)",
'data' => $data,
];
}
EDIT: I have found the answer, check below.
I've made an array $timeLabels this array contains timestamps from the current time till x minutes/hours ago in intervals of x minutes.
Then I loop over my rigs and compare the created_at with the values in the timeLabels. If it is in the same range (+/-10 sec) the data from the collection goes into an array with the timeLabel as key. When a timeLabel has no value, it gets 0 assigned as data. This actually works, but needs some tuning.
Full example:
<?php
namespace App\Http\Controllers\Rigs;
use Auth;
use Carbon\Carbon;
use App\MinerStatistics;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class RigController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the rigs dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
// Get the statistics from the database reported by the miner
// Group them by the rig name
$stats = MinerStatistics::where('user_id', Auth::user()->id)
->where('created_at','>=', Carbon::now()->subMinutes(60))
->orderBy('created_at', 'desc')
->take(300)
->get()
->groupBy('rigname');
// Get the rig names
$rigs = (object) [];
foreach($stats as $key => $value) {
$rigs->$key = $key;
}
// For each rig, get the last known stats between now and 10 seconds ago
$table = (object) [];
foreach($rigs as $key => $value) {
$tableData = $stats[$value]
->where('created_at','>=', Carbon::now()->subSeconds(10));
if (isset($tableData[0]) && $tableData !== []) {
$table->$key = (object) $tableData[0];
$table->$key->data = json_decode($tableData[0]['data'], true);
}
}
return view('rigs', [
'rigs' => $table,
'chart' => $this->chart($stats)
]);
}
public function chart($data)
{
$start = Carbon::now('Europe/Brussels');
$end = Carbon::now('Europe/Brussels')->subMinutes(60);
$timeLabels = $this->createTimeRange($end, $start);
$chartData = (object) [];
// Go over each returned rig
foreach($data as $key => $value) {
$arr = array(); // Create an empty array to store our results
// Go over all our timeLabels
foreach(array_reverse($timeLabels) as $t => $time) {
// Create a range to match the incomming created_at and timeLabels
// Needs finetuning!!
$start = Carbon::createFromFormat('H:i', $time)->subSeconds(30)->format('H:i:s');
$end = Carbon::createFromFormat('H:i', $time)->addSeconds(30)->format('H:i:s');
foreach ($value as $k => $v) {
// Transform created_at to the same format as $start and $end
$created = Carbon::createFromFormat('Y-m-d H:i:s', $v['created_at'])->format('H:i:s');
if($created >= $start && $created <= $end) {
if(!isset($v->data['hashrate'])){
$hashrate_ = json_decode($v->data,true)['hashrate'];
} else {
$hashrate_ = $v->data['hashrate'];
}
$arr[$time] = $hashrate_;
}
}
// If the set timeLabel has no value set to 0
// Rig is not running...
if (!isset($arr[$time])) {
$arr[$time] = '0';
}
}
$chartData->$key = $arr;
}
$datasets = [];
$n = 0;
foreach($chartData as $key => $value) {
$data = [];
foreach ($value as $k => $v) {
$data[] = $v;
}
$colors = [
"0, 188, 212",
"244, 67, 54",
"255, 152, 0"
];
$datasets[] = $this->chartData('Hashrate: ' . $key, array_reverse($data), $colors[$n++]);
}
$chart = app()->chartjs
->name('lineChartTest')
->type('line')
->size(['width' => '100%', 'height' => '20%'])
->labels($timeLabels)
->datasets($datasets)
->optionsRaw("{
scales: {
yAxes: [{
display: true,
ticks: {
suggestedMin: 0, // minimum will be 0, unless there is a lower value.
beginAtZero: true // minimum value will be 0.
}
}]
}
}");
return $chart;
}
public function chartData($label, $data, $color)
{
return [
"label" => $label,
'backgroundColor' => "rgba(" . $color . ", 0)",
'borderColor' => "rgba(" . $color . ", 0.7)",
"pointBorderColor" => "rgba(" . $color . ", 0, 0.7)",
"pointBackgroundColor" => "rgba(" . $color . ", 0.7)",
"pointHoverBackgroundColor" => "#fff",
"pointHoverBorderColor" => "rgba(220,220,220,1)",
'data' => $data,
];
}
private function createTimeRange($start, $end, $interval = '5 mins', $format = '24') {
$startTime = strtotime($start);
$endTime = strtotime($end);
$returnTimeFormat = ($format == '12')?'g:i A':'G:i';
$current = time();
$addTime = strtotime('+'.$interval, $current);
$diff = $addTime - $current;
$times = array();
while ($startTime < $endTime) {
$times[] = date($returnTimeFormat, $startTime);
$startTime += $diff;
}
$times[] = date($returnTimeFormat, $startTime);
return $times;
}
}
If I do it like this, I could create a dedicated class so I can reuse this logic. The data I get returned to pass to the charts is now formatted like this: (exactly what I needed)
{#3436 ▼
+"home": array:13 [▼
"17:47" => 13.44
"17:42" => 13.417
"17:37" => 13.439
"17:32" => 13.436
"17:27" => 13.438
"17:22" => "0"
"17:17" => "0"
"17:12" => "0"
"17:07" => "0"
"17:02" => "0"
"16:57" => "0"
"16:52" => "0"
"16:47" => "0"
]
+"server": array:13 [▼
"17:47" => "0"
"17:42" => 76.373
"17:37" => 76.514
"17:32" => 76.565
"17:27" => 76.486
"17:22" => "0"
"17:17" => "0"
"17:12" => "0"
"17:07" => "0"
"17:02" => "0"
"16:57" => "0"
"16:52" => "0"
"16:47" => "0"
]
}
This question already has answers here:
multidimensional array array_sum
(10 answers)
Closed 5 years ago.
I have the following collection that contains the arrays below:
#items: array:2 [▼
0 => array:13 [▼
"label" => "00000000-N/A"
"views" => 980
"clicks" => 444
"form_submissions_perc" => "100.0"
]
1 => array:13 [▼
"label" => "1111-N/A"
"views" => 323
"clicks" => 1009
"form_submissions_perc" => "100.0"
]
]
I am trying to sum clicks and average views but in my try I get the wrong data:
$sum = 0;
foreach ($data as $k => $subArray) {
foreach ($subArray as $id => $value) {
$sum += $subArray['views'];
}
}
dd($sum);
This was my try to sum but didnt get the right results
Desired output
array:2 [
"views"=> 1303,
"clicks"=> 1451
]
Use $sum as array and get correct value of multiarray:
$sum = array(
'clicks' => 0,
'views' => 0
);
foreach ($data as $id => $value) {
$sum['clicks'] += $value['clicks'];
$sum['views'] += $value['views'];
}
dd($sum);
foreach ($data as $k => $subArray) {
$valueSum[] = $subArray['views'];
$clickSum[] = $subArray['clicks'];
}
echo array_sum($valueSum);
echo array_sum($clickSum);
this is also a clean and easy way .
You can use array_reduce instead
$data = array_reduce($array, function ($old, $new) {
return [
"view" => $old["view"] + $new["view"],
"clicks" => $old["clicks"] + $new["clicks"]
];
}, ["view" => 0, "clicks" => 0]);
print '<pre>';
print_r($data);
$data = [
[
"label" => "00000000-N/A",
"views" => 980,
"clicks" => 444,
"form_submissions_perc" => "100.0"
],
[
"label" => "1111-N/A",
"views" => 323,
"clicks" => 1009,
"form_submissions_perc" => "100.0"
]
];
$result['views'] = array_sum( array_column( $data, 'views' ) );
$result['clicks'] = array_sum( array_column( $data, 'clicks' ) );
dd( $result );
This is the easiest way to do your work.
Result:
Array ( [views] => 1303 [clicks] => 1453 )
I have such array:
array:8 [
"text" => "rt_field"
"title" => "rt_field"
"type_id" => "rt_attr_uint"
"author_id" => "rt_attr_uint"
"created_at" => "rt_attr_timestamp"
"recommended_at" => "rt_attr_timestamp"
"deleted_at" => "rt_attr_timestamp"
"feeds" => "rt_attr_multi"
]
I need to get this:
array:10 [
"text" => "rt_attr_string"
"text_txt" => "rt_field"
"title" => "rt_attr_string"
"title_txt" => "rt_field"
"type_id" => "rt_attr_uint"
"author_id" => "rt_attr_uint"
"created_at" => "rt_attr_timestamp"
"recommended_at" => "rt_attr_timestamp"
"deleted_at" => "rt_attr_timestamp"
"feeds" => "rt_attr_multi"
]
I try parse array ($key => $value). When $value == rt_field: I try rename $key to $key.'_txt', and add such key as default (without _txt) with $value = rt_attr_string.
My code:
foreach ($array_param as $key => $value) {
if ($value == 'rt_field'){
$array_param_second[$key] = 'rt_attr_string';
$array_param[$key] = $key.'_txt';
}
}
$result = array_merge($array_param, $array_param_second);
return $result;
But $key in first array doesn't edit.
What I do wrong?
You are editing the value in either array. If you want to update a key, you need to create a new key.
You can just add the keys to a new array, this way there is no need for merging after the foreach.
$result = [];
foreach ($array_param as $key => $value) {
if ($value == 'rt_field'){
$result[$key] = 'rt_attr_string';
$result[$key . '_txt'] = $value;
} else {
$result[$key] = $value;
}
}
Here is your solution....
Your Array
$array[] = array(
"text" => "rt_field",
"title" => "rt_field",
"type_id" => "rt_attr_uint",
"author_id" => "rt_attr_uint",
"created_at" => "rt_attr_timestamp",
"recommended_at" => "rt_attr_timestamp",
"deleted_at" => "rt_attr_timestamp",
"feeds" => "rt_attr_multi"
);
Solution
$new = array();
$keys = array_keys($array[0]);
foreach($array as $r){
for($i=0;$i<count($keys);$i++){
$key = $keys[$i];
if($r[$key]=='rt_field'){
$new[$key.'_txt'] = $r[$key];
$new[$key] = 'rt_attr_string';
}
else $new[$i][$key] = $r[$key];
}
}
echo "<pre>";print_r($new);