Yii2 select2 selected one value 2 and more time - php

I have select2
return $this->form->field($this->model, 'observers')
->widget(Select2::className(),
[
'data' => Tasks::getAllStaffsGroupOffice(),
'disabled' => !$this->can['changeObservers'],
'options' => [
'multiple' => true,
'value' => ArrayHelper::map($this->model->observers, 'staff_id', 'staff_id'),
'placeholder' => Yii::t('tasks_forms', 'FORM_PLACEHOLDER_CHOOSE'),
'class' => 'hiddenInput'
],
'pluginOptions' => [
'allowClear' => true,
'closeOnSelect'=> false,
],
'pluginLoading' => false,
]);
Tasks::getAllStaffsGroupOffice() geting users array by offices. Example ->
array:4 [▼
"main office" => array:1 [▼
2 => "123 123"
]
"office 1" => array:3 [▼
3 => "staff_1"
6 => "staff_2"
2 => "123 123"
]
"office 3" => array:2 [▼
4 => "staff_3"
3 => "staff_1"
]
"office 2" => array:2 [▼
5 => "staff_4"
3 => "staff_1"
]
]
select2 value example -> array (2 => "2")
As a result, the display of the widget itself looks like this
select2 value
How to make it so that the staff which is in 2 and more offices is displayed only 1 time?

I believe you should filter your staff list and then pass it to Select2 widget. My approach for creating a unique array of staff is like this:
$allStaffsGroupOffice = [
"main office" => [
2 => "123 123"
],
"office 1" => [
3 => "staff_1",
6 => "staff_2",
2 => "123 123"
],
"office 3" => [
4 => "staff_3",
3 => "staff_1"
],
"office 2" => [
5 => "staff_4",
3 => "staff_1"
]
];
$repeatedStaff = [];
$newUniqueList = [];
foreach ($allStaffsGroupOffice as $office => $staffList) {
foreach ($staffList as $staffId => $staffName) {
if (!in_array($staffId, $repeatedStaff)) {
$repeatedStaff[] = $staffId;
$newUniqueList[$office][$staffId] = $staffName;
}
}
}
$newUniqueList will be your new array which contains offices with unique staff.

Related

Advanced Pricing for Shopware 6 (KeyValuePair Type error)

I am trying to update product price with custom rule (advanced price) programmatically in Shopware 6
I have the following array
array:2 [
0 => array:3 [
"productNumber" => "Test1"
"prices" => array:4 [
"ruleId" => "cfda4784f29b40428c881e3a1878ad69"
"quantityStart" => 1
"price" => array:1 [
0 => array:4 [
"currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca"
"gross" => 2.0
"net" => 1.0
"linked" => true
]
]
]
//This is the product ID
"id" => "dc00dd078e4949bc9aadb14e62cf6924"
]
1 => array:3 [
"productNumber" => "Test2"
"prices" => array:4 [
"ruleId" => "cfda4784f29b40428c881e3a1878ad69"
"quantityStart" => 1
"price" => array:1 [
0 => array:4 [
"currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca"
"gross" => 2.0
"net" => 1.0
"linked" => true
]
]
]
//This is the product ID
"id" => "2324be8b90a54524914b86bc8f0521e4"
]
]
As soon as I run the custom CLI command, I get this error:
[TypeError]
Shopware\Core\Framework\DataAbstractionLayer\Write\DataStack\KeyValuePair::__construct(): Argument #1 ($key) must be of type string, int given, cal
led in /var/www/html/vendor/shopware/core/Framework/DataAbstractionLayer/Write/DataStack/DataStack.php on line 47
I’ve tried to insert to product_price table instead of updating the product. Same thing happened.
prices has to be a list containing associative arrays.
[
// ...
'prices' => [
[
'ruleId' => 'cfda4784f29b40428c881e3a1878ad69',
'quantityStart' => 1,
'price' => [
[
'currencyId' => 'b7d2554b0ce847cd82f3ac9bd1c0dfca',
'gross' => 2.0,
'net' => 1.0,
'linked' => true,
],
],
],
],
]

Not able to group inner array of the multi-dimensional array

I have an array like this & I need to group an array based on value.
array:3 [
0 => array:5 [
"id" => 1
"maxView" => 0
"maxClick" => 20
"companyName" => "Project A"
"email" => "user1#email.com"
]
1 => array:5 [
"id" => 1
"maxView" => 0
"maxClick" => 20
"companyName" => "Project A"
"email" => "user2#email.com"
]
2 => array:5 [
"id" => 1
"maxView" => 0
"maxClick" => 20
"companyName" => "Project A"
"email" => "user3#email.com"
]
3 => array:5 [
"id" => 1
"maxView" => 0
"maxClick" => 20
"companyName" => "Project B"
"email" => "user1#email.com"
]
4 => array:5 [
"id" => 1
"maxView" => 0
"maxClick" => 20
"companyName" => "Project B"
"email" => "user6#email.com"
]
]
So far, I have following lines of codes. This is grouping my array based on fieldName = companyName But I am not able to add emails belonging to companyname under an array.
foreach ($records as $record) {
if (!array_key_exists($record[$fieldName], $groupedArray)) {
$groupedArray[$record[$fieldName]] = [];
}
$groupedArray[$record[$fieldName]] = $record;
$groupedArray[$record[$fieldName]]['email'] = [];
if (!in_array($record['email'], $groupedArray[$record[$fieldName]]['email'], true)) {
$groupedArray[$record[$fieldName]]['email'][] = $record['email'];
}
}
My code is adding only last email to the array.
"Project A" => [
....
"email" => array:1 [
0 => "user3#email.com"
]
Can anybody help me what I am missing here ?

How to get required data from this array

I am using Laravel. I have a complex array and i want to make it more readable and I am not sure how to convert it into more readable form and i want to pass only the required information. this is loop
foreach($dataArray as $value){
$addressArray[] = [
'id' => $value['place_id'],
'address'=> $value['terms'],
];
}
dd($addressArray);
}
and this array returns response in following format and I want to format this array
array:5 [
0 => array:4 [
"id" => "ChIJH6WK3tQ0K4gRm0XKDAjRGfA"
"label" => "12 York Street, Toronto, ON, Canada"
"value" => "12 York Street, Toronto, ON, Canada"
"address" => array:5 [
0 => array:2 [
"offset" => 0
"value" => "12"
]
1 => array:2 [
"offset" => 3
"value" => "York Street"
]
2 => array:2 [
"offset" => 16
"value" => "Toronto"
]
3 => array:2 [
"offset" => 25
"value" => "ON"
]
4 => array:2 [
"offset" => 29
"value" => "Canada"
]
]
]
]
2 => array:4 [
"id" => "ChIJJa4u6nqipBIRthG2uMmbv2M"
"label" => "Avinguda de Rius i Taulet, 12, Barcelona, Spain"
"value" => "Avinguda de Rius i Taulet, 12, Barcelona, Spain"
"address" => array:4 [
0 => array:2 [
"offset" => 0
"value" => "Avinguda de Rius i Taulet"
]
1 => array:2 [
"offset" => 27
"value" => "12"
]
2 => array:2 [
"offset" => 31
"value" => "Barcelona"
]
3 => array:2 [
"offset" => 42
"value" => "Spain"
]
]
]
3 => array:4 [
"id" => "ChIJUXFxe6qMwokRT2J7mbHQE3o"
"label" => "1250 Waters Place, Bronx, NY, USA"
"value" => "1250 Waters Place, Bronx, NY, USA"
"address" => array:5 [
0 => array:2 [
"offset" => 0
"value" => "1250"
]
1 => array:2 [
"offset" => 5
"value" => "Waters Place"
]
2 => array:2 [
"offset" => 19
"value" => "Bronx"
]
3 => array:2 [
"offset" => 26
"value" => "NY"
]
4 => array:2 [
"offset" => 30
"value" => "USA"
]
]
]
4 => array:4 [
"id" => "ChIJfz6STcJYwokRFONBxt9Ytvs"
"label" => "1275 York Avenue, New York, NY, USA"
"value" => "1275 York Avenue, New York, NY, USA"
"address" => array:5 [
0 => array:2 [
"offset" => 0
"value" => "1275"
]
1 => array:2 [
"offset" => 5
"value" => "York Avenue"
]
2 => array:2 [
"offset" => 18
"value" => "New York"
]
3 => array:2 [
"offset" => 28
"value" => "NY"
]
4 => array:2 [
"offset" => 32
"value" => "USA"
]
]
]
]
However I want to format data in this format.
array:5 [
0 => array:4 [
"id" => "ChIJH6WK3tQ0K4gRm0XKDAjRGfA"
"label" => "12 York Street, Toronto, ON, Canada"
"value" => "12 York Street, Toronto, ON, Canada"
"address" => [
'number' => 12,//from address array, index 0 value
'street'=>'York Street',//from address array, index 1 value
'city'=>'Tornonto,//from address array, index 2 value
'state=>'ON',//from address array, index 3 value
'country'=>'Canada'//from address array, index 4 value
];
}
Any help? Thanks
You can set the value by referencing each index of the terms key manually
$number = '';
$street = '';
$city = '';
$state = '';
$country = '';
if (isset($value['terms'][0]['value']))
$number = $value['terms'][0]['value'];
if (isset($value['terms'][1]['value']))
$street = $value['terms'][1]['value'];
if (isset($value['terms'][2]['value']))
$city = $value['terms'][2]['value'];
if (isset($value['terms'][3]['value']))
$state = $value['terms'][3]['value'];
if (isset($value['terms'][4]['value']))
$country = $value['terms'][4]['value'];
$addressArray[] = [
'id' => $value['place_id'],
'label' => $value['description'],
'value' => $value['description'],
'address' => [
'number' => $number,
'street' => $street,
'city' => $city,
'state' => $state,
'country' => $country
]
];

Removed array grouped key

Is it possible to remove the key after data is grouped? Now I'm working with the csv file import function. I have two rows of data with the same student but different books because I want to group the books based on the student ID.
Code
foreach ($book as $item) {
$item['books'] = [
'number' => $item['number'],
'description' => $item['description'],
];
if (!isset($group_by[$item['student_id']])) {
$group_by[$item['student_id']] = array(
'student_id' => $item['student_id'],
'name' => $item['name'],
);
}
$group_by[$item['student_id']]['books'][] = $item['books'];
}
This is my grouping function, it worked perfectly as expected but the data structure is not matched to my system
Result is here
array:1 [
"ST001" => array:9 [
"student_id" => "ST001"
"name" => "joe"
"books" => array:2 [
0 => array:2 [
"number" => "1"
"description" => "The magic"
]
1 => array:2 [
"number" => "2"
"description" => "Forest in norway"
]
]
]
]
Expect result
array:9 [
"student_id" => "ST001"
"name" => "joe"
"books" => array:2 [
0 => array:2 [
"number" => "1"
"description" => "The magic"
]
1 => array:2 [
"number" => "2"
"description" => "Forest in norway"
]
]
]
I have no idea how to remove the St001 array. I did tried this $group_by[] = array( but the books data will be break out of data structure.
Assign the inner array to another array.
$array = $array['ST001'];
It will help out here
foreach($book as $item)
{
$item['books'] = ['number' => $item['number'], 'description' => $item['description'], ];
if (!isset($group_by[$item['student_id']]))
{
$group_by[$item['student_id']] = array(
'student_id' => $item['student_id'],
'name' => $item['name'],
);
}
$group_by[$item['student_id']]['books'][] = $item['books'];
}
// remove key and get only value as you want
print_r(array_values($group_by));

ResultSetMapping addScalarResult associative array

In my project with Symfony2, I'm using Doctrine createNativeQuery, and I would like to get an associative array.
This is my code
$rsm = new ResultSetMapping;
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('name', 'name');
$rsm->addScalarResult('phone_one', 'phoneOne');
$rsm->addScalarResult('phone_two', 'phoneTwo');
I have this result:
array:2 [
0 => array:4 [
"id" => "975"
"name" => "one name"
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
1 => array:4 [
0 => array:4 [
"id" => "976"
"name" => "two name"
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
]
It's posible this result?
array:2 [
0 => array:4 [
"id" => "975"
"name" => "one name"
"phones" => [
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
]
1 => array:4 [
0 => array:4 [
"id" => "976"
"name" => "two name"
"phones" => [
"phoneOne" => "122345556"
"phoneTwo" => "345566789"
]
]
]
Thanks a lot
If that is the result you want, why not create a OneToMany for Users to Phones? I would highly suggest you do that.

Categories