Yii 2 how to get all data within current day - php

Basically ProductOffer table.
Migration
$this->createTable('product_offer', [
'id' => $this->primaryKey(),
'product_id' => $this->bigInteger(20) . ' NOT NULL',
'customer_id' => $this->bigInteger(20),
'coupon_code' => $this->string(),
'offer' => $this->string(),
'customer_name' => $this->string(),
'email_address' => $this->string(),
'phone_number' => $this->string(),
'note' => $this->text(),
'admin_note' => $this->text(),
'status' => $this->tinyInteger(),
'created' => $this->dateTime() . ' NOT NULL',
'modified' => $this->dateTime(),
]);
Now I have data in this table and created query.
$stamp = mktime(0, 0, 0);
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address],
['>', 'created', date('m-d-Y H:i:s',$stamp)]
]);
echo "<pre>";
print_r($testQuery->all());
echo "</pre>";
The result is
Array
(
[0] => app\models\ProductOffer Object
(
[verifyCode] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 48
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => Rejected
[created] => 2018-05-16 04:38:28
[modified] =>
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 48
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => 0
[created] => 2018-05-16 04:38:28
[modified] =>
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_relationsDependencies:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
[1] => app\models\ProductOffer Object
(
[verifyCode] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 50
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => Rejected
[created] => 2018-05-17 04:39:55
[modified] =>
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 50
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => 0
[created] => 2018-05-17 04:39:55
[modified] =>
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_relationsDependencies:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
[2] => app\models\ProductOffer Object
(
[verifyCode] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 51
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => test
[admin_note] =>
[status] => Rejected
[created] => 2018-05-17 04:43:29
[modified] =>
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 51
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => test
[admin_note] =>
[status] => 0
[created] => 2018-05-17 04:43:29
[modified] =>
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_relationsDependencies:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
[3] => app\models\ProductOffer Object
(
[verifyCode] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 55
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => Rejected
[created] => 2018-05-17 04:51:57
[modified] =>
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 55
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07#gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => 0
[created] => 2018-05-17 04:51:57
[modified] =>
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_relationsDependencies:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
);
Today in our time zone is 5/17/2018, I want to get data that created today from but it seems my query is not excluding the data that created yesterday.
I already try expression this
['<', 'created', new Expression('NOW()')]
and this
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address],
['>', 'created', new Expression(date('m-d-Y H:i:s', $stamp))]
]);
echo "<pre>";
print_r($testQuery->all());
echo "</pre>";
And now I'm getting an SQL error
Database Exception – yii\db\Exception SQLSTATE[42000]: Syntax error or
access violation: 1064 You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '00:00:00)' at line 1 The SQL being executed was:
SELECT * FROM product_offer WHERE (status = 0) AND (product_id =
'111') AND (email_address = 'asrockg07#gmail.com') AND (created >
05-17-2018 00:00:00)

You may use raw SQL content with a WHERE call in string format, and it is also possible to and together two WHERE calls. Putting this together, we can try the following:
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address]
])
->andWhere('created >= CURDATE()');
Note that I think you want to use CURDATE here, which corresponds to today at midnight, if you want to restrict to records created at any point today.

You're using incorrect format for date. It should be date('Y-m-d H:i:s', $stamp) for passed stamp or date('Y-m-d 00:00:00', $stamp) to force midnight.
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address],
['>', 'created', date('Y-m-d 00:00:00', $stamp)]
]);
Also you're using yii\db\Expression in a wrong way. You cannot pass value directly to Expression, this may open you to SQL Injection. You should use params if you need this.
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address],
new Expression('created > :date', [':date' => date('Y-m-d 00:00:00', $stamp)]),
]);

Related

Error during an object call php soapclient

I'm getting Trying to get property 'timestamp' of non-object while trying to this code:
foreach ($BookingA as $object) {
$Bookings[] = array (
'PID' => $object->person,
'Project' => $object->levels->WSLevelIdentification->code,
'Startdate' => $object->from->timestamp,
'Endddate' => $object->to->timestamp
);
}
Where am I getting it wrong?
When i dump the data from my array i get the right value
array (size=61)
0 =>
array (size=4)
'PID' => string '2036' (length=4)
'Project' => string '19015' (length=5)
'Startdate' => string '26.07.2021 12:30:00' (length=19)
'Endddate' => string '26.07.2021 17:14:00' (length=19)
Soap-Output:
The Error is on this Line: $object->to->timestamp
What am I doing wrong?
stdClass Object ( [absence] => [checked] => [complete] => 1 [duration] => 284 [from] => stdClass Object ( [TS] => 1627295400000 [day] => 26 [hour] => 12 [min] => 30 [month] => 7 [sec] => 0 [timeInSeconds] => 1627295400 [timestamp] => 26.07.2021 12:30:00 [year] => 2021 ) [fromBookingID] => 6058 [fullDay] => [levels] => stdClass Object ( [WSLevelIdentification] => stdClass Object ( [code] => 19015 [levelID] => 1 ) ) [notice] => [person] => 2036 [properties] => stdClass Object ( [WSProperty] => Array ( [0] => stdClass Object ( [key] => 200015 [val] => true/48.2897333/16.3270315/11 ) [1] => stdClass Object ( [key] => 300015 [val] => true/48.1907349/16.5754274/29 ) ) ) [to] => stdClass Object ( [TS] => 1627312440000 [day] => 26 [hour] => 17 [min] => 14 [month] => 7 [sec] => 0 [timeInSeconds] => 1627312440 [timestamp] => 26.07.2021 17:14:00 [year] => 2021 ) [toBookingID] => 6131 )

Insert column value into array using array mapping

I have two array i want to add another key in first array by matching keys with second array below are the array that I have
First Array
Array
(
[0] => Array
(
[id] => 5198
[full_name] => Afnan
[username] => eay.d
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/haulysu4upload-image1499828198.jpg
)
[1] => Array
(
[id] => 5213
[full_name] => Nouf
[username] => noufalswailem
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/jlyfgi4fupload-image1502276119.jpg
)
[2] => Array
(
[id] => 5218
[full_name] => Mohammed Bin Abdullah
[username] => almuribadh
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/m3ttx0luupload-image1500789921.jpg
)
[3] => Array
(
[id] => 5225
[full_name] => Shadin Alshobaily
[username] => shash
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/srura4raupload-image1499829155.jpg
)
[4] => Array
(
[id] => 5251
[full_name] => Razan
[username] => ra.m
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/rwfieka6upload-image1499831173.jpg
)
)
Second array that I have
Array
(
[0] => Array
(
[id] => 38395
[request_by] => 2
[request_to] => 5198
)
)
Required Result
Array
(
[0] => Array
(
[id] => 5198
[full_name] => Afnan
[username] => eay.d
[is_live] => 1
[request_sent] => true
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/haulysu4upload-image1499828198.jpg
)
[1] => Array
(
[id] => 5213
[full_name] => Nouf
[username] => noufalswailem
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/jlyfgi4fupload-image1502276119.jpg
)
[2] => Array
(
[id] => 5218
[full_name] => Mohammed Bin Abdullah
[username] => almuribadh
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/m3ttx0luupload-image1500789921.jpg
)
[3] => Array
(
[id] => 5225
[full_name] => Shadin Alshobaily
[username] => shash
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/srura4raupload-image1499829155.jpg
)
[4] => Array
(
[id] => 5251
[full_name] => Razan
[username] => ra.m
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/rwfieka6upload-image1499831173.jpg
)
)
This is just because requested by user 2 has sent friend request to 5198 user and so that's why request_sent is true to this user only and remining will false if friend request not send I want to do this work using buitl in function instead of loops.
Seems very easy, may be i don't uderstand the task
$tmp = array_flip(array_column($arr2, 'request_to'));
foreach($arr1 as &$v) {
$v['request_sent'] = isset($tmp[$v[id]]);
}
Solution without loops (check it online):
<?php
$users = [
['id' => 5198, 'full_name' => 'Afnan', 'username' => 'eay.d', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5213, 'full_name' => 'Nouf', 'username' => 'noufalswailem', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5218, 'full_name' => 'Mohammed Bin Abdullah', 'username' => 'almuribadh', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5225, 'full_name' => 'Shadin Alshobaily', 'username' => 'shash', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5251, 'full_name' => 'Razan', 'username' => 'ra.m', 'is_live' => 1, 'picture' => 'someurl'],
];
$requests = [
['request_by' => 2, 'request_to' => 5198],
['request_by' => 2, 'request_to' => 5225],
];
array_walk($users, 'setUserRequestStatus', array_column($requests, 'request_to'));
var_dump($users);
function setUserRequestStatus(&$user, $key, $requests_sent_to) {
$user['request_sent'] = in_array($user['id'], $requests_sent_to);
}

How to group image order by time from array

I have array of photo with filename and time. I want to find the photos which are taken in hour. For example, I have photos taken from 10.00, 10.15, 10.59. So when I search. I want these photo to be listed under 10.00.
I think it's something to do with in_array or array_search. But I still don't have an idea how.
Here is my array: $pixArr[]
Array
(
[0] => Array
(
[file] => IMG_7519.JPG
[time] => 13:02
)
[1] => Array
(
[file] => IMG_7518.JPG
[time] => 13:01
)
[2] => Array
(
[file] => IMG_7517.JPG
[time] => 13:00
)
[3] => Array
(
[file] => IMG_7516.JPG
[time] => 11:39
)
[4] => Array
(
[file] => IMG_7515.JPG
[time] => 11:39
)
[5] => Array
(
[file] => IMG_7514.JPG
[time] => 11:38
)
[6] => Array
(
[file] => IMG_7513.JPG
[time] => 11:29
)
[7] => Array
(
[file] => IMG_7512.JPG
[time] => 11:26
)
[8] => Array
(
[file] => IMG_7511.JPG
[time] => 11:26
)
)
I expect it coming out something like this:
10:00
listPhoto.php
<?
//now how to list files by hour???
?>
you may use array_filter As #u_mulder suggested:
<?php
$pixArr = array
(
0 => array
(
'file' => 'IMG_7519.JPG',
'time' => '13:02'
),
1 => array
(
'file' => 'IMG_7518.JPG',
'time' => '13:01'
),
2 => array
(
'file' => 'IMG_7517.JPG',
'time' => '13:00'
),
3 => array
(
'file' => 'IMG_7516.JPG',
'time' => '11:39'
),
4 => array
(
'file' => 'IMG_7515.JPG',
'time' => '11:39'
),
5 => array
(
'file' => 'IMG_7514.JPG',
'time' => '11:38'
),
6 => array
(
'file' => 'IMG_7513.JPG',
'time' => '11:29'
),
7 => array
(
'file' => 'IMG_7512.JPG',
'time' => '11:26'
),
8 => array
(
'file' => 'IMG_7511.JPG',
'time' => '11:26'
),
);
print_r(array_filter($pixArr, function($value){
if($value['time'] >= '11:00' && $value['time'] <= '12:00')
return $value;
}));
See Demo: https://eval.in/513466
you can using assort This function sorts an array such that array indices maintain their correlation with the array elements they are associated with.
I have been tried this problem.
$arr = [
[
"file" => "IMG_7519.JPG",
"time" => "13:02"
],
[
"file" => "IMG_7519.JPG",
"time" => "12:02"
],
[
"file" => "IMG_7519.JPG",
"time" => "15:02"
]
];
asort($arr); //sorting array
echo "<pre>".print_r($arr)."</pre>";
and this is the result.
Array ( [1] => Array ( [file] => IMG_7519.JPG [time] => 12:02 ) [0] => Array ( [file] => IMG_7519.JPG [time] => 13:02 ) [2] => Array ( [file] => IMG_7519.JPG [time] => 15:02 ) )

access attribute array from transaction object

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
}

formatting an array using php

I have the following array which have duplicate data:
Array
(
[0] => Array
(
[orders] => Array
(
[id] => 9
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => No contact
[chashed] => NO
[created] => 2015-06-27 12:49:34
[modified] => 2015-06-27 12:49:34
[comment] =>
)
[order_products] => Array
(
[id] => 2
[order_id] => 9
[product_id] => 1
[pieces] => 1
)
[products] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[psettings] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-28 00:28:26
[bppp] => 44000
[sppp] => 45000
[discount] => 25
[service_charge] => 30
)
)
[1] => Array
(
[orders] => Array
(
[id] => 10
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 1
[location_id] => 1
[status] => No contact
[chashed] => NO
[created] => 2015-06-28 03:30:25
[modified] => 2015-06-28 03:30:25
[comment] =>
)
[order_products] => Array
(
[id] => 6
[order_id] => 10
[product_id] => 1
[pieces] => 1
)
[products] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[psettings] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-28 00:28:26
[bppp] => 44000
[sppp] => 45000
[discount] => 25
[service_charge] => 30
)
)
[2] => Array
(
[orders] => Array
(
[id] => 9
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => No contact
[chashed] => NO
[created] => 2015-06-27 12:49:34
[modified] => 2015-06-27 12:49:34
[comment] =>
)
[order_products] => Array
(
[id] => 3
[order_id] => 9
[product_id] => 2
[pieces] => 1
)
[products] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[psettings] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
)
[3] => Array
(
[orders] => Array
(
[id] => 10
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 1
[location_id] => 1
[status] => No contact
[chashed] => NO
[created] => 2015-06-28 03:30:25
[modified] => 2015-06-28 03:30:25
[comment] =>
)
[order_products] => Array
(
[id] => 5
[order_id] => 10
[product_id] => 2
[pieces] => 1
)
[products] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[psettings] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
)
[4] => Array
(
[orders] => Array
(
[id] => 9
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => No contact
[chashed] => NO
[created] => 2015-06-27 12:49:34
[modified] => 2015-06-27 12:49:34
[comment] =>
)
[order_products] => Array
(
[id] => 4
[order_id] => 9
[product_id] => 3
[pieces] => 1
)
[products] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
[psettings] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
)
[5] => Array
(
[orders] => Array
(
[id] => 10
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 1
[location_id] => 1
[status] => No contact
[chashed] => NO
[created] => 2015-06-28 03:30:25
[modified] => 2015-06-28 03:30:25
[comment] =>
)
[order_products] => Array
(
[id] => 7
[order_id] => 10
[product_id] => 3
[pieces] => 1
)
[products] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
[psettings] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
)
)
I want to format this array and produce the following array:
Array
(
[0] => Array
(
[orders] => Array
(
[id] => 9
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => No contact
[chashed] => NO
[created] => 2015-06-27 12:49:34
[modified] => 2015-06-27 12:49:34
[comment] =>
)
[order_products] => Array
(
[0] => Array
(
[id] => 2
[order_id] => 9
[product_id] => 1
[pieces] => 1
)
[1] => Array
(
[id] => 3
[order_id] => 9
[product_id] => 2
[pieces] => 1
)
[2] => Array
(
[id] => 4
[order_id] => 9
[product_id] => 3
[pieces] => 1
)
)
[products] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[1] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[2] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
)
[psettings] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-28 00:28:26
[bppp] => 44000
[sppp] => 45000
[discount] => 25
[service_charge] => 30
)
[1] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
[2] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
)
)
[1] => Array
(
[orders] => Array
(
[id] => 10
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 1
[location_id] => 1
[status] => No contact
[chashed] => NO
[created] => 2015-06-28 03:30:25
[modified] => 2015-06-28 03:30:25
[comment] =>
)
[order_products] => Array
(
[0] => Array
( [id] => 6
[order_id] => 10
[product_id] => 1
[pieces] => 1
)
[1] => Array
(
[id] => 5
[order_id] => 10
[product_id] => 2
[pieces] => 1
)
[2] => Array
(
[id] => 7
[order_id] => 10
[product_id] => 3
[pieces] => 1
)
)
[products] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[1] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[2] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
)
[psettings] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-28 00:28:26
[bppp] => 44000
[sppp] => 45000
[discount] => 25
[service_charge] => 30
)
[1] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
[2] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
)
)
)
How can I do this. I reformat simple array but this does not make sense to me to format. It makes me cry. Please help me. If any helper function is suggested it will be really a gift. Thanks in advance.
You need to do like below:-
<?php
$firstArray = Array
(
'0' => Array
(
'orders' => Array
(
'id' => 9,
'name' => 'Abdus Sattar Bhuiyan',
'email' => 'sattar.kuet#gmail.com',
'mobile' => '01673050495',
'alt_mobile' => '01818953250',
'city_id' => 2,
'location_id' => 5,
'status' => 'No contact',
'chashed' => 'NO',
'created' => '2015-06-27 12:49:34',
'modified' => '2015-06-27 12:49:34',
'comment' => ''
),
'order_products' => Array
(
'id' => 2,
'order_id' => 9,
'product_id' => 1,
'pieces' => 1
),
'products' => Array
(
'id' => 1,
'category_id' => 1,
'name' => 'নভোযানের নাম সি প্রোগ্রামিং',
'writer' => 'Engr. Abdus Sattar Bhuiyan',
'created' => '2015-06-24 16:17:45'
),
'psettings' => Array
(
'id' => 1,
'category_id' => 1,
'product_id' => 1,
'img' => '1.jpg',
'desc' => 'description',
'created' => '2015-06-28 00:28:26',
'bppp' => 44000,
'sppp' => 45000,
'discount' => 25,
'service_charge' => 30
),
),
'1' => Array
(
'orders' => Array
(
'id' => 9,
'name' => 'Abdus Sattar Bhuiyan',
'email' => 'sattar.kuet#gmail.com',
'mobile' => '01673050495',
'alt_mobile' => '01818953250',
'city_id' => 2,
'location_id' => 5,
'status' => 'No contact',
'chashed' => 'NO',
'created' => '2015-06-27 12:49:34',
'modified' => '2015-06-27 12:49:34',
'comment' => ''
),
'order_products' => Array
(
'id' => 3,
'order_id' => 9,
'product_id' => 2,
'pieces' => 1
),
'products' => Array
(
'id' => 2,
'category_id' => 1,
'name' => 'Resonance of creativity with C++',
'writer' => 'Engr. Abdus Sattar Bhuiyan',
'created' => '2015-06-26 07:32:52'
),
'psettings' => Array
(
'id' => 2,
'category_id' => 1,
'product_id' => 2,
'img' => '2.jpg',
'desc' => '',
'created' => '2015-06-26 07:33:41',
'bppp' => 150,
'sppp' => 250,
'discount' => 20,
'service_charge' => 30
),
),
'2' => Array
(
'orders' => Array
(
'id' => 9,
'name' => 'Abdus Sattar Bhuiyan',
'email' => 'sattar.kuet#gmail.com',
'mobile' => '01673050495',
'alt_mobile' => '01818953250',
'city_id' => 2,
'location_id' => 5,
'status' => 'No contact',
'chashed' => 'NO',
'created' => '2015-06-27 12:49:34',
'modified' => '2015-06-27 12:49:34',
'comment' => ''
),
'order_products' => Array
(
'id' => 4,
'order_id' => 9,
'product_id' => 3,
'pieces' => 1
),
'products' => Array
(
'id' => 3,
'category_id' => 1,
'name' => 'programming by story C',
'writer' => 'Hasibul Hasan Shanto',
'created' => '2015-06-26 07:35:57'
),
'psettings' => Array
(
'id' => 3,
'category_id' => 1,
'product_id' => 3,
'img' => '3.jpg',
'desc' => '',
'created' => '2015-06-26 07:36:26',
'bppp' => 150,
'sppp' => 250,
'discount' => 10,
'service_charge' => 30
),
),
);
$new_array = array();
foreach($firstArray as $key=> $arr){
if($key == 0){
$new_array[0]['orders'] = $arr['orders'];
$new_array[0]['order_products'][] = $arr['order_products'];
$new_array[0]['products'][] = $arr['products'];
$new_array[0]['psettings'][] = $arr['psettings'];
}else{
foreach($new_array as $key=> $newarr){
if($arr['orders'] == $newarr['orders']){
$new_array[$key]['order_products'][] = $arr['order_products'];
$new_array[$key]['products'][] = $arr['products'];
$new_array[$key]['psettings'][] = $arr['psettings'];
}else{
$new_array[] = $arr;
}
}
}
}
echo "<pre/>";print_r($new_array);
?>
Output:- https://eval.in/388565.
Note:- don't worry about the array i putted. I just take your array and formatted it for running purpose and checking at my end. thanks.
This is a fairly simple job for array_column() (manual reference). Like this ($array is your source):
$composite = [];
$composite['orders'] = array_column($array, 'orders')[0];
$composite['order_products'] = array_column($array, 'order_products');
$composite['products'] = array_column($array, 'products');
$composite['psettings'] = array_column($array, 'psettings');
print_r($composite);
This results in the following array:
Array
(
[orders] => Array
(
[id] => 9
[name] => Abdus Sattar Bhuiyan
[email] => sattar.kuet#gmail.com
[mobile] => 01673050495
[alt_mobile] => 01818953250
[city_id] => 2
[location_id] => 5
[status] => No contact
[chashed] => NO
[created] => 2015-06-27 12:49:34
[modified] => 2015-06-27 12:49:34
[comment] =>
)
[order_products] => Array
(
[0] => Array
(
[id] => 2
[order_id] => 9
[product_id] => 1
[pieces] => 1
)
[1] => Array
(
[id] => 3
[order_id] => 9
[product_id] => 2
[pieces] => 1
)
[2] => Array
(
[id] => 4
[order_id] => 9
[product_id] => 3
[pieces] => 1
)
)
[products] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[name] => নভোযানের নাম সি প্রোগ্রামিং
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-24 16:17:45
)
[1] => Array
(
[id] => 2
[category_id] => 1
[name] => Resonance of creativity with C++
[writer] => Engr. Abdus Sattar Bhuiyan
[created] => 2015-06-26 07:32:52
)
[2] => Array
(
[id] => 3
[category_id] => 1
[name] => programming by story C
[writer] => Hasibul Hasan Shanto
[created] => 2015-06-26 07:35:57
)
)
[psettings] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[product_id] => 1
[img] => 1.jpg
[desc] => description
[created] => 2015-06-28 00:28:26
[bppp] => 44000
[sppp] => 45000
[discount] => 25
[service_charge] => 30
)
[1] => Array
(
[id] => 2
[category_id] => 1
[product_id] => 2
[img] => 2.jpg
[desc] =>
[created] => 2015-06-26 07:33:41
[bppp] => 150
[sppp] => 250
[discount] => 20
[service_charge] => 30
)
[2] => Array
(
[id] => 3
[category_id] => 1
[product_id] => 3
[img] => 3.jpg
[desc] =>
[created] => 2015-06-26 07:36:26
[bppp] => 150
[sppp] => 250
[discount] => 10
[service_charge] => 30
)
)
)
If you need to aggregate multiple such arrays into one master array that contains it all, then you can for example wrap it inside a simple loop and add a numeric index for each composite set:
$composite = [];
foreach($arrays as $num=>$array) {
$composite[$num]['orders'] = array_column... // etc.
}
Also make note of the index_key feature of array_column mentioned in the manual, may come in handy if you want to use the order IDs as the main keys --- though for that, you'd have to re-factor the loop logic a bit. Let me know if it's a relevant concern and I'll add more; or if this is enough as-is.

Categories