Why does my function don't add proper data - php

I'm trying to create an array of data which will be grouped by time (field_time_value).
This is the raw result of SQL query and I operate on this data ($query):
This is the result I'd like to have (desired) - all the closed keys has an empty value:
This is what I have right now (look at the value on [15.15][data][1][data] - it's empty and according to data in $query it should be filled in, as above), all the closed keys has an empty value:
This is the code I'm using:
$days = array(
1 => t('Monday'),
2 => t('Tuesday'),
3 => t('Wednesday'),
4 => t('Thursday'),
5 => t('Friday'),
6 => t('Saturday'),
7 => t('Sunday'),
);
foreach ($query as $key => $value) {
foreach($days as $day_key => $day_name) {
if ($value->field_day_value == $day_key) {
$rows[$value->field_time_value]['data'][$day_key] = array('data' => 'Day: '.$value->field_day_value.' Hour: '.$value->field_time_value);
} else {
$rows[$value->field_time_value]['data'][$day_key] = array('data' => 'empty');
}
}
}
What am I doing wrong?

Don't bother anymore, I get the problem. There should be one more condition which will prevent from overriding data in my array:
foreach ($query as $key => $value) {
foreach($days as $day_key => $day_name) {
if ($value->field_day_value == $day_key) {
$rows[$value->field_time_value]['data'][$day_key] = array('data' => 'Day: '.$value->field_day_value.' Hour: '.$value->field_time_value);
} else {
if (!isset($rows[$value->field_time_value]['data'][$day_key])) {
$rows[$value->field_time_value]['data'][$day_key] = array('data' => 'empty');
}
}
}
}

Related

How to convert and insert string value 9,999.99 to float/double in sql using php

I have arrays of data (import from excel) values in string. One of the data are accounting numbers eg: 9,999.99
my sql data type for that accounting numbers is float.
each time I insert the value.. it only store 9 instead 9,999.99
And I plan to store in format 9999.99 (without the comma)
I cannot change the sql data type to string as it will be use for calculations later on.. so how do i convert and store the data?
appreciate your suggestions.
Thanks
This is my code to insert into DB
// To set HMS daily collection
function set_pbb_data($sheetDataPBB)
{
$OK = 0;
$notOK = 0;
$firstElement = true;
foreach ($sheetDataPBB as $value)
{
if ($firstElement) {
$firstElement = false;
}
else {
$dbEntry = array(
'sett_date' => $value[0],
'trans_date' => $value[1],
'card_no' => $value[2],
'card_type' => $value[3],
'trans_curr' => $value[4],
'trans_amt' => $value[5],
'sett_curr' => $value[6],
'sett_amt' => $value[7],
'gross_cur' => $value[8],
'gross_amt' => $value[9],
'mdr' => $value[10],
'mid' => $value[11],
'approval_code' => $value[12],
'status' => $value[13],
'tid' => $value[14],
'batch_no' => $value[15],
'dba' => $value[16],
'trace_no' => $value[17],
'prod_type' => $value[18]
);
if ($value[0] != null){
$this->db->insert('pbb_cc_tbl', $dbEntry);
if ($this->db->affected_rows() > 0) {
$OK++;
}
else {
echo '<script>alert(" '.$value[0].' Already Exist!");</script>';
$notOK++;
}
}
} //end-if
} //end-foreach
if ($this->db->affected_rows() > 0) {
echo '<script>alert("'.$OK.' PBB Data Added Successfully"); </script>';
}
else {
echo '<script>alert("'.$notOK.' PBB Data Already Exist!"); window.history.back();</script>';
}
}
read each row in excel and insert one by one using sql query
insert into database.table (value) values (cast("999,99" as decimal(9,3)));

PHP - Delete element from multidimensional-array based on value [duplicate]

This question already has answers here:
Delete element from multidimensional-array based on value
(7 answers)
Closed 5 years ago.
foreach ($array_leave_dates as $emp => $leave_type) {
foreach ($leave_type as $leave_dates) {
if($leave_type == 'Maternity Leave'){
unset($array_leave_dates[$leave_type]);
}
else{
echo $leave_dates[$row];
}
}
}
Here we can fetch $leave_dates and want to remove or unset leave_type == 'Maternity Leave'. But could'nt. Please help to point out the mistake in my code above.
Have a look at the // comments
foreach ($array_leave_dates as $emp => $leave_type) {
// you treat $leave_type as array here
foreach ($leave_type as $leave_dates) {
// you treat $leave_type as string here
// doesn't feel right
if($leave_type == 'Maternity Leave') {
// you are unsetting with a value
//unset($array_leave_dates[ --> $leave_type <-- ]);
// i assume you want to delete the key
unset($array_leave_dates[$emp]);
}
else{
// $row doesn't seem to exist, looks wrong from here
echo $leave_dates[$row];
}
}
}
Not sure what your data source looks like:
<?php
// Remove the whole employee
$employees = array(
'emp1' => array('sick' => 'Mon - Tue'),
'emp2' => array('bun in oven' => '2016 - 2017'),
'emp3' => array('broken heart' => '2017 - ∞'),
);
foreach ($employees as $emp => $leave) {
foreach ($leave as $leaveName => $date) {
if($leaveName == 'bun in oven') {
unset($employees[$emp]);
}
}
}
print_r($employees);
// OR remove only 'Maternity' from the employee but keep everything else
<?php
$employees = array(
'emp1' => array('sick' => 'Mon - Tue', 'its friday and im not coming in' => 'Fri'),
'emp2' => array('bun in oven' => '2016 - 2017', 'sick' => 'Thu'),
'emp3' => array('broken heart' => '2017 - ∞'),
);
foreach ($employees as $emp => $leave) {
foreach ($leave as $leaveName => $date) {
if($leaveName == 'bun in oven') {
unset($employees[$emp][$leaveName]);
}
}
}
print_r($employees);
If $leave_type can be "Maternity Leave", then why do you search for $leave_dates inside that string value? The question is naturally rethorical. From the very fact that you have two foreach cycles and the second is embedded into the first makes me think that $leave_type is not what you think it is. So, I think you have a multi dimensional array, where the outer keys are employee names or ids and the inner keys are the types. Example:
array(
'John Doe' => array('Sickness' => array('a', 'b', 'c')),
'Mary Doe' => array('Sickness' => array('a', 'b', 'c'), 'Maternal Leave' => array('d', 'e'))
)
If that is the case, then you need to modify your cycles:
foreach ($array_leave_dates as $emp => $leave) {
if ($leave['Maternity Leave']) {
unset($array_leave_dates[$emp]['Maternity Leave']);
}
}
If you want a more general solution, then this might help you:
function removeByKey(&$arr, $k) {
if (arr[$k] !== null) {
unset arr[$k];
}
foreach($arr as $key => $value) {
if (is_array($value)) {
$arr[$key] = removeByKey($arr[$key], $k);
}
}
return $arr;
}

How to append value in array with keys without getting overriding

i'm getting result in a foreach loop now i want to form a new array with keys and form that array with my new data. Now when i try to assign data it gets override to the previous one as it's not getting new index. how can i achive that so far i have done that:
foreach ($result as $key) {
$pickup_location = $key->locationid;
if (isset($pickup_location)) {
$pickup_location = $this->db->get_where('locations', array('id' => $pickup_location ))->row();
if (!empty($pickup_location)) {
$supplier_dashboard['pickup_location_name'] = $pickup_location->name_en;
}
}
$dropoff_location = $key->location_dropoff;
if (isset($dropoff_location)) {
$dropoff_location = $this->db->get_where('locations', array('id' => $dropoff_location ))->row();
if (!empty($dropoff_location)) {
$supplier_dashboard['dropoff_location_name'] = $dropoff_location->name_en;
}
}
$car_make = $key->car_id;
if (isset($car_make)) {
$car_details = $this->db->get_where('chauffeur_rates', array('chauffeur_id' => $car_make))->row();
if (!empty($car_details)) {
$supplier_dashboard['car_make'] = $car_details->chauffeur_make;
}
}
}
return $supplier_dashboard;
}
and my resulting array is:
Array
(
[pickup_location_name] => Seoul Downtown
[dropoff_location_name] => Disneyland Paris
[car_make] => makecar
)
however i have atleast 7 location names and car makes instead of getting added as a new array it overrides the previous one, i should have get the result as
Array
[0](
[pickup_location_name] => Seoul Downtown
[dropoff_location_name] => Disneyland Paris
[car_make] => makecar
)
Array
[1](
[pickup_location_name] => Seoul 1
[dropoff_location_name] => Disneyland 1
[car_make] => makecar
)
Array
[2](
[pickup_location_name] => Seoul 2
[dropoff_location_name] => Disneyland 2
[car_make] => makecar
)
... upto 7
Every time you looping the key of the array is always the same that's why you are getting overide the results. You need to put a key that is unique. You can try this
$i = 0; //First key
foreach ($result as $key){
$supplier_dashboard[$i]['pickup_location_name'] = $pickup_location->name_en;
$i++; //add +1 to the key so the next element not overrides
}
You have to add $key to array in which your record get added
use
$supplier_dashboard[$key]['pickup_location_name'] = //your code
instead of
$supplier_dashboard['pickup_location_name'] = //your code
Instead of adding the items the your array, you are directly setting the value in the top most collection.
Instad of
$myobject['foo'] = $value;
You need to do (where $iteration is the current position in your loop)
$myobject[$iteration]['foo'] = $value;
Try the following :
$carItemNumber = 0;
foreach ($result as $key) {
$pickup_location = $key->locationid;
if (isset($pickup_location)) {
$pickup_location = $this->db->get_where('locations', array('id' => $pickup_location ))->row();
if (!empty($pickup_location)) {
$supplier_dashboard[$carItemNumber]['pickup_location_name'] = $pickup_location->name_en;
}
}
$dropoff_location = $key->location_dropoff;
if (isset($dropoff_location)) {
$dropoff_location = $this->db->get_where('locations', array('id' => $dropoff_location ))->row();
if (!empty($dropoff_location)) {
$supplier_dashboard[$carItemNumber]['dropoff_location_name'] = $dropoff_location->name_en;
}
}
$car_make = $key->car_id;
if (isset($car_make)) {
$car_details = $this->db->get_where('chauffeur_rates', array('chauffeur_id' => $car_make))->row();
if (!empty($car_details)) {
$supplier_dashboard[$carItemNumber]['car_make'] = $car_details->chauffeur_make;
}
}
$carItemNumber++;
}
return $supplier_dashboard;

Check data in array for null values

I want to check data in array to see if there are null values. If there are, I'd like to display an alert.
Example:
$data = array(1 => 'AKB48', 2 => '', 3 => 'JKT48');
The Array of index 1 ($data[1]) is null, and I want it to display "WARNING, data in array is null"
if data in array does not have empty/null values then don't show an alert:
$data = array(1 => 'AKB48', 2 => 'HKT48', 3 => 'JKT48');
(the above array will no trigger an alert)
How can I achieve this solution?
something like this?
$data = array(1 => 'AKB48', 2 => '', 3 => 'JKT48');
foreach($data as $val) {
if($val == '') {
echo "alert, array consist of empty value";
}
}
$data = array(1 => 'AKB48', 2 => '', 3 => 'JKT48');
foreach($data as $v)
{
if(empty($v))
{
echo "Array contains null value";
break;
}
}
Something like this?
isDefined will check if the value is a valid non-empty String.
function isDefined($var) {
return isset($var) && !is_null($var) && !empty($var);
}
$data = array(
array('AKB48', 'HKT48', NULL),
array('AKB49', '', 'JKT49'),
array('AKB50', 'HKT50', 'JKT50')
);
for ($i = 0; $i < count($data); $i++) {
foreach ($data[$i] as $col) {
if (!isDefined($col)) {
echo "<<<Attention: Array #$i contains an empty value!>>> ";
}
}
}
Running example of code above.

Php Array key=> value searching

HI I am fairly new to php.
I have an array
$arr = array(0 => array('GID'=>1,'groupname'=>"cat1",'members'=>array(0=>array('mid'=>11,'mname'=>'wwww'),1=>array('mid'=>12,'mname'=>'wswww'))),
1 => array('GID'=>2,'groupname'=>"cat2",'members'=>array(0=>array('mid'=>13,'mname'=>'gggwwww'),1=>array('mid'=>14,'mname'=>'wvvwww'))),
2 => array('GID'=>3,'groupname'=>"cat1",'members'=>array(0=>array('mid'=>15,'mname'=>'wwddsww')),1=>array('mid'=>16,'mname'=>'wwwdddw')));
ie...,I have GID,groupname,mid(member id),mname(member name).I want to insert a new mid and mname into a group if it is already in the array ,if it is not exists then create a new subarray with these elements..I also need to check a member id(mid) is also present.........................I used the code but its not working fine............. if (!empty($evntGroup)) {
foreach ($evntGroup as $k => $group) {
if ($group['GID'] == $group_id) {
foreach($group as $j=> $mem){
if($mem['mid'] == $mem_id){
unset($evntGroup[$k]['members'][$j]['mid']);
unset($evntGroup[$k]['members'][$j]['mname']);
}
else{
$evntGroup[$k]['members'][] = array(
'mid' => $mem_id,
'mname' => $mem_name);
}}
} else {
$evntGroup[] = array(
'GID' => $group_id,
'groupname' => $Group['event_group_name'],
'members' => array(
0 => array(
'mid' => $mem_id,
'mname' => $mem_name
)
)
);
}
}
} else {
$evntGroup[$i]['GID'] = $group_id;
$evntGroup[$i]['groupname'] = $Group['event_group_name'];
$evntGroup[$i]['members'][] = array(
'mid' => $mem_id,
'mname' => $mem_name);
$i++;
}
In the form of a function, the easiest solution will look something like this:
function isGidInArray($arr, $val) {
foreach($arr as $cur) {
if($cur['GID'] == $val)
return true;
}
return false;
}
You've updated your question to specify what you want to do if the specified GID is found, but that's just a trivial addition to the loop:
function doSomethingIfGidInArray($arr, $val) {
foreach($arr as $cur) {
if($cur['GID'] == $val) {
doSomething();
break; //Assuming you only expect one instance of the passed value - stop searching after it's found
}
}
}
There is unfortunately no native PHP array function that will retrieve the same index of every array within a parent array. I've often wanted such a thing.
Something like this will match if GID equals 3:
foreach( $arr as $item ) {
if( $item['GID'] == 3 ) {
// matches
}
}
There is the code
function updateByGid(&$array,$gid,$groupname,$mid,$mname) {
//For each element of the array
foreach ($array as $ii => $elem) {
//If GID has the same value
if ($elem['GID'] == $gid) {
//Insert new member
$array[$ii]['members'][]=array(
'mid'=>$mid,
'mname'=>$mname);
//Found!
return 0;
}
}
//If not found, create new
$array[]=array(
'GID'=>$gid,
'groupname'=>$groupname,
'members'=>array(
0=>array(
'mid'=>$mid,
'mname'=>$mname
)
)
);
return 0;
}

Categories