Morris.js chart with PHP array - php

I need your help for this case.
I have an array in PHP.
How can I apply this array:
$visits = $ga->query($params);
Witch gave me something like this:
Array
(
[http_code] => 200
[kind] => analytics#gaData
[rows] => Array
(
[0] => Array
(
[0] => 20141223
[1] => 26
)
[1] => Array
(
[0] => 20141224
[1] => 15
)
...
In this code :
<? function getVisits() {
$morris = new MorrisLineCharts('getVisits');
$morris->xkey = array('date');
$morris->ykeys = array('value');
$morris->labels = array('Money');
$morris->data = array(
array('date' => '20141223', 'value' => 26),
array('date' => '20141224', 'value' => 15),
);
echo $morris->toJavascript();
}
getVisits();
?>
Thanks a lot.

You could loop over the data returned by Google Analytics, to construct an array suitable for Morris.
<? function getVisits( $ga_rows = array() ) {
foreach( $ga_rows as &$_row ) {
$_row = array('date' => $_row [0], 'value' => $_row [1]);
}
$morris = new MorrisLineCharts('getVisits');
$morris->xkey = array('date');
$morris->ykeys = array('value');
$morris->labels = array('Money');
$morris->data = $ga_rows;
echo $morris->toJavascript();
}
// the relevant data from the array you retreived from Google Analytics
getVisits( $google_analytics_data['rows'] );
?>

Related

Append a new array to an existing array

I am using Codeigniter Sessions to build a "Betslip".
I am adding the team name and odds to the bet slip and then plan to loop through each "bet" element to create a betslip.
The ideal array needs to look like :
[betslip] => Array
(
[bet] => Array
(
[team] => Rayo Vallecano
[odds] => 67/100
)
[bet] => Array
(
[team] => Elche
[odds] => 1/100
)
)
However in my code, I just seem to be overwriting what is already there.
My current PHP code is as follows :
// Get Team Name
$teamname = $this->uri->segment(3);
// Get Odds
$odds1 = $this->uri->segment(4);
$odds2 = $this->uri->segment(5);
$odds = $odds1;
$odds .= "/";
$odds .= $odds2;
// Build An array titled Bet
$bet = array(
'bet' => array(
'team' => urldecode($teamname),
'odds' => $odds
)
);
$betslip = $this->session->userdata('betslip');
// Create The Betslip For The First Time...
if(empty($betslip))
{
$this->session->set_userdata('betslip', $bet);
}
else
{
// Add To The Betslip Array...
$betslip['bet'] = array(
'team' => urldecode($teamname),
'odds' => $odds
);
$this->session->set_userdata('betslip', $betslip);
}
How do I just append a bet to the existing bet slip array?
Is it possible to have multiple array keys with the same name too?
Thanks in advance..
This is not an ideal array
[betslip] => Array
(
[bet] => Array
(
[team] => Rayo Vallecano
[odds] => 67/100
)
[bet] => Array
(
[team] => Elche
[odds] => 1/100
)
)
it should be like
[betslip] => Array
(
[0] => Array
(
[team] => Rayo Vallecano
[odds] => 67/100
)
[1] => Array
(
[team] => Elche
[odds] => 1/100
)
)
<?php
$item = array();
$item2 = array(
'team' => 1,
'odds' => "1/100"
);
for ($x = 0; $x <= 10; $x++) {
$item[] = $item2;
}
print_r($item);
?>

php array flip value as key and make it simple

i have been troubling to format array correctly
i have this code:
require('simple_html_dom.php');
$table = array();
$html = file_get_html('apc.html');
foreach($html->find('tr') as $row) {
$rack = ltrim($row->find('td',0)->plaintext);
$location = ltrim($row->find('td',1)->plaintext);
$usage = ltrim($row->find('td',2)->plaintext);
$auk = ltrim($row->find('td',3)->plaintext);
$cost = ltrim($row->find('td',4)->plaintext);
$rack = rtrim($rack);
$location = rtrim($location);
$usage = rtrim($usage);
$auk = rtrim($auk);
$cost = rtrim($cost);
$table[$rack][$usage][$auk][$cost] = true;
}
echo '<pre>';
print_r($table);
echo '</pre>';
using simple_html_dom above i can convert html table to an array as follow:
[Rack01] => Array
(
[741,60] => Array
(
[1.409,04] => Array
(
[267,72] => 1
)
)
[110,88] => Array
(
[210,67] => Array
(
[40,03] => 1
)
)
)
[Rack 09] => Array
(
[843,84] => Array
(
[1.603,30] => Array
(
[304,63] => 1
)
)
)
I would like to have result as below:
array(
[0] => array (
[usage] => 'Rack 01',
[usage] => '741,60',
[auk] => '1.409.04',
[cost] => '267,72')
[1] => array (
[usage] => 'Rack 02',
[usage] => 'value???',
[auk] => 'value???',
[cost] => 'value???')
any help would be apreaciate
Something like this. Also note that trim will do both left and right:
foreach($html->find('tr') as $row) {
$rack = trim($row->find('td',0)->plaintext);
$location = trim($row->find('td',1)->plaintext);
$usage = trim($row->find('td',2)->plaintext);
$auk = trim($row->find('td',3)->plaintext);
$cost = trim($row->find('td',4)->plaintext);
$table[] = array('rack' => $rack,
'usage' => $usage,
'auk' => $auk,
'cost' => $cost);
}

PHP Counting inside an Array

I want to create a list where if its already in the array to add to the value +1.
Current Output
[1] => Array
(
[source] => 397
[value] => 1
)
[2] => Array
(
[source] => 397
[value] => 1
)
[3] => Array
(
[source] => 1314
[value] => 1
)
What I want to Achieve
[1] => Array
(
[source] => 397
[value] => 2
)
[2] => Array
(
[source] => 1314
[value] => 1
)
My current dulled down PHP
foreach ($submissions as $timefix) {
//Start countng
$data = array(
'source' => $timefix['parent']['id'],
'value' => '1'
);
$dataJson[] = $data;
}
print_r($dataJson);
Simply use an associated array:
$dataJson = array();
foreach ($submissions as $timefix) {
$id = $timefix['parent']['id'];
if (!isset($dataJson[$id])) {
$dataJson[$id] = array('source' => $id, 'value' => 1);
} else {
$dataJson[$id]['value']++;
}
}
$dataJson = array_values($dataJson); // reset the keys - you don't nessesarily need this
This is not exactly your desired output, as the array keys are not preserved, but if it suits you, you could use the item ID as the array key. This would simplify your code to the point of not needing to loop through the already available results:
foreach ($submissions as $timefix) {
$id = $timefix['parent']['id'];
if (array_key_exists($id, $dataJson)) {
$dataJson[$id]["value"]++;
} else {
$dataJson[$id] = [
"source" => $id,
"value" => 1
];
}
}
print_r($dataJson);
You should simplify this for yourself. Something like:
<?
$res = Array();
foreach ($original as $item) {
if (!isset($res[$item['source']])) $res[$item['source']] = $item['value'];
else $res[$item['source']] += $item['value'];
}
?>
After this, you will have array $res which will be something like:
Array(
[397] => 2,
[1314] => 1
)
Then, if you really need the format specified, you can use something like:
<?
$final = Array();
foreach ($res as $source=>$value) $final[] = Array(
'source' => $source,
'value' => $value
);
?>
This code will do the counting and produce a $new array as described in your example.
$data = array(
array('source' => 397, 'value' => 1),
array('source' => 397, 'value' => 1),
array('source' => 1314, 'value' => 1),
);
$new = array();
foreach ($data as $item)
{
$source = $item['source'];
if (isset($new[$source]))
$new[$source]['value'] += $item['value'];
else
$new[$source] = $item;
}
$new = array_values($new);
PHP has a function called array_count_values for that. May be you can use it
Example:
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
Output:
Array
(
[1] => 2
[hello] => 2
[world] => 1
)

Formatting an Array in PHP

I'm pulling an array from the database and it looks like so:
Array
(
[0] => Array
(
[tracker_id] => 28
[tracking_numbers] => hdkshwuy47937892hd
)
[1] => Array
(
[tracker_id] => 28
[tracking_numbers] => 797825464411
)
)
I need to reformat it to look like this:
Array
(
[0] => Array
(
[tracker_id] => 28
[tracking_numbers] => Array
(
[0] => hdkshwuy47937892hd
[1] => 797825464411
)
)
)
I can't seem find the right search in the array or keys to create an array of numbers for the single tracker id.
Use array_column() for < php V5.5
<?php
$a=array
( array
('tracker_id' => 28,
'tracking_numbers'=> "hdkshwuy47937892hd"
),
array('tracker_id' => 28,
'tracking_numbers' => "797825464411",
) );
$a[0]['tracking_numbers']=array_column($a,"tracking_numbers");
unset($a[1]);
print_r($a);
Demo
try this
$arr_output = array();
foreach($arr_input as $arr)
{
$tracker_id = $arr['tracker_id'];
$tracking_numbers = $arr['tracking_numbers'];
$arr_output[$traker_id][] = $tracking_numbers;
}
print_r($arr_output);
UPDATE 2:
$arr_output = array();
$arr_output1 = array();
foreach($arr_input as $arr)
{
$tracker_id = $arr['tracker_id'];
$tracking_numbers = $arr['tracking_numbers'];
$arr_output[$traker_id][] = $tracking_numbers;
}
$i=0;
foreach($arr_output as $key=>$value)
{
$arr_output1[$i]['tracker_id']=$key
$arr_output1[$i]['tracking_numbers']=$value
$i+=1;
}
print_r($arr_output1);

PHP/MySQL: Recreate multidimensional array from existing array

I've got an multi-array (currently with objects) that I want to reorder based on a specific key/value.
Array
(
[0] => stdClass Object
(
[task_id] => 1
[task_title] => Title
[users_username] => John
)
[1] => stdClass Object
(
[task_id] => 2
[task_title] => Title
[users_username] => John
)
[2] => stdClass Object
(
[task_id] => 3
[task_title] => Title
[users_username] => Mike
)
)
I'd like to reorder it to get multi-arrays by user_name, so I can cycle through the task by username.
Array
(
[John] => Array
(
[0] => Array
(
[task_id] => 1
[title] => Title
)
[1] => Array
(
[task_id] => 2
[title] => Title
)
)
[Mike] => Array
(
[0] => Array
(
[task_id] => 3
[title] => Title
)
)
)
Is it possible to recreate my array to an array like that above?
Updated version of the code
<?php
$it0 = (object) array('task_id' => 1,'task_title' => 'Title','users_username' => 'John');
$it1 = (object) array('task_id' => 2,'task_title' => 'Title','users_username' => 'John');
$it2 = (object) array('task_id' => 3,'task_title' => 'Title','users_username' => 'Mike');
$array = array($it0,$it1,$it2);
$return = array();
foreach($array as $id => $value){
$return[$value->users_username][] = array('task_id' => $value->task_id,'title' => $value->task_title);
}
var_dump($return);
Yes, it is possible.
You'll have to loop through your current array and create a new array to do it.
example:
$new_array = array();
foreach ($array as $row)
{
$new_row = array(
'task_id' => $row->task_id,
'title' => $row->task_title,
);
$name = $row->users_username;
if (isset($new_array[$name]))
{
$new_array[$name][] = $new_row;
}
else
{
$new_array[$name] = array($new_row);
}
}
Now $new_array contains the new array exactly like the one you're asking for.
Then you can sort it with
ksort($new_array);
There may be another way to do this, with some built-in function, but sometimes I'd rather just do it myself, and know how it is working, without having to look up the documentation.
The approach:
Iterate through all of the first array, looking at [users_username] and putting them into a new array.
Code:
$dst_array = array();
foreach ($src_array as $val)
{
$user = $val->users_username;
// TODO: This check may be unnecessary. Have to test to find out.
// If this username doesn't already have an array in the destination...
if (!array_key_exists($user, $dst_array))
$dst_array[$user] = array(); // Create a new array for that username
// Now add a new task_id and title entry in that username's array
$dst_array[$user][] = array(
'task_id' => $val->task_id
'title' => $val->title
);
}
Just something like this (maybe not 100% PHP code):
foreach ( $obj : $oldArray ) {
$newTask['task_id'] = $obj->task_id;
$newTask['title'] = $obj->title;
$newArray[$oldName][] = $newTask;
}
If you want to order it; you can just call a order function afterwards.

Categories