php array flip value as key and make it simple - php

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);
}

Related

PHP Manipulate associative array

I'm using PHP in doing our exercise which is to get the necessary details of a user and the quiz he/she took.
This is my code so far.
<?php
require("dbOption/Db.class.php");
$db = new Db();
$data = array();
$quiz = array();
$easy = "";
$normal = "";
$hard = "";
// SELECT id, course FROM quizz WHERE course = 2
$res_quizz = $db->query("SELECT id, course FROM quizz WHERE course = :course", array("course"=>2));
foreach ($res_quizz as $key => $value) {
$quizzid = $value['id'];
// SELECT easy, normal, hard, userid FROM quizzscore WHERE quizid = $quizid
$res_quizzscore = $db->query("SELECT easy, normal, hard, userid, quizzid FROM quizzscore WHERE quizzid = :quizzid", array("quizzid"=>$quizzid));
foreach ($res_quizzscore as $key => $value2) {
$easy = $value2['easy'];
$normal = $value2['normal'];
$hard = $value2['hard'];
$quizzid = $value2['quizzid'];
$userid = $value2['userid'];
$q = array(
'Qz'.$quizzid => array(
'easy' =>$easy,
'normal' =>$normal,
'hard' =>$hard,
)
);
$quiz['quizzes'] = $q;
// SELECT name FROM USER WHERE id = $userid
$res_user = $db->query("SELECT name FROM user WHERE id = :id", array("id"=>$userid));
foreach ($res_user as $key => $value3) {
$name = $value3['name'];
}
}
$data[$userid] = array();
$data[$userid]['name'] = $name;
$data[$userid]['quizzes'] = $quiz;
echo "<pre>";
print_r($data);
echo "<pre/>";
}
The array is poorly manipulated and that's why I'm getting this result.
Array
(
[1] => Array
(
[name] => John Smith
[quizzes] => Array
(
[quizzes] => Array
(
[Qz1] => Array
(
[easy] => 5
[normal] => 6
[hard] => 7
)
)
)
)
)
Array
(
[1] => Array
(
[name] => John Smith
[quizzes] => Array
(
[quizzes] => Array
(
[Qz2] => Array
(
[easy] => 3
[normal] => 4
[hard] => 5
)
)
)
)
)
What I want to have is this kind of result.
Array
(
[1] => Array
(
[name] => John Smith
[quizzes] => Array
(
[quizzes] => Array
(
[Qz1] => Array
(
[easy] => 5
[normal] => 6
[hard] => 7
)
[Qz2] => Array
(
[easy] => 3
[normal] => 4
[hard] => 5
)
)
)
)
)
Any ideas would be most appreciated.

Adding same element to every level of array php

I need to add another another element to each level of the array (sorry, think that is bad terminology).
I have an array -
Array ( [0] => Array (
[actor_rt_id] => 162683283,
[item_number] => 3 )
[1] => Array (
[actor_rt_id] => 162657351,
[item_number] => 5 )
)
This code produces the array. The commented out line is what I tried to add to the array. The code before the comment creates the array.
$data_itemone['actor_rt_id'] = $this->input->post('actor_id');
$data_itemtwo['item_number'] = $this->input->post('item_number');
$data_item = array_merge($data_itemone, $data_itemtwo);
$res = [];
foreach($data_item as $key => $value){
foreach ($value as $data => $thevalue) {
$res[$data][$key] = $thevalue;
//$res['film_id'] = $film_id;
}
}
I have an another variable I need to add from post which is a single string.
$film_id = $this->input->post('film_id');
I need it to be in the array like so -
Array ( [0] => Array (
[actor_rt_id] => 162683283,
[item_number] => 3,
[film_id] => 52352
)
[1] => Array (
[actor_rt_id] => 162657351,
[item_number] => 5,
[film_id] => 52352
)
)
...but my code (uncommented) produces -
Array ( [0] => Array (
[actor_rt_id] => 162683283,
[item_number] => 3
)
[film_id] => 16639,
[1] => Array
( [actor_rt_id] => 162657351,
[item_number] => 5 )
)
Tried a few things. Can't seem to get it to work.
Change
$res['film_id'] = $film_id;
to
$res[$data]['film_id'] = $film_id;
this will add it to the right array.
How if you try this.
$data_itemone['actor_rt_id'] = [123, 245];
$data_itemtwo['item_number'] = [456, 789];
$film_id = 52352;
$data_item = array_merge($data_itemone, $data_itemtwo);
$res = [];
foreach($data_item as $key => $value){
foreach ($value as $data => $thevalue) {
$res[$data][$key] = $thevalue;
$res[$data]['film_id'] = $film_id;
}
}
print_r($res);
Try this one
<?
$data_itemone['actor_rt_id'] = $this->input->post('actor_id');
$data_itemtwo['item_number'] = $this->input->post('item_number');
$film_id = $this->input->post('film_id');
$data_item = array_merge($data_itemone, $data_itemtwo);
$res = [];
foreach($data_item as $key => $value){
foreach ($value as $data => $thevalue) {
$res[$data][$key] = $thevalue;
$res[$data]['film_id'] = $film_id;
}
}
?>

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);
?>

Morris.js chart with PHP array

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'] );
?>

How do i get the final array?

I have a array here:
$array = array(
array('t'=>'t1','v'=>'001'),
array('t'=>'t2','v'=>'002'),
array('t'=>'t3','v'=>'003'),
array('t'=>'t1','v'=>'004'),
array('t'=>'t4','v'=>'005'),
array('t'=>'t2','v'=>'006'),
array('t'=>'t5','v'=>'007'),
array('t'=>'t3','v'=>'008'),
);
The final array i want is this :
array(
't1' => array('v'=>array(001,004)),
't2' => array('v'=>002),
't3' => array('v'=>array(003,008)),
't4' => array('v'=>005),
't5' => array('v'=>006),
't6' => array('v'=>007)
)
Is there any way i can achieve the final array using php array manipulation functions? I don't want to use any loops (for or foreach). Tried doing using usort() but getting no where
Here is my usort code that calls user defined function:
public function sort($a,$b)
{
$const = array();
$temp1 = array();
$temp2 = array();
//echo $a['t'].":".$a['v'] . " - " . $b['t'].":".$b['v']. "<br/>";
if($a['t'] == $b['t']){
$temp1[$a['t']] = array($a['v'],$b['v']);
//$const = $temp1;
}else{
if(!array_key_exists($a['t'],$temp1) && !array_key_exists($b['t'],$temp1)){
$temp2[$a['t']] = array($a['v']);
$temp2[$b['t']] = array($b['v']);
}
}
$result = array_merge($temp1, $temp2);
print_r($result);
}
$array = array(
array('t'=>'t1','v'=>'001'),
array('t'=>'t2','v'=>'002'),
array('t'=>'t3','v'=>'003'),
array('t'=>'t1','v'=>'004'),
array('t'=>'t4','v'=>'005'),
array('t'=>'t2','v'=>'006'),
array('t'=>'t5','v'=>'007'),
array('t'=>'t3','v'=>'008'),
);
$res = array();
foreach($array as $val){
$res[$val['t']]['v'][] = $val['v'];
}
echo "<pre>";
print_r($res);
Output :
Array
(
[t1] => Array
(
[v] => Array
(
[0] => 001
[1] => 004
)
)
[t2] => Array
(
[v] => Array
(
[0] => 002
[1] => 006
)
)
[t3] => Array
(
[v] => Array
(
[0] => 003
[1] => 008
)
)
[t4] => Array
(
[v] => Array
(
[0] => 005
)
)
[t5] => Array
(
[v] => Array
(
[0] => 007
)
)
)
<?php
$array = array(
array('t'=>'t1','v'=>'001'),
array('t'=>'t2','v'=>'002'),
array('t'=>'t3','v'=>'003'),
array('t'=>'t1','v'=>'004'),
array('t'=>'t4','v'=>'005'),
array('t'=>'t2','v'=>'006'),
array('t'=>'t5','v'=>'007'),
array('t'=>'t3','v'=>'008'),
);
foreach ($array as $key => $row) {
$t[$key] = $row['t'];
$v[$key] = $row['v'];
}
array_multisort($v, SORT_ASC, $array);
$reqArray = array();
foreach($array as $value)
{
$reqArray[$value['t']]['v'][] = $value['v'];
}
print_r($reqArray);
?>
Try with:
$input = array( /* your data */ );
$output = array();
foreach ( $input as $value ) {
$t = $value['t'];
$v = $value['v'];
if ( !isset($output[$t]) ) {
$output[$t] = array('v' => $v);
} else if ( is_array($output[$t]['v']) ) {
$output[$t]['v'][] = $v;
} else {
$output[$t]['v'] = array($output[$t]['v'], $v);
}
}

Categories