I want remove array if element having empty or count '0'.I used PHPEXCEL in laravel.
My Code:
$results=Excel::load('assets/uploads/studentdata/file.xlsx', function($reader) {
})->get()->toArray();
I got output is following like,
Array ( [0] => Array ( [grade] => 12 [schoolentrancedate] => Carbon\Carbon Object ( [date] => 2014-09-02 00:00:00
[timezone_type] => 3 [timezone] => UTC ) [schoolexitdate] => Carbon\Carbon Object ( [date] => 2015-06-25 00:00:00
[timezone_type] => 3 [timezone] => UTC ) [studentfirstname] => Gutierrez [studentmiddlename] => [studentlastname] => Larry
[age] => [gender] => M [dateofbirth] => Carbon\Carbon Object ( [date] => 1997-06-16 00:00:00 [timezone_type] => 3 [timezone]
=> UTC ) [language] => [parentfullname] => Irma Perlera [phone] => 617-778-3913 [mobile] => 617-778-3913 [parentemail] =>
info1234#gmail.com [emergencypersonfullname] => Irma Perlera [emergencypersonphone] => 617-778-3913 [emergencypersonmobile] =>
617-778-3913 [house] => 237 [apartment] => [street] => Ferry [city] => Everett [state] => MA [zipcode] => [weekdayfrom] =>
[weekdayto] => [intime] => [outtime] => [mondayintime] => Carbon\Carbon Object ( [date] => 2015-03-16 08:10:00 [timezone_type]
=> 3 [timezone] => UTC ) [mondayouttime] => Carbon\Carbon Object ( [date] => 2015-03-16 14:30:00 [timezone_type] => 3
[timezone] => UTC ) [tuesdayintime] => Carbon\Carbon Object ( [date] => 2015-03-16 08:10:00 [timezone_type] => 3 [timezone] =>
UTC ) [tuesdayouttime] => Carbon\Carbon Object ( [date] => 2015-03-16 14:30:00 [timezone_type] => 3 [timezone] => UTC )
[wednesdayintime] => Carbon\Carbon Object ( [date] => 2015-03-16 08:10:00 [timezone_type] => 3 [timezone] => UTC )
[wednesdayouttime] => Carbon\Carbon Object ( [date] => 2015-03-16 13:30:00 [timezone_type] => 3 [timezone] => UTC )
[thursdayintime] => Carbon\Carbon Object ( [date] => 2015-03-16 08:10:00 [timezone_type] => 3 [timezone] => UTC )
[thursdayouttime] => Carbon\Carbon Object ( [date] => 2015-03-16 14:30:00 [timezone_type] => 3 [timezone] => UTC )
[fridayintime] => Carbon\Carbon Object ( [date] => 2015-03-16 08:10:00 [timezone_type] => 3 [timezone] => UTC )
[fridayouttime] => Carbon\Carbon Object ( [date] => 2015-03-16 14:30:00 [timezone_type] => 3 [timezone] => UTC ) [note] =>
[oneway] => [fromhome] => [fromschool] => [twoway] => [43] => )
[1] => Array ( [grade] => [schoolentrancedate] => [schoolexitdate] => [studentfirstname] => [studentmiddlename] =>
[studentlastname] => [age] => [gender] => [dateofbirth] => [language] => [parentfullname] => [phone] => [mobile] =>
[parentemail] => [emergencypersonfullname] => [emergencypersonphone] => [emergencypersonmobile] => [house] => [apartment] =>
[street] => [city] => [state] => [zipcode] => [weekdayfrom] => [weekdayto] => [intime] => [outtime] => [mondayintime] =>
[mondayouttime] => [tuesdayintime] => [tuesdayouttime] => [wednesdayintime] => [wednesdayouttime] => [thursdayintime] =>
[thursdayouttime] => [fridayintime] => [fridayouttime] => [note] => [oneway] => [fromhome] => [fromschool] => [twoway] => [43]
=> )
[2] => Array ( [grade] => [schoolentrancedate] => [schoolexitdate] => [studentfirstname] => [studentmiddlename] =>
[studentlastname] => [age] => [gender] => [dateofbirth] => [language] => [parentfullname] => [phone] => [mobile] =>
[parentemail] => [emergencypersonfullname] => [emergencypersonphone] => [emergencypersonmobile] => [house] => [apartment] =>
[street] => [city] => [state] => [zipcode] => [weekdayfrom] => [weekdayto] => [intime] => [outtime] => [mondayintime] =>
[mondayouttime] => [tuesdayintime] => [tuesdayouttime] => [wednesdayintime] => [wednesdayouttime] => [thursdayintime] =>
[thursdayouttime] => [fridayintime] => [fridayouttime] => [note] => [oneway] => [fromhome] => [fromschool] => [twoway] => [43]
=> ) )
I need to remove above array if array[1] ,array[2].. is empty or count '0' means its should be remove or if any other solution to avoid to read empty rows using phpexcel in laravel.
Try this..
$data="your array";
for($i=0;$i<count($data);$i++)
{
$countstring=count(array_filter($data[$i]));
if($countstring==0)
{
unset($data[$i]); // remove empty array
} else {
$finaldata[] =$data[$i]; //get non empty array
}
}
print_r($finaldata);
Related
How would I go about removing duplicate items from my multidimensional object array?
In my sample below, I have 2 items which contain the id value of 4 (fullname is 'Jodie (Y6Y5)').
I need to modify my array so that there are no duplicate id values.
I've tried doing loops to fix it, and I've also tried array_unique().
Does anyone have any better ideas on how I can achieve this?
Example input array:
Array
(
[0] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 4
[pupil_id] => 1
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Jodie (Y6Y5)
[email] => jodie#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] => 07777777777
[username] => jerrys
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 14:23:02
[updated] => 2018-02-14 14:18:08
[login] => 2018-02-13 15:45:09
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 86
)
[1] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 3
[pupil_id] => 5
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Karla (Rec Y5)
[email] => karla#email.co.uk
[email_2] => ally#email.com
[telephone] =>
[telephone_2] =>
[username] => rickygutpa
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 10:33:30
[updated] => 2018-02-14 14:16:21
[login] => 0000-00-00 00:00:00
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 86
)
[2] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 1
[pupil_id] => 4
[pupil_id_1] => 0
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Florence (Y6 2)
[email] => florence#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] =>
[username] => mrslacey
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-01 09:47:34
[updated] => 2018-02-14 14:49:32
[login] => 2018-02-05 11:48:54
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 87
)
[3] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 4
[pupil_id] => 1
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Jodie (Y6Y5)
[email] => jodie#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] => 07777777777
[username] => jerrys
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 14:23:02
[updated] => 2018-02-14 14:18:08
[login] => 2018-02-13 15:45:09
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 87
)
)
Expect Result:
Array
(
[0] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 4
[pupil_id] => 1
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Jodie (Y6Y5)
[email] => jodie#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] => 07777777777
[username] => jerrys
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 14:23:02
[updated] => 2018-02-14 14:18:08
[login] => 2018-02-13 15:45:09
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 86
)
[1] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 3
[pupil_id] => 5
[pupil_id_1] => 2
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Karla (Rec Y5)
[email] => karla#email.co.uk
[email_2] => ally#email.com
[telephone] =>
[telephone_2] =>
[username] => rickygutpa
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-06 10:33:30
[updated] => 2018-02-14 14:16:21
[login] => 0000-00-00 00:00:00
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 86
)
[2] => guardian Object
(
[guardians] => Array
(
)
[errors] => Array
(
)
[id] => 1
[pupil_id] => 4
[pupil_id_1] => 0
[pupil_id_2] => 0
[pupil_id_3] => 0
[pupil_id_4] => 0
[school_id] => 1
[title] =>
[firstname] =>
[surname] =>
[fullname] => Florence (Y6 2)
[email] => florence#email.co.uk
[email_2] =>
[telephone] => 0777777777777
[telephone_2] =>
[username] => mrslacey
[password] => password
[active] => 1
[deleted] => 0
[inserted] => 2018-02-01 09:47:34
[updated] => 2018-02-14 14:49:32
[login] => 2018-02-05 11:48:54
[last_login] => 0000-00-00 00:00:00
[email_update_app] => 0
[email_update_app_date] => 0000-00-00 00:00:00
[email_update_web] => 0
[email_update_web_date] => 0000-00-00 00:00:00
[is_wonde] => 0
[wonde_id] =>
[wonde_mis_id] =>
[wonde_upi] =>
[grade_id] => 87
)
)
Best practice (and a technique that you will find implemented in Stackoverflow questions every day) is to assign temporary keys, overwrite any pre-existing subarrays based on the temporary keys, then remove the temporary keys at the end.
This avoids having to do iterated lookups. For highest efficiency in your codes, try to minimize iterated function calls.
Method #1: Remove earlier duplicate occurrences / Retain last occurrences (Demo)
foreach($guardian->guardians as $subarray){
$result[$subarray->id]=$subarray; // assign temporary keys
}
$guardian->guardians=array_values($result); // re-declare and remove temporary keys (re-index the subarrays)
var_export($guardian);
*Alternatively, here is a functional-style one-liner providing the same effect: (Demo)
$guardian->guardians=array_values(array_column((array)$guardian->guardians,NULL,'id'));
var_export($guardian);
For those who don't know, array_column() chokes on objects, so objects must be temporarily converted to arrays.
Method #2: Remove later duplicate occurrences / Retain first occurrences (Demo)
foreach($guardian->guardians as $subarray){
if(!isset($result[$subarray->id])){
$result[$subarray->id]=$subarray; // only store if first occurrence of id
}
}
$guardian->guardians=array_values($result); // re-index
var_export($guardian);
I've managed to get a working answer by doing as following:
$tempGuardians = array();
foreach($guardian->guardians as $key => $value){
if (in_array($value->id, $tempGuardians)){
unset($guardian->guardians[$key]);
} else {
array_push($tempGuardians, $value->id);
}
}
unset($tempGuardians);
If anyone knows any cleaner ways or better ways of doing this, I'm open to new ways.
The answer is included on your question, you already said it: array_unique:
$newArray=array_unique($a1);
How would I go about getting the balance from this result?
Braintree_Customer Object
(
[_attributes:protected] => Array
(
[id] => 425865
[merchantId] => abcddeff
[firstName] => Waka
[lastName] => Flaka
[company] => Waka Flaka Co.
[email] => demoemail#gmail.com
[phone] => 281.330.8004
[fax] => 419.555.1235
[website] => http://example.com
[createdAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[updatedAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[customFields] =>
[creditCards] => Array
(
[0] => Braintree_CreditCard Object
(
[_attributes] => Array
(
[bin] => 400551
[expirationMonth] => 09
[expirationYear] => 2020
[last4] => 0004
[billingAddress] => Braintree_Address Object
(
[_attributes] => Array
(
[id] => gx
[customerId] => 30007705
[firstName] => Flaka
[lastName] => Waka
[company] =>
[streetAddress] =>
[extendedAddress] =>
[locality] =>
[region] =>
[postalCode] =>
[countryCodeAlpha2] =>
[countryCodeAlpha3] =>
[countryCodeNumeric] =>
[countryName] =>
[createdAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[updatedAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
)
)
[cardType] => Visa
[cardholderName] => Flaka Waka
[commercial] => Unknown
[countryOfIssuance] =>
[createdAt] => DateTime Object
(
[date] => 2015-11-17 17:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[customerId] => 30007705
[customerLocation] => US
[debit] => Yes
[default] => 1
[durbinRegulated] => Yes
[expired] =>
[healthcare] => Unknown
[imageUrl] => https://assets.braintreegateway.com/payment_method_logo/visa.png?environment=sandbox
[issuingBank] => Unknown
[payroll] => Unknown
[prepaid] => No
[subscriptions] => Array
(
[0] => Braintree_Subscription Object
(
[_attributes] => Array
(
[addOns] => Array
(
)
[balance] => 0.00
My code to receive the cardType is
$cardtype = $customer->creditCards[0]->cardType;
You can try this
$balance = $customer->creditCards[0]->subscriptions[0]->balance;
from the response of braintree. access attribute array from transaction object
which way to access it?
Braintree\Transaction Object (
[_attributes:protected] => Array (
[id] => 3ytqkd
[status] => authorized
[type] => sale
[currencyIsoCode] => USD
[amount] => 100.00
[merchantAccountId] => arvaan
[orderId] =>
[createdAt] => DateTime Object
(
[date] => 2015-12-01 07:15:40
[timezone_type] => 3
[timezone] => UTC
)
[updatedAt] => DateTime Object
(
[date] => 2015-12-01 07:15:40
[timezone_type] => 3
[timezone] => UTC
)
[customer] => Array
(
[id] =>
[firstName] =>
[lastName] =>
[company] =>
[email] =>
[website] =>
[phone] =>
[fax] =>
)
[billing] => Array
(
[id] =>
[firstName] =>
[lastName] =>
[company] =>
[streetAddress] =>
[extendedAddress] =>
[locality] =>
[region] =>
[postalCode] =>
[countryName] =>
[countryCodeAlpha2] =>
[countryCodeAlpha3] =>
[countryCodeNumeric] =>
)
[refundId] =>
[refundIds] => Array
(
)
[refundedTransactionId] =>
[partialSettlementTransactionIds] => Array
(
)
[authorizedTransactionId] =>
[settlementBatchId] =>
[shipping] => Array
(
[id] =>
[firstName] =>
[lastName] =>
[company] =>
[streetAddress] =>
[extendedAddress] =>
[locality] =>
[region] =>
[postalCode] =>
[countryName] =>
[countryCodeAlpha2] =>
[countryCodeAlpha3] =>
[countryCodeNumeric] =>
)
[customFields] =>
[avsErrorResponseCode] =>
[avsPostalCodeResponseCode] => I
[avsStreetAddressResponseCode] => I
[cvvResponseCode] => M
[gatewayRejectionReason] =>
[processorAuthorizationCode] => DHYKZ3
[processorResponseCode] => 1000
[processorResponseText] => Approved
[additionalProcessorResponse] =>
[voiceReferralNumber] =>
[purchaseOrderNumber] =>
[taxAmount] =>
[taxExempt] =>
[creditCard] => Array
(
[token] =>
[bin] => 555555
[last4] => 4444
[cardType] => MasterCard
[expirationMonth] => 12
[expirationYear] => 2017
[customerLocation] => US
[cardholderName] =>
[imageUrl] => https://assets.braintreegateway.com/payment_method_logo/mastercard.png?environment=sandbox
[prepaid] => Unknown
[healthcare] => Unknown
[debit] => Unknown
[durbinRegulated] => Unknown
[commercial] => Unknown
[payroll] => Unknown
[issuingBank] => Unknown
[countryOfIssuance] => Unknown
[productId] => Unknown
[uniqueNumberIdentifier] =>
[venmoSdk] =>
)
[statusHistory] => Array
(
[0] => Braintree\Transaction\StatusDetails Object
(
[_attributes:protected] => Array
(
[timestamp] => DateTime Object
(
[date] => 2015-12-01 07:15:40
[timezone_type] => 3
[timezone] => UTC
)
[status] => authorized
[amount] => 100.00
[user] => jitendra.arvaan
[transactionSource] => api
)
)
)
[planId] =>
[subscriptionId] =>
[subscription] => Array
(
[billingPeriodEndDate] =>
[billingPeriodStartDate] =>
)
[addOns] => Array
(
)
[discounts] => Array
(
)
[descriptor] => Braintree\Descriptor Object
(
[_attributes:protected] => Array
(
[name] =>
[phone] =>
[url] =>
)
)
[recurring] =>
[channel] =>
[serviceFeeAmount] =>
[escrowStatus] =>
[disbursementDetails] => Braintree\DisbursementDetails Object
(
[_attributes:protected] => Array
(
[disbursementDate] =>
[settlementAmount] =>
[settlementCurrencyIsoCode] =>
[settlementCurrencyExchangeRate] =>
[fundsHeld] =>
[success] =>
)
)
[disputes] => Array
(
)
[paymentInstrumentType] => credit_card
[processorSettlementResponseCode] =>
[processorSettlementResponseText] =>
[threeDSecureInfo] =>
[creditCardDetails] => Braintree\Transaction\CreditCardDetails Object
(
[_attributes:protected] => Array
(
[token] =>
[bin] => 555555
[last4] => 4444
[cardType] => MasterCard
[expirationMonth] => 12
[expirationYear] => 2017
[customerLocation] => US
[cardholderName] =>
[imageUrl] => https://assets.braintreegateway.com/payment_method_logo/mastercard.png?environment=sandbox
[prepaid] => Unknown
[healthcare] => Unknown
[debit] => Unknown
[durbinRegulated] => Unknown
[commercial] => Unknown
[payroll] => Unknown
[issuingBank] => Unknown
[countryOfIssuance] => Unknown
[productId] => Unknown
[uniqueNumberIdentifier] =>
[venmoSdk] =>
[expirationDate] => 12/2017
[maskedNumber] => 555555******4444
)
)
[customerDetails] => Braintree\Transaction\CustomerDetails Object
(
[_attributes:protected] => Array
(
[id] =>
[firstName] =>
[lastName] =>
[company] =>
[email] =>
[website] =>
[phone] =>
[fax] =>
)
)
[billingDetails] => Braintree\Transaction\AddressDetails Object
(
[_attributes:protected] => Array
(
[id] =>
[firstName] =>
[lastName] =>
[company] =>
[streetAddress] =>
[extendedAddress] =>
[locality] =>
[region] =>
[postalCode] =>
[countryName] =>
[countryCodeAlpha2] =>
[countryCodeAlpha3] =>
[countryCodeNumeric] =>
)
)
[shippingDetails] => Braintree\Transaction\AddressDetails Object
(
[_attributes:protected] => Array
(
[id] =>
[firstName] =>
[lastName] =>
[company] =>
[streetAddress] =>
[extendedAddress] =>
[locality] =>
[region] =>
[postalCode] =>
[countryName] =>
[countryCodeAlpha2] =>
[countryCodeAlpha3] =>
[countryCodeNumeric] =>
)
)
[subscriptionDetails] => Braintree\Transaction\SubscriptionDetails Object
(
[_attributes:protected] => Array
(
[billingPeriodEndDate] =>
[billingPeriodStartDate] =>
)
)
)
)
can access object using magic method
$transaction_id = $result->transaction->__get('id');
you havr to loop through the response you have got using foreach(),
foreach($reas->_attributes as $key => $val) {
echo $key;// will give you the attribute name
is_array($val) ? print_r($val) : echo $val;// will give you the attribute value
}
I have two array
Array
(
[0] => stdClass Object
(
[id] => 14
[employee_id] => ST0011
[emp_name] => Munish Sharma
[gender] =>
[marital_status] =>
[address] =>
[postal_code] =>
[home_phone] =>
[mobile_phone] =>
[work_email] =>
[private_email] =>
[joined_date] => 1970-01-01 00:00:00
[emp_salary] => 0
[confirmation_date] => 0000-00-00 00:00:00
[department] => HRM
[f_name] => fname
[dob] => 1970-01-01
[c_address] =>
[domainName] =>
[emp_status] => active
)
[1] => stdClass Object
(
[id] => 12
[employee_id] => ST001dasd
[emp_name] => Rakesh Negi
[gender] =>
[marital_status] =>
[address] =>
[postal_code] =>
[home_phone] =>
[mobile_phone] =>
[work_email] =>
[private_email] =>
[joined_date] => 2015-08-11 00:00:00
[emp_salary] => 0
[confirmation_date] => 0000-00-00 00:00:00
[department] => HRM
[f_name] => dsad
[dob] => 1970-01-01
[c_address] =>
[domainName] =>
[emp_status] => active
)
[2] => stdClass Object
(
[id] => 13
[employee_id] => ST001
[emp_name] => Rakesh Negi
[gender] =>
[marital_status] =>
[address] =>
[postal_code] =>
[home_phone] =>
[mobile_phone] =>
[work_email] =>
[private_email] =>
[joined_date] => 2015-08-06 00:00:00
[emp_salary] => 0
[confirmation_date] => 0000-00-00 00:00:00
[department] => HRM
[f_name] => Father Name
[dob] => 1970-01-01
[c_address] =>
[domainName] =>
[emp_status] => active
)
)
and the second array is this
Array
(
[0] => stdClass Object
(
[id] => 55
[emp_id] => ST001
[check_in] => 11:38:09
[check_out] => 00:00:00
[total_time] => 00:00:00
[status] =>
[date_time] => 2015-10-20
[emp_name] => Rakesh Negi
)
)
Now I want to make it like this
Array
(
[0] => stdClass Object
(
[id] => 14
[employee_id] => ST0011
[emp_name] => Munish Sharma
[gender] =>
[marital_status] =>
[address] =>
[postal_code] =>
[home_phone] =>
[mobile_phone] =>
[work_email] =>
[private_email] =>
[joined_date] => 1970-01-01 00:00:00
[emp_salary] => 0
[confirmation_date] => 0000-00-00 00:00:00
[department] => HRM
[f_name] => fname
[dob] => 1970-01-01
[c_address] =>
[domainName] =>
[emp_status] => active
)
[1] => stdClass Object
(
[id] => 12
[employee_id] => ST001dasd
[emp_name] => Rakesh Negi
[gender] =>
[marital_status] =>
[address] =>
[postal_code] =>
[home_phone] =>
[mobile_phone] =>
[work_email] =>
[private_email] =>
[joined_date] => 2015-08-11 00:00:00
[emp_salary] => 0
[confirmation_date] => 0000-00-00 00:00:00
[department] => HRM
[f_name] => dsad
[dob] => 1970-01-01
[c_address] =>
[domainName] =>
[emp_status] => active
)
[3] => stdClass Object
(
[id] => 55
[emp_id] => ST001
[check_in] => 11:38:09
[check_out] => 00:00:00
[total_time] => 00:00:00
[status] =>
[date_time] => 2015-10-20
[emp_name] => Rakesh Negi
)
)
I want to remove the value from first array if the em_id in the both array will be same is it possible in?
I tried using where condion with join like this
$date = date('Y-m-d');
$this->db->select('hrm_attendance.*,employees.emp_name');
$this->db->from('hrm_attendance');
$this->db->join('employees','hrm_attendance.emp_id=employees.employee_id','LEFT OUTER');
$this->db->where('hrm_attendance.date_time = "'.$date.'"');
$this->db->order_by('check_in','asc');
$query = $this->db->get();
return $query->result();
and getting this output
(
[0] => stdClass Object
(
[id] => 55
[emp_id] => ST001
[check_in] => 11:38:09
[check_out] => 00:00:00
[total_time] => 00:00:00
[status] =>
[date_time] => 2015-10-20
[emp_name] => Rakesh Negi
)
)
But I want the output I have described above
if I remove $this->db->where('hrm_attendance.date_time = "'.$date.'"'); line then I am getting the all record from both the table I want all the data from the employee table with all ID and only those data from the hrm_attendance where the emp_id is available with where condition
$this->db->where('hrm_attendance.date_time = "'.$date.'"');
Please check the Code igniter Docs
http://www.codeigniter.com/userguide2/database/active_record.html#select
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
// Produces:
// SELECT * FROM blogs
// JOIN comments ON comments.id = blogs.id
in select('*') you can specify your limited fields also.
In your db->where is incorrect $this->db->where('hrm_attendance.date_time = "'.$date.'"');
http://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data
public function some_name() {
$date = date('Y-m-d');
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'hrm_attendance ha', 'LEFT');
$this->db->join($this->db->dbprefix . 'employees e', 'e.employee_id = ha.emp_id', 'LEFT');
$this->db->where('ha.date_time', $date);
$this->db->order_by('check_in','ASC');
$query = $this->db->get();
return $query->result();
// Or Try With result_array();
// return $query->result_array();
}
I find out the solution by adding some code like this
$date = date('Y-m-d');
$this->db->select('hrm_attendance.*,employees.emp_name');
$this->db->from('hrm_attendance');
$this->db->join('employees','hrm_attendance.emp_id=employees.employee_id','LEFT OUTER');
$this->db->where('hrm_attendance.date_time = "'.$date.'"');
$this->db->order_by('check_in','asc');
$query = $this->db->get();
$result = $query->result();
$count = count($result);
for($i = 0;$i<$count;$i++)
{
$id[] = $data['user_logged_in'][$i]->emp_id;
}
$this->db->select('*');
$this->db->from('employees');
$this->db->where_not_in('employee_id',$id);
$query = $this->db->get();
$data['absent'] = $query->result();
print_r($data['absent']);
By using this I got the all those employee who are not in the table hrm_attendance
i've an format array like this
Array (
[0] => stdClass Object ( [subscriptionContractId] => 20878 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-02 03:27:39.000 [endDate] => 2015-07-04 03:27:39.000 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 2 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => 2015-07-03 03:27:39.000 [lastPaymentDate] => 2015-07-02 03:27:39.000 [subscriptionContractHistory] =>Array ( [0] => stdClass Object ( [subscriptionContractId] => 20878 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-07-01 03:27:53.290 ) ) [subscriptionContractLogs] => )
[1] => stdClass Object ( [subscriptionContractId] => 20861 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:31:47.000 [endDate] => 2015-06-30 11:32:05.150 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:31:47.000 [subscriptionContractHistory] => Array ( [0] =>
stdClass Object ( [subscriptionContractId] => 20861 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:31:55.530 ) [1] => stdClass Object ( [subscriptionContractId] => 20861 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:32:05.977 ) ) [subscriptionContractLogs] => )
[2] => stdClass Object ( [subscriptionContractId] => 20860 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:29:37.000 [endDate] => 2015-06-30 11:30:19.887 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:29:37.000 [subscriptionContractHistory] => Array ( [0] => stdClass Object ( [subscriptionContractId] => 20860 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:30:10.267 ) [1] => stdClass Object ( [subscriptionContractId] => 20860 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:30:20.687 ) ) [subscriptionContractLogs] => )
[3] => stdClass Object ( [subscriptionContractId] => 20859 [merchantId] => 10062 [merchantName] => LinkIT360 [startDate] => 2015-07-01 11:27:33.000 [endDate] => 2015-06-30 11:27:57.683 [operatorCode] => 60201 [operatorName] => Mobinil-EGY [status] => 5 [isVerified] => 1 [initialPaymentproductId] => [initialPaymentDate] => [recurringPaymentproductId] => game_sku_1 [productCatalogName] => Game [autoRenewContract] => 1 [msisdn] => 201200000000 [customerAccountNumber] => 201200000000 [subscriptionContractName] => Game [nextPaymentDate] => [lastPaymentDate] => 2015-07-01 11:27:33.000 [subscriptionContractHistory] => Array ( [0] => stdClass Object ( [subscriptionContractId] => 20859 [status] => 2 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:27:42.173 ) [1] => stdClass Object ( [subscriptionContractId] => 20859 [status] => 5 [isMerchantNotified] => 1 [paymentTransactionId] => [date] => 2015-06-30 11:27:58.467 ) ) [subscriptionContractLogs] => )
i want to take this value [subscriptionContractId] => 20859
PFB my code
foreach($getContract as $key =>$value) {
$cancel = $getContract[$key]->subscriptionContractId;
$this->data['res'] = $cancel;
}
with that above code only showing single value of record, meanwhile record is around 27 records.
and another code, i try to rectify with initialize array, PFB.
$cancel = array()
foreach($getContract as $key =>$value) {
$cancel[] = $getContract[$key]->subscriptionContractId;
$this->data['res'] = $cancel;
}
i need only the value, of the array such as 20879, etc. eventhough i use double loop to extract particular array it only show the last record on 10663.
Please help
You can modify your code as follows (see php documenation about foreach for more background):
$cancel = array();
foreach($getContract as $value) {
$cancel[] = $value->subscriptionContractId;
...
}
This way, in every iteration $value is assigned one value from your getContract array, with it's key just being omitted. Then you can store the value's subscriptionContractId in your $cancel array to do whatever you are planning to do.
Note: briosheje is right about the line:
$this->data['res'] = $cancel;
$this->data['res'] is overwritten in every iteration. As you are overwriting it with the your $cancel array, your code should still work, but your doing unnecessary work here, as your array might be subjected to further change in the next iteration.
My advice would be to move this line after your loop brackets ended, so that your result is saved just once:
...
foreach($getContract as $value) {
...
}
$this->data['res'] = $cancel;