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));
Related
Im my laravel project I have this nested array that needs to be updated (transformed - for the frontend):
array:2 [
0 => array:3 [
"entity_id" => 931
"entity" => "user"
"children" => array:2 [
0 => array:2 [
"entity_id" => 5
"entity" => "location"
]
1 => array:2 [
"entity_id" => 932
"entity" => "user"
]
]
]
1 => array:2 [
"entity_id" => 486
"entity" => "user"
]
]
EXPECTED ARRAY:
array:2 [
0 => array:4 [
"id" => 931
"text" => "Test User 1"
"type" => "user"
"children" => array:2 [
0 => array:3 [
"id" => 5
"text" => "Location 1"
"type" => "location"
]
1 => array:3 [
"id" => 932
"text" => "Test User 2"
"type" => "user"
]
]
]
1 => array:3 [
"id" => 486
"text" => "Test User 3"
"type" => "user"
]
]
I have created a recursive function for this job
public function getNewStructure($tree, &$output) {
foreach($tree as $data) {
$output[] = array(
'id' => $data['entity_id'],
'text' => $data['user'] === 'user' ? User::find($data['entity_id'])->name : Location::find($data['entity_id'])->name,
'type' => $data['entity']
);
$this->getNewStructure($data['children'] ?? [], $output);
}
return $output;
}
but it not returns as expected:
array:4 [
0 => array:3 [
"id" => 931
"text" => "Test User 1"
"type" => "user"
]
0 => array:3 [
"id" => 5
"text" => "Location 1"
"type" => "location"
]
1 => array:3 [
"id" => 932
"text" => "Test User 2"
"type" => "user"
]
1 => array:3 [
"id" => 486
"text" => "Test User 3"
"type" => "user"
]
]
How can I add the children to the $output array in the recursive function ???
I have tried by adding the children key:
$this->getNewStructure($data['children'] ?? [], $output['children']);
as when iterating again it will push the current array in the right place.... but is not working...
I fixed
public function getNewStructure($tree, &$output) {
foreach($tree as $data) {
$element = array(
'id' => $data['entity_id'],
'text' => $data['user'] === 'user' ? User::find($data['entity_id'])->name : Location::find($data['entity_id'])->name,
'type' => $data['entity']
);
if(is_array($data['children']) && count($data['children']) > 0) {
$element['children'] = $this->getNewStructure($data['children'], $element['children']);
}
$output[] = $element;
}
return $output;
}
I have an API request like this:
$postInformation =
(new postInformation)->handle(['field' => 'slug', 'value' => $this->slug]);
A dump of this response, besides other things, show some categories that are coming from this API request. And they are on this format where there is a 'categories' key with the top categories:
categories:
^ array:40 [
"id" => 2
...
"categories" => array:2 [
"data" => array:15 [
0 => array:3 [
"id" => 6
"name" => array:1 [
"en" => "General"
]
"on_request" => 0
]
1 => array:3 [
"id" => 14
"name" => array:1 [
"en" => "Tuts"
]
"on_request" => 0
]
2 => array:3 [
"id" => 3
"name" => array:1 [
"en" => "Laravel"
]
"on_request" => 0
]
...
]
...
]
I created a table 'post_top_categories' and a model PostTopCategory and I want to get the top categories from the API response above and store them into the 'post_top_categories' table. But I'm not understanding how to properly achieve this. Do you know how it can be achieved it? Thanks
foreach($yourArray['categories']['data'] as $topCategory)
{
$catId = $topCategory['id'];
$catname = $topCategory['name']['en'];
$catOnRequest = $topCategory['on_request'];
// Do what you want with those values now
}
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.
I'm very new in PHP. I'm doing with the array data structure. Currently, I got two result which is the same ID but different details. I need to combine the details based on the ID.
This is my array result
array:2 [
0 => array:13 [
"id" => "1"
"date" => "01-02-2019"
"order" => array:1 [
0 => array:2 [
"code" => "F001"
"name" => "fish"
]
]
]
1 => array:13 [
"id" => "1"
"date" => "01-02-2019"
"order" => array:1 [
0 => array:2 [
"code" => "F002"
"name" => "chicken"
]
]
]
]
This is my expected result
array:1 [
0 => array:13 [
"id" => "1"
"date" => "01-02-2019"
"order" => array:2 [
0 => array:2 [
"code" => "F001"
"name" => "fish"
]
1 => array:2 [
"code" => "F002"
"name" => "chicken"
]
]
]
]
I'm trying to insert the order array if there have the same id
This is my code
public function order($value)
{
$group_id = [];
$groupItem = [];
foreach ($value as $item) {
$item['date'] = date('Y-m-d',strtotime($item['date']));
$groupItem = $item['order'][] = [
'id' => $item['id'],
'name' => $item['name'],
];
$group_id[$item['id']][]= $groupItem;
}
}
Result
array:1 [
"1" => array:1 [
0 => array:2 [
"code" => "F001"
"name" => "fish"
]
1 => array:2 [
"code" => "F002"
"name" => "chicken"
]
]
]
As you can see, I grouped the order but the date and id were missing.
Here is a working order function:
function order($input) {
$output = array();
foreach($input as $item) {
if (!isset($output[$item['id']])) {
$output[$item['id']] = array(
"id" => $item["id"],
"date" => $item["date"],
"order" => array()
);
}
$output[$item['id']]['order'][] = $item['order'];
}
return array_values($output);
}
INPUT:
array(
array(
"id" => "1",
"date" => "01-02-2019",
"order" => array(
array(
"code" => "F001",
"name" => "fish"
)
)
),
array(
"id" => "1",
"date" => "01-02-2019",
"order" => array(
array(
"code" => "F002",
"name" => "chicken"
)
)
)
);
OUTPUT:
array(
array(
"id"=> "1",
"date"=> "01-02-2019",
"order"=> array(
array(
array(
"code"=> "F001",
"name"=> "fish"
)
),
array(
array(
"code"=> "F002",
"name"=> "chicken"
)
)
)
)
)
Assign the proper data. Try -
foreach ($value as $item) {
$date = date('Y-m-d',strtotime($item['date']));
$id = $item['id'];
$group_id[$id]['id'] = $id;
$group_id[$id]['date'] = $date;
$group_id[$id]['order'][] = [
'id' => $item['id'],
'name' => $item['name'],
];
}
I would write something like this, if you need to account for multiple IDs:
$result = array_reduce( $input, function ( $result, $item ) {
$id = $item['id'];
if ( ! isset( $result[ $id ] ) ) { // We don't have an item yet.
$result[ $id ] = $item;
} else { // We have an item and need to add orders to it.
$result[ $id ]['order'] = array_merge( $result[ $id ]['order'], $item['order'] );
}
return $result;
}, [] );
I have simple code that tries to update data in MongoDB:
$this->collection->update(
["_id" => new \MongoId($id)],
['$set' => ['value_default' => $data]]);
It involks an error:
localhost:27017: Found $id field without a $ref before it, which is invalid.
Where $data is array. And $id is string id of primary key.
Structure of document
Before update I tried to get document by id:
$cursor= $this->collection->find(array("_id" => new \MongoID($id)));
It returns me an object.
I try to add $data thta looks as:
array:2 [
0 => array:8 [
"id" => "594146d2b3569eb653851895"
"unique_id" => array:1 [
"$id" => "5954d6dfb3569ef41f7f42cf"
]
"type_value" => "float"
"prefix" => "probability"
"title" => "Вероятность"
"type" => "1"
"value" => "1"
"nodes" => []
]
1 => array:8 [
"id" => "5941499fb3569e6454851896"
"unique_id" => array:1 [
"$id" => "5954d6dfb3569ef41f7f42d0"
]
"type_value" => "float"
"prefix" => "playerArc"
"title" => "Арка игрока"
"type" => "1"
"value" => "2"
"nodes" => []
]
]