array(10) {
["table_name"]=>
string(0) ""
["chart_type"]=>
string(5) "Table"
["chart_name"]=>
string(9) "Veterans "
["dashboard_name"]=>
string(7) "Default"
["data_option"]=>
string(0) ""
["limit_value"]=>
string(1) "0"
["view_status"]=>
bool(true)
["view_priority"]=>
string(0) ""
["chart_data"]=>
array(72) {
[0]=>
array(11) {
[0]=>
string(4) "Rank"
[1]=>
string(6) "Branch"
[2]=>
string(12) "Level 3 Name"
[3]=>
string(12) "Level 4 Name"
[4]=>
string(23) "Logged Premia Score MTD"
[5]=>
string(26) "Converted Premia Score MTD"
[6]=>
string(21) "Persistancy Score MTD"
[7]=>
string(23) "Logged Premia Score YTD"
[8]=>
string(26) "Converted Premia Score YTD"
[9]=>
string(21) "Persistancy Score YTD"
[10]=>
string(5) "Score"
}
[1]=>
array(11) {
[0]=>
float(1)
[1]=>
string(22) " GWALIOR DIRECTSegment"
[2]=>
string(29) "G00563 - GOVIND SINGH KUSHWAH"
[3]=>
string(21) "S03643 - Shishir Jain"
[4]=>
float(1)
[5]=>
float(1)
[6]=>
float(1)
[7]=>
float(0.9994)
[8]=>
float(1)
[9]=>
float(1)
[10]=>
float(9.89901)
}
[2]=>
array(11) {
[0]=>
float(2)
[1]=>
string(26) " KURUKSHETRA DIRECTSegment"
[2]=>
string(23) "A02311 - Amarjeet Singh"
[3]=>
string(21) "S00927 - Sachin Kumar"
[4]=>
float(1)
[5]=>
float(1)
[6]=>
float(1)
[7]=>
float(0.953)
[8]=>
float(1)
[9]=>
float(1)
[10]=>
float(9.82245)
}
}
["color_scheme_name"]=>
string(7) "Default"
}
How do i display the chart_data in php
$arr=json_decode($row[0],TRUE);
var_dump($arr);
foreach($arr as $row)
{
foreach($row['chart_data'] as $k)
{
echo $k['Rank'];
echo $k['Branch'];
}
}
EDIT:
for ($x=0; $x<=count
($arr['chart_data']); $x++) {
foreach($arr['chart_data'][$x] as $key=>$val)
{
echo $val;
}
}
You are having 2 foreach calls.
The first one passes all keys of the outer array, amongst which chart_data.
The second one looks at the children of the outer array, and looks for chart_data in each child. But is obviously not finding any.
Change your code to this:
$arr=json_decode($row[0],TRUE);
foreach($arr['chart_data'] as $k){
echo $k['Rank'];
echo $k['Branch'];
}
edit:
I see. (Thanks for indenting your array) Your problem is off course that $arr['rank']and $arr['branch'] don't exist. They are mere strings inside $k[0] and $k[1] and themselves don't contain any other data.
What would you have like to see outputted?
Maybe this is solution.
$arr = json_decode($row[0],TRUE);
foreach($row['chart_data'] as $chart_data)
{
foreach($chart_data[0] as $k)
{
echo $k['Rank'];
echo $k['Branch'];
}
}
Update :
Rank, Branch is values. Your array is bad format.
$arr = json_decode($row[0],TRUE);
foreach($row['chart_data'] as $chart_data)
{
foreach($chart_data[0] as $k)
{
echo $k[0];
echo $k[1];
}
}
Related
This question already has answers here:
How can i list has same id data with while loop in PHP? [closed]
(1 answer)
php get unique sub array [duplicate]
(1 answer)
Closed 4 years ago.
been using most of the day to solve my problem - and i am about to give up.
I have an array looking like this:
array(2) {
[1]=>
array(5) {
[0]=>
array(6) {
["name"]=>
string(15) "Henning smaall"
["drivernumber"]=>
string(4) "8830"
["period_start"]=>
string(10) "2018-07-20"
["period_end"]=>
string(10) "2018-07-31"
["status"]=>
string(1) "0"
["note"]=>
string(9) "Operation"
}
[1]=>
array(6) {
["name"]=>
string(15) "Henning smaall"
["drivernumber"]=>
string(4) "8830"
["period_start"]=>
string(10) "2018-07-17"
["period_end"]=>
string(10) "2018-07-17"
["status"]=>
string(1) "0"
["note"]=>
string(0) ""
}
[2]=>
array(6) {
["name"]=>
string(15) "Henning smaall"
["drivernumber"]=>
string(4) "8830"
["period_start"]=>
string(10) "2018-07-16"
["period_end"]=>
string(10) "2018-07-16"
["status"]=>
string(1) "1"
["note"]=>
string(0) ""
}
[3]=>
array(6) {
["name"]=>
string(15) "Henning smaall"
["drivernumber"]=>
string(4) "8830"
["period_start"]=>
string(10) "2018-07-27"
["period_end"]=>
string(10) "2018-07-27"
["status"]=>
string(1) "0"
["note"]=>
string(0) ""
}
[4]=>
array(6) {
["name"]=>
string(15) "Henning smaall"
["drivernumber"]=>
string(4) "8830"
["period_start"]=>
string(10) "2018-07-31"
["period_end"]=>
string(10) "2018-07-31"
["status"]=>
string(1) "1"
["note"]=>
string(0) ""
}
}
[13]=>
array(2) {
[0]=>
array(6) {
["name"]=>
string(15) "Henrik Hjersing"
["drivernumber"]=>
string(4) "8850"
["period_start"]=>
string(10) "2018-07-10"
["period_end"]=>
string(10) "2018-07-24"
["status"]=>
string(1) "0"
["note"]=>
string(0) ""
}
[1]=>
array(6) {
["name"]=>
string(15) "Henrik Hjersing"
["drivernumber"]=>
string(4) "8850"
["period_start"]=>
string(10) "2018-07-18"
["period_end"]=>
string(10) "2018-08-01"
["status"]=>
string(1) "0"
["note"]=>
string(11) "asdasdasdad"
}
}
}
The output i am looking for should be something like
Henning smaall (8830)
2018-07-20 - 2018-07-31
2018-07-17 - 2018-07-17
2018-07-16 - 2018-07-16
2018-07-27 - 2018-07-27
2018-07-31 - 2018-07-31
Henrik Hjersing (8850)
2018-07-10 - 2018-07-24
2018-07-18 - 2018-08-01
For now i am using dummy data and easy php testcode - but i keep getting the name reccuring in the output.
Database function:
public function resultsetGroup(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
}
SQL function:
public function getVacations(){
//$this->db->query('SELECT * FROM vacation ORDER BY user_id, status, period_start');
$this->db->query('SELECT u.id, u.name, u.drivernumber, v.period_start, v.period_end, v.status, v.note FROM users u, vacation v where u.id = v.user_id');
$results = $this->db->resultsetGroup();
return $results;
}
This is the php code i've been trying to alter in 500 different ways, and still i cannot get the result i am after - if i do not pdo group by u.id but u.name - it works, but then again, 2 people can have the same name - and that will break my intention.
<?php var_dump($data['vacations']); ?>
<?php foreach($data['vacations'] as $key => $vac) : ?>
<?php echo $key; ?>
<?php foreach($vac as $va) : ?>
<?php echo key($vac);?>
<h1><?php echo $va['name'] ?></h1>
<?php endforeach ; ?>
<?php endforeach ; ?>
Can you pls help me out? i feel like im running around in cirkles.
enter code here
Try this:
$vacations = array();
foreach($data['vacations'] as $key => $value){
foreach($value as $index => $person){
$vacations[$person['drivernumber']] = array();
$vacations[$person['drivernumber']]['name'] = $person['name'];
$vacations[$person['drivernumber']]['drivenumber'] = $person['drivenumber'];
$vacations[$person['drivernumber']]['vacations'] = "";
}
}// With this, you will have the array $vacations with the drivernumbers as keys without duplicates and initialized the name, drivenumber and vacations string.
foreach($data['vacations'] as $key => $value){
foreach($value as $index => $person){
$vacations[$person[drivenumber]]['vacations'] += $person['period_start'] . "-" . $person['period_end'] . " ";
}
}//This will concatenate all the period_start and period_end of each person in their respective position in the array.
foreach($vacations as $key => $value){
echo $value['name'] . " " . $value['drivernumber'] . " " . $value['vacations'];
}//Finally.. print in the format that you are requesting.
Please try.
I have written below lines of code
public function shiftarray($cursor, $arg)
{
$keyarguments = array("first_name","roll_no");
$arrayStudents =array();
foreach ($cursor as $k => $row)
{
foreach ($keyarguments as $key)
{
if (strcasecmp($row[$key], $arg) == 0)
{
array_unshift($arrayStudents, $row);
}
else
{
}
}
}
return $arrayStudents;
}
I have array of students in $cursor like
{ [0]=> array(50) { ["_id"]=> object(MongoId)#23 (1) { ["$id"]=> string(24) "58131c7799fbad4c1d000202" } ["student_id"]=> float(2) ["registration_temp_perm_no"]=> string(1) "1" ["roll_no"]=> float(1) ["admission_date"]=> string(10) "01/07/2016" ["first_name"]=> string(7) "Neil" ["middle_name"]=> string(4) "David" ["last_name"]=> string(6) "Stephan" ["dob"]=> string(10) "12/03/1981" ["gender"]=> string(6) "Female" ["blood_group"]=> string(2) "A+" ["birth_place"]=> string(11) "Sadar Bazar" ["nationality"]=> string(6) "Indian" ["language"]=> string(7) "English" ["religion"]=> string(8) "Agnostic" ["address_line1"]=> string(20) "4148 Hazelcrest Hill" ["address_line2"]=> string(20) "22883 Memorial Place" ["city"]=> string(11) "Sadar Bazar" ["state"]=> string(13) "Uttar Pradesh" ["pincode"]=> string(6) "190010" ["country"]=> string(5) "India" ["phone1"]=> string(10) "9039180419" ["phone2"]=> string(10) "7681559402" ["email"]=> string(24) "educianstudent#gmail.com" ["is_sms_enabled"]=> string(3) "Yes" ["is_active"]=> int(1) ["has_finished"]=> int(0) ["student_category"]=> string(1) "5" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["Biometric_ID"]=> string(1) "1" ["siblings"]=> string(14) "Cynthia Taylor" ["guardian_name"]=> string(14) "Cynthia Taylor" ["guardian_occupation"]=> string(13) "Senior Editor" ["guardian_qualification"]=> string(20) "Research Assistant I" ["guardian_email_id"]=> string(23) "educianparent#gmail.com" ["gaurdain_contact_details"]=> string(10) "9419513603" ["guardian_relationship"]=> string(6) "Father" ["height"]=> string(3) "4.9" ["weight"]=> string(4) "34.9" ["allergies"]=> string(0) "" ["batch"]=> int(2) ["academicyear"]=> string(4) "2015" ["batchhistory"]=> array(1) { [0]=> array(5) { ["batchid"]=> float(2) ["academic_year"]=> string(4) "2015" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["sequenceno"]=> int(1) ["courseId"]=> object(MongoId)#24 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } } } ["uploads"]=> array(1) { ["profile_pic"]=> string(39) "58131c7799fbad4c1d000202schoolgirl2.jpg" } ["created_at"]=> NULL ["updated_at"]=> string(0) "" ["routearray"]=> array(2) { [0]=> array(5) { ["routeid"]=> int(2) ["academicyear"]=> string(4) "2016" ["current"]=> int(0) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "09/28/2016" } [1]=> array(5) { ["routeid"]=> int(3) ["academicyear"]=> string(4) "2016" ["current"]=> int(1) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "11/17/2016" } } ["HostelAlloted"]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#25 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#26 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } ["HostelAllotmentHistory"]=> array(1) { [0]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#27 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#28 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } } ["courseId"]=> object(MongoId)#29 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } }
...
...
I am trying to bring those student on the beginning of array whose first name is brian and rest of students should at the bottom.
Now the above code place the students on the top of the array but the rest of the students are ommited/removed. I don't want them to be removed.
I am trying to tweak what code to write in else condition.
Please help!!!
Fill $arrayStudents before the loop otherwise only matches will be collected and unshifted.
Extend example of php.net/unshift
$add = array('big');
$queue = array(
array("orange"),
array("banana"),
array("apple1"),
array("raspberry")
);
array_unshift($queue, $add);
print_r($queue);
In your case: search for keys you want to unshift, the rest goes to the standard $list = array(/list of studends/):
$list = array();
$bringToTop = array('brian', 'elvis');
foreach($data as $item) {
if (/* in in the list to bring on top*/) {
$memory[] = $item;
} else {
$list[] = $item;
}
}
foreach($memory as $item) {
array_unshift($list, $item);
}
print_r($list);
Kind regards
Add $arrayStudents[] = $row; to your else scope. Here is a simple demo to illustrate it.
When matches add the element to the result from the start of array, unmatches add them from the end of the array.
I want to loop through the following array.
array(2) {
[0]=> array(6) {
[0]=> string(10) "excel_id "
[1]=> string(12) "excel_name "
[2]=> string(11) "excel_email"
[3]=> string(13) "excel_address"
[4]=> string(12) "excel_giveby"
[5]=> string(16) "excel_accesories"
}
[1]=> array(6) {
[0]=> float(111)
[1]=> string(10) "sh1exname1"
[2]=> string(11) "sh1exemail1"
[3]=> string(12) "sh1exaddress"
[4]=> string(12) "sh1exgivenby"
[5]=> string(16) "sh1exaccesorries"
}
}
can anyone help me?
If you tried anything at all you should have found foreach.
this will loop through all of your array values.
You can simply loop through a multi-dimensional array by using a multi-dimensional foreach like this:
foreach($array as $parentValue)
{
foreach($parentValue as $childValue)
{
// your code
}
}
try this:
foreach ($main_array as $outer_entry){
foreach($outer_entry as $entity){
print_r($entity);
}
}
i have two csv files: one contracts data and the other containing awarded contracts. I need to combine these two files using common field(contractname) and compute total amount of current awarded contracts.
Link to csv files:
https://github.com/younginnovations/problem-statements/tree/master/clean-up-contracts-data
I do get desired output bt undefined offset error comes up.
the code:unset(awards_info[$y][0]); does unsets the data but 'Undefined offset: 0' error occurs..
similarly the code $list_data[10]!=NULL also shows the same error
but both are producing desired output.
var_dump($awards_info):: produces following:
array(5) { [0]=> array(6) { [0]=> string(12) "contractName" [1]=> string(12) "contractDate" [2]=> string(14) "completionDate" [3]=> string(7) "awardee" [4]=> string(15) "awardeeLocation" [5]=> string(6) "Amount" } [1]=> array(6) { [0]=> string(15) "Contract-2070-3" [1]=> string(8) "5/9/2014" [2]=> string(9) "8/25/2014" [3]=> string(11) "SK Builders" [4]=> string(5) "Banke" [5]=> string(6) "200000" } [2]=> array(6) { [0]=> string(15) "Contract-2070-5" [1]=> string(9) "3/18/2014" [2]=> string(8) "4/8/2014" [3]=> string(24) "S engineering industries" [4]=> string(9) "Makwanpur" [5]=> string(6) "300000" } [3]=> array(6) { [0]=> string(15) "Contract-2070-9" [1]=> string(8) "3/6/2014" [2]=> string(8) "4/6/2014" [3]=> string(24) "Gourishankar nirman sewa" [4]=> string(8) "Lalitpur" [5]=> string(6) "400000" } [4]=> array(6) { [0]=> string(16) "Contract-2070-10" [1]=> string(8) "2/6/2014" [2]=> string(9) "6/16/2014" [3]=> string(11) "SK Builders" [4]=> string(5) "Banke" [5]=> string(6) "400000" } }
$file_contract=fopen('contracts.csv','r');
$file_awards=fopen('awards.csv','r');
while(($data=fgetcsv($file_contract))!=FALSE){
$contracts_info[]=$data;
}
while(($data=fgetcsv($file_awards))!=FALSE){
$awards_info[]=$data;
}
$con_count=count($contracts_info);
for($x=0;$x<$con_count;$x++){
if($x==0){
unset($awards_info[0][0]);
$data[$x]=array_merge($contracts_info[0],$awards_info[0]);
}
else{
$check=0;
$award_count=count($awards_info);
for($y=1;$y<$award_count;$y++){
if($awards_info[$y][0]==$contracts_info[$x][0]){
unset($awards_info[$y][0]);
$data[$x]=array_merge($contracts_info[$x],$awards_info[$y]);
$check=1;
}
}
if($check==0){
$data[$x]=$contracts_info[$x];
}
}
}
$final_data=fopen('final.csv','w');
foreach($data as $list_data){
fputcsv($final_data,$list_data);
}
fclose($final_data);
$c=0;
foreach ($data as $list_data){
if(($list_data[1]=='Current') && ($list_data[10]!=NULL))
{
$c+=$list_data[12];
}
}
echo $c;
You can just add the content file in the new file, without parsing files :
$fp = fopen('final.csv','w');
fwrite($fp, file_get_contents('contracts.csv'));
fwrite($fp, file_get_contents('awards.csv'));
fclose($fp);
echo file_get_contents('final.csv');
ok it's a wrong way. So i try to rewrite the loop :
for($x=0; $x < count($contracts_info); $x++) {
$data[$x] = $contracts_info[$x];
if($x == 0) {
if(isset($awards_info[$x])) {
$data[$x] = array_merge($data[$x], $awards_info[$x]);
unset($awards_info[$x]);
}
} else {
foreach($awards_info as $y => $award_info_datas) {
if($award_info_datas[0] == $contracts_info[$x][0]) {
$data[$x] = array_merge($data[$x], $award_info_datas);
unset($awards_info[$y]);
break;
}
}
}
}
i have the following array:
[2]=>
object(stdClass)#9 (4) {
["ID"]=>
string(32) "43c845f895a56fbe8aea9435ef8fa806"
["Type"]=>
string(8) "Campaign"
["Name"]=>
string(28) "An unmissable invitation for"
["Actions"]=>
array(5) {
[0]=>
object(stdClass)#10 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-05-07 17:00:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(0) ""
}
[1]=>
object(stdClass)#11 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-05-07 09:01:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(0) ""
}
[2]=>
object(stdClass)#12 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-04-30 22:29:00"
["IPAddress"]=>
string(14) "94.171.192.216"
["Detail"]=>
string(0) ""
}
[3]=>
object(stdClass)#13 (4) {
["Event"]=>
string(5) "Click"
["Date"]=>
string(19) "2013-04-30 17:43:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(60) "http://www.rbh.co.uk/rbhevent/?name=[fullname]&email=[email]"
}
[4]=>
object(stdClass)#14 (4) {
["Event"]=>
string(4) "Open"
["Date"]=>
string(19) "2013-04-30 17:43:00"
["IPAddress"]=>
string(12) "109.239.93.2"
["Detail"]=>
string(0) ""
}
}
}
i am trying to count the events that are the same. So for example ["Event"] = Open =4 / ["Event"] = Click =1.
I am trying to achieve this via counting a foreach loop:
$i=0;
foreach($entry->Actions as $actions ) {
echo $i++;
}
Im not quite sure how to approach this? Can someone suggest a best practice?
$counts = array();
foreach($entry->Actions as $actions) {
if(!isset($counts[$actions->Event])) {
$counts[$actions->Event] = 0;
}
++$counts[$actions->Event];
}
print_r($counts);
<?php
$amounts = array(); // Events as key
foreach($entry->Actions as $actions)
{
if (isset($amounts[$actions["Event"]])) $amounts[$actions["Event"]]++;
else $amounts[$actions["Event"]] = 1;
}
print_r($amounts);
echo "<br>".$amounts["Open"];
?>