Trying to create a JSON array for Morris.js donut chart, but cant think of any way to get correct format. Any tips?
My controller:
$user = User::find((Auth::user()->id));
$budget = $user->budget()->get();
$labels = ['rent', 'heating', 'utilities', 'internet_tv', 'phone', 'food', 'sweets', 'alcohol_cigs', 'insurance' , 'loans', 'finance_other', 'cosmetics'
, 'medicine', 'clothes_shoes', 'accessories', 'electronics', 'school', 'entertainment', 'food_out', 'holidays', 'books', 'pets', 'gifts', 'car', 'other'];
$data2 = [];
foreach ($labels as $label)
{
$data2['label'][] = $label;
$data2['value'][] = $budget->sum($label);
}
$data2 = json_encode($data2);
What I am getting:
'{"label":["rent","heating","utilities","internet_tv" ...],"value":[435,30,0,0 ...]}'
I want to get:
'[{"label":"rent","value":"435"},{"label":"heating","value":"30"},{"label":"utilities","value":"0"},{"label":"internet_tv","value":"0"} ...]'
Your code is creating two subarrays to the $data2 array, one label and one value. Then, data is pushed to these two arrays over and over again.
Instead, you want to create a new array and push that one onto the $data2 array, like this:
$data2 = [];
foreach ($labels as $label)
{
$data2[] = [
'label' => $label,
'value' => $budget->sum($label)
];
}
Related
hi im using this php code
$data = new $models([
'number' => $row[0],
'name' => $row[1],
]);
like this its working fine but what i want is that i dont know the keys 'number' and 'name' what if i want them come from array like this ..
$array = ['name','number','anything'];
$data = new $models([
foreach($array as $key => $arr)
{
$arr => $row[$key];
}
]);
how can i do something like this ..
calling foreach inside creating new class ..
thanks ..
<?php
class Foo{}
$className='Foo';
$array = ['name','number','anything'];
$row = ['name_v', 'number_v', 'anything_v'];
$foo = new $className();
foreach(array_combine($array, $row) as $k => $v){
$foo->$k = $v;
}
print(json_encode($foo));
So long as they have the same number of elements in the order that you want, just combine them:
$array = ['name', 'number', 'anything'];
$data = new $models(array_combine($array, $row));
You can do it in one line:
$data = new $models(array_combine(['name', 'number', 'anything'], $row));
I'm struggling with array_merge to make an array of items. The code which I have:
$items = [];
foreach ($products as $product) {
Log::info($product->orderproduct->idorder_product);
$items = array_merge($items, [
'id' => $product->orderproduct->idorder_product
]);
}
Log::info(print_r($items, true));
The output is:
6
7
['id' => 7]
How can I create an array with both id's?
Not sure what result you expect, so there are 2 options:
foreach ($products as $product) {
Log::info($product->orderproduct->idorder_product);
// First
$items[] = $product->orderproduct->idorder_product;
// Second
$items[] = ['id' => $product->orderproduct->idorder_product];
}
Array merge is just another array which add into the bottom of the array.
I think you are misleading us on the result you want to get.
$items = array(); / $items = [];
you can push the data into array easily by this code
$items[] = array(
'id' => $product->orderproduct->idorder_product,
)
I have a PHP array like this:
$arr = array(
'id' => 'app.settings.value.id',
'title' => 'app.settings.value.title',
'user' => 'app.settings.value.user'
);
I want to remove '.id', '.title' and '.user' in the array values. I want to insert the key of this array at the end of the value. I know, that I can get an array key by passing an array and a value to 'array_search', but I want the key within this one array definition - at the end of it's value. Is this possible?
I don't like this, but I guess it's simple and works:
$arr = array(
'id' => 'app.settings.value',
'title' => 'app.settings.value',
'user' => 'app.settings.value'
);
$result = array();
foreach ($arr AS $key => $value) {
$result[$key] = $value . '.' . $key;
}
You're storing app.settings.value which is the same for all array items, so personally I'd prefer:
$arr = array(
'id',
'title',
'user'
);
function prepend_key($key)
{
return 'app.settings.value.' . $key;
}
$result = array_map('prepend_key', $arr);
With $result containing:
array(
'app.settings.value.id',
'app.settings.value.title',
'app.settings.value.user'
);
At the end of the day, either works :)
I would like to add all array elements to another array as elements
Here is my codes
<pre>
$resultBasedBuild = array();
$data = {0.225, 0.132, 0.114};
$index = 0;
foreach ($data as $singleData) {
$resultBasedData[] = array(
'name' => 'my name'
,'data' => array(array($index, $singleData))
);
}
$result = json_encode($resultBasedData);
</pre>
The expected output would be
[{"name":"20140722.1304","data":[[0,0.225],[0,0.132],[0,0.114]]}]
Thank you for your help.
I sorted this problem using one more array.
$data = [0.225, 0.132, 0.114];
$result = json_encode(['name' => 'my name', 'data' => array_map(function ($item)
{
return [0, $item];
}, $data)]);
This seems like a simple challenge, but I'm struggling.
I want to retrieve records using a join query on two database tables and represent them as an array of arrays, whereby each of the elements in the root array is a parent record and each nested element represents a child record.
The SQL query is working fine, and it returns a set of rows in which the channel_key column is a grouping column.
Here's my attempt at populating the array structure from the rows:
$rows = $db->get_results($query);
$key = '';
$programmes = array();
foreach ($rows as $row) {
$programme = array(
'title' => $row->title,
'start' => $row->start,
'duration' => $row->duration
);
$programmes[] = $programme;
if ($key != $row->channel_key) {
$channels[] = array(
'key' => $row->channel_key,
'programme' => $programmes
);
$key = $row->channel_key;
$programmes = array();
}
}
Unfortunately this only populates the root level arrays (the ones that correspond to the parent records).
Any suggestions please?
Thanks,
Tim
You only have one programme in the $programmes array when you assign it to the channel.
An alternative would be to build the channel array with the channel_key.
e.g.
<?php
$channels = array();
foreach($rows as $row) {
// Create the channel node if it doesn't exist
if (!isset($channels[$row->channel_key])) {
$channels[$row->channel_key] = array(
'key' => $row->channel_key,
'programme' => array()
);
}
$programme = array(
'title' => $row->title,
'start' => $row->start,
'duration' => $row->duration
);
// Add to the existing channel node.
$channels[$row->channel_key]['programme'][] = $programme;
}
Simple solution can be.
$rows = $db->get_results($query);
$key = '';
$programmes = array();
foreach ($rows as $row) {
$programme = array(
'title' => $row->title,
'start' => $row->start,
'duration' => $row->duration
);
$programmes[$row->channel_key][] = $programme;
}