PHP: Compare key value of objects - php

I have two arrays each one contain a set of objects
$input = [
(object)[
'id' => 2,
'number' => 54523,
'date' => '2019-12-17',
'book_for' => 'tttt',
'subject' => 'tttt',
'content' => 'ttt',
'language' => 'Arabic',
'cpfor' => 'tt',
'book_key' => '2WHae',
'note' => 'ttt',
'type' => 'Internal',
'pid' => 2,
'bookid' => 2,
'process' => 'STA',
],
];
and the second array is
$answers = [
(object)[
'aid' => 1,
'a_number' => 0,
'answer_of' => 1,
'answer_date' => '2019-12-17 00:00:00',
'answer_for' => 'xcc',
'answer_subject' => 'DSF',
'answer_content' => 'FSD',
'language' => '',
'cpfor' => 'WE',
'note' => 'EWWE',
],
(object)[
'aid' => 2,
'a_number' => 0,
'answer_of' => 2,
'answer_date' => '2019-12-19 00:00:00',
'answer_for' => 'answer for tt',
'answer_subject' => 'answer for tt',
'answer_content' => 'answer for tt',
'language' => 'Arabic',
'cpfor' => 'answer for ttvv',
'note' => 'answer for tt',
],
];
I want looping through and compare the value of id key in first array with value of answer_of key in second array if the id is not the same answer_of then return this object else ignore in other mean if id is not exist in second array(answer_of) return the object with all key and values else ignore
i tried the below function but even get those are exist in second array
function unanswered2($array1, $array2)
{
if (!(empty($array2))) {
$unanswered_arrays = [];
foreach ($array1 as $b) {
foreach ($array2 as $a) {
if ($b->bookid != $a->answer_of) {
array_push($unanswered_arrays, (object)$b);
}
}
}
return $unanswered_arrays;
}
return $array1;
}
anyone can tell where i have an error or any suggestion
the result is
$result=unanswered2($input,$answers);
$result= [
(object)[
'id' => 2,
'number' => 54523,
'date' => '2019-12-17',
'book_for' => 'tttt',
'subject' => 'tttt',
'content' => 'ttt',
'language' => 'Arabic',
'cpfor' => 'tt',
'book_key' => '2WHae',
'note' => 'ttt',
'type' => 'Internal',
'pid' => 2,
'bookid' => 2,
'process' => 'STA',
],
];
it should ignore it not return

I think the simplest solution is to use array_filter and a prepared list of answer_of values (for which you can use array_column).
Here's an example:
function findUnanswered(array $questions, array $answers): array
{
// use array_column to prepare a list of `answer_of` values
// *array_flip* them, so we can later use isset for better performance
$availableAnswers = array_flip(array_column($answers, 'answer_of'));
// filter all questions
return array_filter(
$questions,
// by a callback function, that
static function (object $question) use($availableAnswers): bool {
// ^ which we *use* from the outer scope
// |
// checks if the question id is [not set]* in +
// (* meaning no answer exists)
return !isset($availableAnswers[$question->id]);
}
);
}
Demo: https://3v4l.org/ZKlNn

I've read your comment to the prev my answer...
function getDiff($ar1, $ar2){
return array_filter($ar2, function($obj) use ($ar1) {
foreach($ar1 as $obj1){
return $obj1->bookid !== $obj->answer_of;
}
}, ARRAY_FILTER_USE_BOTH);
}
print_r(getDiff($input,$answers));
Output:
Array
(
[0] => stdClass Object
(
[aid] => 1
[a_number] => 0
[answer_of] => 1
[answer_date] => 2019-12-17 00:00:00
[answer_for] => xcc
[answer_subject] => DSF
[answer_content] => FSD
[language] =>
[cpfor] => WE
[note] => EWWE
)
)
https://3v4l.org/oGQWS

Provided you don't care about the original keys in the answers array.
Input:
<?php
$input = [
(object)[
'id' => 2,
'number' => 54523,
'date' => '2019-12-17',
'book_for' => 'tttt',
'subject' => 'tttt',
'content' => 'ttt',
'language' => 'Arabic',
'cpfor' => 'tt',
'book_key' => '2WHae',
'note' => 'ttt',
'type' => 'Internal',
'pid' => 2,
'bookid' => 2,
'process' => 'STA',
],
];
$answers = [
(object)[
'aid' => 1,
'a_number' => 0,
'answer_of' => 1,
'answer_date' => '2019-12-17 00:00:00',
'answer_for' => 'xcc',
'answer_subject' => 'DSF',
'answer_content' => 'FSD',
'language' => '',
'cpfor' => 'WE',
'note' => 'EWWE',
],
(object)[
'aid' => 2,
'a_number' => 0,
'answer_of' => 2,
'answer_date' => '2019-12-19 00:00:00',
'answer_for' => 'answer for tt',
'answer_subject' => 'answer for tt',
'answer_content' => 'answer for tt',
'language' => 'Arabic',
'cpfor' => 'answer for ttvv',
'note' => 'answer for tt',
],
];
Method:
$unanswered = array_diff_key(
array_column($answers, null, 'answer_of'),
array_column($input, null, 'id')
);
var_export($unanswered);
Output:
array (
1 =>
stdClass::__set_state(array(
'aid' => 1,
'a_number' => 0,
'answer_of' => 1,
'answer_date' => '2019-12-17 00:00:00',
'answer_for' => 'xcc',
'answer_subject' => 'DSF',
'answer_content' => 'FSD',
'language' => '',
'cpfor' => 'WE',
'note' => 'EWWE',
)),
)

Related

Filter and re-format a given multi-dimensional array (php)

I have been given a dataset by my tutor and asked to carry out the following:
// make a function
// get skus of which the figure of the average monthly sales unit is equal or greater than 350
// array in the following format:
[
'id' => 11102,
'sku' => 'TEST_3',
'alternates' => [
'asin' => [
'code' => '50',
'value' => 'JL1235',
],
'barcode' => [
'code' => '10',
'value' => 'JS160694',
],
],
'commodityCode' => '9989898889',
'price' => [
'value' => 145.99,
],
'instructions' => [
'picking' => [
'code' => 'PICK',
'value' => 'I\'VE JUST HAD AN UNHAPPY LOVE AFFAIR, SO I DON\'T SEE WHY ANYBODY ELSE SHOULD HAVE A GOOD TIME.',
],
],
'quantity' => [
'inner' => 100,
'masterCarton' => 200,
'pallet' => 300,
],
'averageMonthlyUnitSales' => 100,
'created_at' => '2022-03-13 04:14:12'
],
Example dataset is here:
$data = [
[
'id' => '9947',
'sku' => 'test_1',
'asin_code' => '50',
'asin_value' => '',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '120.50',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => '',
'qty_inner' => '120',
'qty_masterCarton' => '200',
'qty_pallet' => '1200',
'averageMonthlyUnitSales' => '750',
'created_at' => '2019-02-23T01:54:14.957299+00:00'
],
[
'id' => '10921',
'sku' => 'test_2',
'asin_code' => '50',
'asin_value' => 'bx12345',
'barcode' => '10',
'barcode_value' => 'jb170931',
'commodityCode' => '9989898889',
'price' => '20.59',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'It\'s only half completed, I\'m afraid',
'qty_inner' => '70',
'qty_masterCarton' => '250',
'qty_pallet' => '270',
'averageMonthlyUnitSales' => '120',
'created_at' => '2021-12-23T11:41:31.193982+00:00'
],
[
'id' => '11102',
'sku' => 'test_3',
'asin_code' => '50',
'asin_value' => 'jl1235',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '145.99',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'I\'ve just had an unhappy love affair, so I don\'t see why anybody else should have a good time.',
'qty_inner' => '100',
'qty_masterCarton' => '200',
'qty_pallet' => '300',
'averageMonthlyUnitSales' => '100',
'created_at' => '2022-03-13T04:14:12.11.093745 +00:00'
],
];
I can perform the first part of the assignment (filter for averageMonthlyUnitSales>350) by carrying out something like:
$filtered_array= array_filter($data, function($item){
return ($item['averageMonthlyUnitSales']>350);
});
But I am not too sure how I can go about getting a new array in the required format?
The only way I can see to simply do both is as follows.
$data = [
[
'id' => '9947',
'sku' => 'test_1',
'asin_code' => '50',
'asin_value' => '',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '120.50',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => '',
'qty_inner' => '120',
'qty_masterCarton' => '200',
'qty_pallet' => '1200',
'averageMonthlyUnitSales' => '750',
'created_at' => '2019-02-23T01:54:14.957299+00:00'
],
[
'id' => '10921',
'sku' => 'test_2',
'asin_code' => '50',
'asin_value' => 'bx12345',
'barcode' => '10',
'barcode_value' => 'jb170931',
'commodityCode' => '9989898889',
'price' => '20.59',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'It\'s only half completed, I\'m afraid',
'qty_inner' => '70',
'qty_masterCarton' => '250',
'qty_pallet' => '270',
'averageMonthlyUnitSales' => '120',
'created_at' => '2021-12-23T11:41:31.193982+00:00'
],
[
'id' => '11102',
'sku' => 'test_3',
'asin_code' => '50',
'asin_value' => 'jl1235',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '145.99',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'I\'ve just had an unhappy love affair, so I don\'t see why anybody else should have a good time.',
'qty_inner' => '100',
'qty_masterCarton' => '200',
'qty_pallet' => '300',
'averageMonthlyUnitSales' => '100',
'created_at' => '2022-03-13T04:14:12.11.093745 +00:00'
],
];
$new = [];
foreach ($data as $line){
if ( $line['averageMonthlyUnitSales'] > 350 ){
// reformat the array
$new = [
'id' => $line['id'],
'sku' => $line['sku'],
'alternates' => ['asin' => ['code'=>$line['asin_code'], 'value'=>$line['asin_value'] ],
'barcode' => ['code'=> $line['barcode'], 'value' => $line['barcode_value']]
],
'commodityCode' => $line['commodityCode'],
'price' => [ 'value' => $line['price'] ],
'instructions' => ['picking' => ['code' => $line['picking_instruction_code'], 'value'=> $line['picking_instruction_value']]
],
'quantity' =>['inner' =>$line['qty_inner'], 'masterCarton' =>$line['qty_masterCarton'], 'pallet'=>$line['qty_pallet']],
'averageMonthlyUnitSales'=>$line['averageMonthlyUnitSales'],
'created_at'=>$line['averageMonthlyUnitSales']
];
}
}
print_r($new);
RESULT
PHP 8.1.3
Array
(
[id] => 9947
[sku] => test_1
[alternates] => Array
(
[asin] => Array
(
[code] => 50
[value] =>
)
[barcode] => Array
(
[code] => 10
[value] => js160694
)
)
[commodityCode] => 9989898889
[price] => Array
(
[value] => 120.50
)
[instructions] => Array
(
[picking] => Array
(
[code] => PICK
[value] =>
)
)
[quantity] => Array
(
[inner] => 120
[masterCarton] => 200
[pallet] => 1200
)
[averageMonthlyUnitSales] => 750
[created_at] => 750
)

php Two-dimensional arrays are sorted and grouped according to the value of the key

I have a two-dimensional array that needs to be grouped according to assemble_id ,I can't get the result I want by traversing. Below is the original array:
$list = [
0 =>[
'nickname' => 'Bob',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1
],
1 =>[
'nickname' => 'Jack',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1
],
2 =>[
'nickname' => 'Grace',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1
],
3 =>[
'nickname' => 'Jelly',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1
]
];
What i need is:
[
[
'assemble_id' => 'c1d0zUbmP',
'assemble_group' => [
[
'nickname' => 'Bob',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1
],
[
'nickname' => 'Grace',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1
]
]
],
[
'assemble_id' => 'ED6OJX4VV',
'assemble_group' => [
[
'nickname' => 'Jack',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1
],
[
'nickname' => 'Jack',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1
]
]
]
];
I need to group according to the same assemble_id and put is_group=1 in the first of each group.
hery is my code:
function formatData($list)
{
$data = [];
foreach ($list as $k=>$v) {
$list[$k]['created_at'] = formatDate($v['created_at'],'Y-m-d H:i');
$list[$k]['phone'] = setMobile($v['phone']);
$data[$k]['assemble_id'] = $v['assemble_id'];
$data[$v['assemble_id']][] = $list[$k];
}
return $data;
}
This did not get the results I wanted.
Assign temporary associative keys, then remove them when finished looping.
Code: (Demo)
foreach ($list as $row) {
$result[$row['assemble_id']]['assemble_id'] = $row['assemble_id'];
$result[$row['assemble_id']]['assemble_group'][] = [
'nickname' => $row['nickname'],
'phone' => $row['phone'], // add your function here
'is_group' => $row['is_group'],
'created_at' => $row['created_at'], // add your function here
'assemble_id' => $row['assemble_id'],
'status' => $row['status']
];
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'assemble_id' => 'c1d0zUbmP',
'assemble_group' =>
array (
0 =>
array (
'nickname' => 'Bob',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1,
),
1 =>
array (
'nickname' => 'Grace',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1,
),
),
),
1 =>
array (
'assemble_id' => 'ED6OJX4VV',
'assemble_group' =>
array (
0 =>
array (
'nickname' => 'Jack',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1,
),
1 =>
array (
'nickname' => 'Jelly',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1,
),
),
),
)

How to make this multidimensional array into three specified parts?

I have this array
$data = [
0 => [
'id' => '114',
'organization_name' => 'ABC Ltd',
'organization_telephone' => '01234 112233',
'organization_email' => 'admin#example.com',
'organization_url' => 'http://www.example.com',
'order_id' => '119',
'order_delivery_address_1' => '55',
'order_delivery_address_2' => 'High Street',
'order_delivery_address_3' => '',
'order_delivery_postcode' => 'LL27 0YX',
'product_colour_name' => 'Red',
'product_size_name' => '8/9',
'product_price' => '7.5',
'product_quantity' => '10',
'product_line_price' => '75',
],
];
And I want to divide it into 3 arrays as below:
$orgnization_details = [
'id' => '114',
'organization_name' => 'ABC Ltd',
'organization_telephone' => '01234 112233',
'organization_email' => 'admin#example.com',
'organization_url' => 'http://www.example.com',
]
$order_details = [
'order_id' => '119',
'order_delivery_address_1' => '55',
'order_delivery_address_2' => 'High Street',
'order_delivery_address_3' => '',
'order_delivery_postcode' => 'LL27 0YX',
]
$product_details = [
'product_colour_name' => 'Red',
'product_size_name' => '8/9',
'product_price' => '7.5',
'product_quantity' => '10',
'product_line_price' => '75',
]
I tried using array_filter for orgnization_details as below
foreach ($data as $details)
{
$orgnization_details = array_filter($details, function($key) { return $key <= 'organization_url'; }, ARRAY_FILTER_USE_KEY);
}
But it didn't work as expected.
Please help me with this.
You are very close.
The array_filter() needs to be modified to check if the beginning of the key is a specific string:
$orgnization_details = array_filter($details, function($key){
// do the first 13 chars equal "organization_" or is the key "id"?
return substr( $key, 0, 13 ) === 'organization_' || $key === 'id';
}, ARRAY_FILTER_USE_KEY);
// Do similar logic for setting $order_details
// Do similar logic for setting $product_details
You could define the keys and check for an intersection of the keys in the main array:
$orginazation_keys = array_flip(['id',
'organization_name',
'organization_telephone',
'organization_email',
'organization_url' ]);
$orgnization_details = array_intersect_key($data[0], $orginazation_keys);
Or you can grep for them since they follow a pattern:
$orgnization_details = array_intersect_key($data[0],
array_flip(preg_grep('/^(organization|id)/', array_keys($data[0]))));
<?php
$data = [
0 => [
'id' => '114',
'organization_name' => 'ABC Ltd',
'organization_telephone' => '01234 112233',
'organization_email' => 'admin#example.com',
'organization_url' => 'http://www.example.com',
'order_id' => '119',
'order_delivery_address_1' => '55',
'order_delivery_address_2' => 'High Street',
'order_delivery_address_3' => '',
'order_delivery_postcode' => 'LL27 0YX',
'product_colour_name' => 'Red',
'product_size_name' => '8/9',
'product_price' => '7.5',
'product_quantity' => '10',
'product_line_price' => '75',
],
];
$prefix_map = [
'id' => 'organization_details',
'organization' => 'organization_details',
'order' => 'order_details',
'product' => 'product_details'
];
foreach($data[0] as $k => $v) {
$key_prefix = explode('_', $k)[0];
$new_key = $prefix_map[$key_prefix];
$out[$new_key][$k] = $v;
}
extract($out);
var_export($organization_details);
Output:
array (
'id' => '114',
'organization_name' => 'ABC Ltd',
'organization_telephone' => '01234 112233',
'organization_email' => 'admin#example.com',
'organization_url' => 'http://www.example.com',
)
This creates a new multi-dimensional array with the corresponding associated keys by mapping the existing key prefixes (part before the underscore). Then it's just a case of using extract on that array to create the named variables.
This results in the three variables: $organization_details, $order_details and $product_details that you desire.
If your sub-arrays are consistently in the same order you can simply slice:
<?php
$data = [
0 => [
'id' => '114',
'organization_name' => 'ABC Ltd',
'organization_telephone' => '01234 112233',
'organization_email' => 'admin#example.com',
'organization_url' => 'http://www.example.com',
'order_id' => '119',
'order_delivery_address_1' => '55',
'order_delivery_address_2' => 'High Street',
'order_delivery_address_3' => '',
'order_delivery_postcode' => 'LL27 0YX',
'product_colour_name' => 'Red',
'product_size_name' => '8/9',
'product_price' => '7.5',
'product_quantity' => '10',
'product_line_price' => '75',
]
];
$first = $data[0];
$organisation_details = array_slice($first, 0, 5);
$order_details = array_slice($first, 5, 5);
$product_details = array_slice($first, -5);
var_export($organisation_details);
var_export($order_details);
var_export($product_details);
Output:
array (
'id' => '114',
'organization_name' => 'ABC Ltd',
'organization_telephone' => '01234 112233',
'organization_email' => 'admin#example.com',
'organization_url' => 'http://www.example.com',
)array (
'order_id' => '119',
'order_delivery_address_1' => '55',
'order_delivery_address_2' => 'High Street',
'order_delivery_address_3' => '',
'order_delivery_postcode' => 'LL27 0YX',
)array (
'product_colour_name' => 'Red',
'product_size_name' => '8/9',
'product_price' => '7.5',
'product_quantity' => '10',
'product_line_price' => '75',
)

Calculate percentage for each row between a column value and the sum of the entire column

I want to calculate the percentage for each row in an array based on a specific column value and the total of all values in that column.
Sample data:
$results = [
['type' => 'AA', 'count' => 4],
['type' => 'AE', 'count' => 59],
['type' => 'AF', 'count' => 13],
['type' => 'BB', 'count' => 44],
['type' => 'BC', 'count' => 16],
['type' => 'BD', 'count' => 36]
];
Desired result:
[
['type' => 'AA', 'count' => 4, 'percent' => '2%'],
['type' => 'AE', 'count' => 59, 'percent' => '34%'],
['type' => 'AF', 'count' => 13, 'percent' => '8%'],
['type' => 'BB', 'count' => 44, 'percent' => '26%'],
['type' => 'BC', 'count' => 16, 'percent' => '9%'],
['type' => 'BD', 'count' => 36, 'percent' => '21%'],
]
My code:
foreach($results as $row) {
$count = $row['count'];
$type = $row['type'];
$array[$type][] = $count;
}
Before performing the arithmetic on each row, you'll need to sum the count values for all rows (for your sample data, the total is 172).
Then loop over the rows, calculate the percentage and push the new associative rows into the new array.
Code: (Demo)
$results = [
['type' => 'AA', 'count' => 4],
['type' => 'AE', 'count' => 59],
['type' => 'AF', 'count' => 13],
['type' => 'BB', 'count' => 44],
['type' => 'BC', 'count' => 16],
['type' => 'BD', 'count' => 36]
];
$total = array_sum(
array_column($results, 'count')
); // 172
foreach($results as &$row) {
$row['percent'] = round($row['count'] / $total * 100, 0) . '%';
}
var_export($results);
Or instead of the classic loop, you can use a modern arrow function inside of array_map() to merge the new column into each row.
var_export(
array_map(
fn($row) => $row + ['percent' => round($row['count'] / $total * 100, 0) . '%'],
$results
)
);
Output (from either approach):
array (
0 =>
array (
'type' => 'AA',
'count' => 4,
'percent' => '2%',
),
1 =>
array (
'type' => 'AE',
'count' => 59,
'percent' => '34%',
),
2 =>
array (
'type' => 'AF',
'count' => 13,
'percent' => '8%',
),
3 =>
array (
'type' => 'BB',
'count' => 44,
'percent' => '26%',
),
4 =>
array (
'type' => 'BC',
'count' => 16,
'percent' => '9%',
),
5 =>
array (
'type' => 'BD',
'count' => 36,
'percent' => '21%',
),
)

PHP error : exception 'ErrorException' with message 'Illegal string offset 'id''

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).

Categories