I am working with a an array and I get getting this error above, I working with a very simply array that looks like this,
array (
'Emails' =>
array (
0 =>
array (
'id' => 172,
'email' => 'sam#andrews.com',
'first_name' => 'Sam',
'last_name' => 'Andrews',
'display_name' => 'simonainley',
'initials' => 'SA',
'active' => 1,
'login_type' => 'normal',
'cost_visible' => 0,
'notification_frequency' => 'D',
'admin' => 1,
'pivot' =>
array (
'organisation_id' => 200,
'user_id' => 172,
'is_admin' => 1,
),
),
1 =>
array (
'id' => 110,
'email' => 'mike#fish.com',
'first_name' => 'Mike',
'last_name' => 'Fish',
'display_name' => 'mikefish',
'initials' => 'MF',
'active' => 1,
'login_type' => 'normal',
'cost_visible' => 0,
'notification_frequency' => 'H',
'admin' => 1,
'pivot' =>
array (
'organisation_id' => 200,
'user_id' => 110,
'is_admin' => 1,
),
),
'notification' => 'A user changed the status of New SEA LTD Projectto <strong>completed</strong>.',
),
)
This array gets set into a variable, and then I loop through it, like this,
foreach($data['emails'] as $email) {
Log::info($email);
$emailData['id'] = $email['id'];
$emailData['first_name'] = $email['first_name'];
$emailData['last_name'] = $email['last_name'];
$emailData['email'] = $email['email'];
Log::info($emailData);
}
The Log::info($email) outputs the following,
array (
'id' => 110,
'email' => 'mike#fish.com',
'first_name' => 'Mike',
'last_name' => 'Fish',
'display_name' => 'mikefish',
'initials' => 'MF',
'active' => 1,
'login_type' => 'normal',
'cost_visible' => 0,
'notification_frequency' => 'H',
'admin' => 1,
'pivot' =>
array (
'organisation_id' => 200,
'user_id' => 110,
'is_admin' => 1,
),
)
The Log::info($emailData) outputs the following,
array (
'id' => 110,
'first_name' => 'Mike',
'last_name' => 'Fish',
'email' => 'mike#fish.com',
)
So I can see the 'id' attribute in my logs so why would i be seeing,
exception 'ErrorException' with message 'Illegal string offset 'id''
the exception is being triggered by this line apparently,
$emailData['id'] = $email['id'];
any ideas?
You have a third entry in your array 'notification' => 'A user changed...' which is a string, and therefore does not have an id (nor does it have any of the other fields: email, first_name, etc).
Related
This question already has answers here:
Sort array using array_multisort() with dynamic number of arguments/parameters/rules/data
(5 answers)
Closed last month.
I'm using array_multisort (PHP 7.4) to apply a mutli-dimensional sort. I think I'm using it correctly, but the result is not what I would expect. No matter what direction I set, it only sorts ascending (even if I set both to SORT_DESC). I tried different sort flags (e.g. SORT_STRING, SORT_NATURAL), but this did not make a difference.
$definitions = [
[
'name' => 'Bart',
'age' => 12,
'location' => 'Brazil',
],
[
'name' => 'Daniel',
'age' => 51,
'location' => 'Brazil',
],
[
'name' => 'Adam',
'age' => 33,
'location' => 'France',
],
[
'name' => 'Adam',
'age' => 44,
'location' => 'France',
],
[
'name' => 'Adam',
'age' => 5,
'location' => 'France',
],
[
'name' => 'Zed',
'age' => 21,
'location' => 'GB',
],
];
$sorting = [
[
'field' => 'name',
'direction' => SORT_ASC,
],
[
'field' => 'age',
'direction' => SORT_DESC,
]
];
$sort_args = [];
foreach ($sorting as $sort) {
$sort_args[] = array_column($definitions, $sort['field']);
$sort_args[] = $sort['direction'];
$sort_args[] = SORT_REGULAR;
}
array_multisort($definitions, ...$sort_args);
The result:
array (
0 =>
array (
'name' => 'Adam',
'age' => 5,
'location' => 'France',
),
1 =>
array (
'name' => 'Adam',
'age' => 33,
'location' => 'France',
),
2 =>
array (
'name' => 'Adam',
'age' => 44,
'location' => 'France',
),
3 =>
array (
'name' => 'Bart',
'age' => 12,
'location' => 'Brazil',
),
4 =>
array (
'name' => 'Daniel',
'age' => 51,
'location' => 'Brazil',
),
5 =>
array (
'name' => 'Zed',
'age' => 21,
'location' => 'GB',
),
)
What I expected:
array (
0 =>
array (
'name' => 'Adam',
'age' => 44,
'location' => 'France',
),
1 =>
array (
'name' => 'Adam',
'age' => 33,
'location' => 'France',
),
2 =>
array (
'name' => 'Adam',
'age' => 5,
'location' => 'France',
),
3 =>
array (
'name' => 'Bart',
'age' => 12,
'location' => 'Brazil',
),
4 =>
array (
'name' => 'Daniel',
'age' => 51,
'location' => 'Brazil',
),
5 =>
array (
'name' => 'Zed',
'age' => 21,
'location' => 'GB',
),
)
Am I missing something or is this busted?
You have the arguments in the wrong order. The array you wish to sort should be the last item for this type of sorting.
Example #3: https://www.php.net/manual/en/function.array-multisort.php#example-5040
You may have done this so that you can use the spread operator (which is fine). If you want to continue using the spread operator you would adjust your code as so:
$sort_args[] = &$definitions; // add this line
array_multisort(...$sort_args); // remove `$definitions` from this line
Note that we are passing $definitions as reference otherwise when we push it to $sort_args, we would be creating a copy (due to the nature of PHP).
Without passing by reference, that original array won't be sorted, only the copy we created.
I have two classes containing informations about accounts and invoices like this
The accounts list
array (
0 =>
'id' => 1,
'username' => 'abc',
'company' => 'My Corporation',
)),
1 =>
'id' => 2,
'username' => 'cde',
'company' => 'My Company',
))
The invoice list
array (
0 =>
'account_id' => 1,
'invoiceId' => '15',
)),
1 =>
'id' => 2,
'account_id' => '2',
'invoiceId' => '17',
)),
2 =>
'account_id' => 1,
'invoiceId' => '20',
)),
3 =>
'id' => 2,
'account_id' => '2',
'invoiceId' => '30',
))
When iterating over both collections each invoice to be shown once. My issues is that everything will be duplicated
My code is
<?php
foreach ($invoices as $invoice) {
foreach($accounts as $account) {
echo $account->getUsername($invoice->getAccountId())."<BR>";
echo $account->getCompany($invoice->getAccountId());
}
}
I am not sure what I should do
I think this sould solve tour problem:
<?php
$accounts = array (
0 => array (
'id' => 1,
'username' => 'abc',
'company' => 'My Corporation',
),
1 => array (
'id' => 2,
'username' => 'cde',
'company' => 'My Company',
));
$invoices = array (
0 => array (
'account_id' => 1,
'invoiceId' => '15',
),
1 => array (
'id' => 2,
'account_id' => '2',
'invoiceId' => '17',
),
2 => array (
'account_id' => 1,
'invoiceId' => '20',
),
3 => array (
'id' => 2,
'account_id' => '2',
'invoiceId' => '30',
));
foreach($accounts as $account) {
foreach ($invoices as $invoice) {
echo "account id : ";
echo $account['id'];
echo "invoices: ";
if ( $invoice['account_id'] === $account['id'] ) {
echo $invoice['account_id'];
// echo $account->getUsername($invoice->getAccountId())."<BR>";
// echo $account->getCompany($invoice->getAccountId());
}
}
}
I have an application I am trying to update from legacy to new driver. I am having a problem with collections being damaged when the below code is triggered. I think I have narrowed it down here.
function update($collection,$criteria,$data,$insertIfNotExists = false)
{
if(!empty($collection) && !empty($criteria) && !empty($data)) {
if (!isset($this->collection[$collection])) {
$this->collection[$collection] = (new MongoDB\Client)->hebe->{$collection};
}
if ($insertIfNotExists) {
$oldData = $this->collection[$collection]->findOne($criteria);
if ($oldData == NULL) {
$data['createdDate'] = date("Y-m-d H:i:s");
$data['modifiedDate'] = (isset($data['modifiedDate'])) ? $data['modifiedDate']:date("Y-m-d H:i:s");
/*
return ($this->collection[$collection]->insertOne($data)) ? array('status'=>'ok'):array('status'=>'error','error'=>'unknown_error');
*/
} else {
$newData = $oldData;
foreach($data as $n=>$v) {
$newData[$n] = $v;
}
$newData['modifiedDate'] = (isset($newData['modifiedDate'])) ? $newData['modifiedDate']:date("Y-m-d H:i:s");
/*
return ($this->collection[$collection]->updateOne($criteria,['$set' => $newData])) ? array('status'=>'ok'):array('status'=>'error','error'=>'unknown_error');
*/
}
} else {
/*
return ($this->collection[$collection]->updateOne($criteria,['$set' => $data])) ? array('status'=>'ok'):array('status'=>'error','error'=>'unknown_error');
*/
}
}
}
example variables are
$collection = 'customer'
$criteria = array ( 'number' => '9999',)
$data = array (
'number' => '9999',
'name' => 'Testing Account',
'reference' => 'Peter Smith',
'defaultDeliveryAddress' => 1,
'visitAddress' => '',
'address' => '',
'district' => 'Marknad',
'postAddress' => '',
'orgNumber' => '5562041771',
'phone' => '031-7802700',
'fax' => '031-193328',
'groupCode' => 'int',
'creditCustomer' => '',
'typeOfDelivery' => 'Bil',
'typeOfPayment' => '10',
'emailAddresses' =>
array (
'invoice' => 'email1#domain.com',
'order' => 'email2#domain.com',
'deliveryNote' => 'email3#domain.com',
'packingSlip' => 'email4#domain.com',
),
'orderType' => NULL,
'termsOfDelivery' => 'RC',
'creditLimit' => 100000.0,
'pricelist' => 4,
'pricelist1' => '',
'pricelist2' => '9998',
'pricelist3' => '104',
'discount' => NULL,
'countryCode' => 'SE',
'currencyCode' => 'SEK',
'blocked' => 0,
'deliveryCost' => 0,
'vatCode' => '2',
'email' => 'peremail#domain.com',
'password' => 'password',
'modifiedDate' => '2019-06-25 00:00:00',
'deliveryAddresses' =>
array (
0 =>
array (
'row' => 1,
'name' => 'Test Address',
'address' => 'Box 12345',
'postAddress' => '42246',
'default' => true,
),
1 =>
array (
'number' => '9999',
'name' => 'Testing Address',
'reference' => '13232',
'address' => 'Box 12245',
'postAddress' => '42246',
),
),
'references' =>
array (
0 =>
array (
'number' => '9999',
'name' => 'Testing',
'email' => '',
'password' => '',
),
1 =>
array (
'number' => '9999',
'name' => 'Testing2',
'email' => '',
'password' => 'password',
),
2 =>
array (
'number' => '9999',
'name' => 'Peter Smith',
'email' => '',
'password' => 'password',
),
),
)
Can someone point me in the right direction on what I am doing wrong with updateOne and insertOne. From what I understand by the docs is it supports array.
EDIT: a little background is I upgraded this system and MongoDB from 2.6 to 3.6. I also upgraded mongodb driver from legacy mongo.
The problem was the old data in the collection had quotations and the updateOne/insertOne data did not. Correcting this seem to solve the problem.
How can I get the all the 'name' and 'city' value back in an array from the following multidimensional array?
$myarray=Array(Array('name' => 'A','id' => '1', 'phone' => '416-23-55',
Base => Array ('city' => 'toronto'),'EBase' => Array('city' => 'North York'),
'Qty' => '1'), (Array('name' =>'B','id' => '1','phone' => '416-53-66','Base' =>
Array ('city' => 'qing'), 'EBase' => Array('city' => 'chong'),'Qty' => '2')));
I expect the returned value be
$namearray=Array('A','B');
$basecityarray=Array('toronto','qing');
$Ebasecityarray=Array('North York','chong');
Thank you!
You can definitely try something like this:
$shop = array( array( 'Title' => "rose",
'Price' => 1.25,
'Number' => 15
),
array( 'Title' => "daisy",
'Price' => 0.75,
'Number' => 25,
),
array( 'Title' => "orchid",
'Price' => 1.15,
'Number' => 7
)
);
You can access the first row as
echo $shop[0][0]." costs ".$shop[0][1]." and you get ".$shop[0][2]."<br />";
or $shop[0]->Title will return to you rose.
Ok I have a mult-dimensional array which has the following structure...
0 =>
array (
'membership' =>
array (
'member' =>
array (
'name' => '',
'landline' => '',
'libcard' => '',
'mobile' => '',
'email' => '',
),
'updated_at' => '',
'member_id' => 12345,
'starts_at' => '',
'id' => 14,
'group_id' => 280,
'optional_field_values' =>
array (
0 =>
array (
'optional_field' =>
array (
'name' => '',
'updated_at' => '',
'id' => 1,
'group_id' => 280,
'description' => '',
'created_at' => '',
),
'updated_at' => '',
'optional_field_id' => 1,
'membership_id' => 14,
'id' => 4,
'value' => '12539267',
'created_at' => '',
),
),
'ends_at' => '',
'joining_fee' => 0,
'created_at' => '',
),
),
Now I can access everything inside Membership and inside Member using code like...
$member[0]['membership']['member']['name']
or
$member[0]['membership']['joining_fee']
But when ever I try to access stuff inside optional_field_values I get nothing returned...
Any ideas why this is not working?
Edit:
Trying to access the field using code like...
$member[0]['membership']['optional_field_values']['value']
$member[0]['membership']['optional_field_values'][0]['value']
^ Should work...
(Edited to match OP's edit)
How about :
$member[0]['membership']['optional_field_values'][0]['value']
You can iterate over all optional field values like this :
foreach ($member[0]['membership']['optional_field_values'] as $field)
echo $field['value'];