Laravel/PHP flatten multidimensional array - php

I'm wondering if someone can help with the following please? Currently I have this array:
array:1 [
"blocks" => array:3 [
0 => array:1 [
"component" => "TextColumns"
]
1 => array:2 [
0 => array:1 [
"component" => "TextColumns"
]
1 => array:1 [
"component" => "TextColumns"
]
]
2 => array:1 [
"component" => "TextColumns"
]
]
]
What I want to achieve is a final array of the below:
array:1 [
"blocks" => array:3 [
0 => array:1 [
"component" => "TextColumns"
]
1 => array:1 [
"component" => "TextColumns"
]
2 => array:1 [
"component" => "TextColumns"
]
3 => array:1 [
"component" => "TextColumns"
]
]
]
If I use array_flatten or laravels ->flatten() I do not get the desired results as it flattens it too much:
array:1 [
"blocks" => array:3 [
0 => array:1 [
"component" => "TextColumns"
]
1 => array:2 [
0 => "TextColumns"
1 => "TextColumns"
]
2 => array:1 [
"component" => "TextColumns"
]
]
]
EDIT: Here is some source code for reference. I have header, content and footer methods. Both header and footer will always return a single array. Content however will be a multidimensional array as there could be multiple content blocks i.e.
protected static function header(): array
{
return [
'component' => 'TextColumns',
];
}
protected static function content(): array
{
return [
[
'component' => 'TextColumns',
],
[
'component' => 'TextColumns',
],
];
}
Then this is all merged together in the following array. Hence I need the content blocks to be flattened so that the blocks array just contains a simple array
dd([
'blocks' => [
static::header(),
static::content(),
static::footer(),
],
]);
Any help would be appreciated!
Thanks

Related

Transform Laravel collection and group by key

I would like to transform a collection of object in laravel, but I can't figure it out how. I want to group the record by the type column, and set the grouped record under an array key value.
public function transformData()
{
$data = collect();
$matches = [ 'race', 'ethnicity'];
$attributes = Attribute::withTrashed()
->whereIn('type', $matches)
->get()
->groupBy('type');
$data->put(
$type, // to be the group key(like race)
[
'values' => $attributes->toArray(), // to be all the value with type race
]
);
dd(collect($data));
}
Here is what I'm getting:
Illuminate\Support\Collection^ {#2368
#items: array:1 [
"values" => array:2 [
"ethnicity" => array:2 [
0 => array:2 [
"id" => 1
"type" => "ethnicity"
]
1 => array:2 [
"id" => 2
"type" => "ethnicity"
]
]
"race" => array:2 [
0 => array:2 [
"id" => 6
"type" => "race"
]
1 => array:2 [
"id" => 7
"type" => "race"
]
]
]
]
#escapeWhenCastingToString: false
}
BUT I would like to return this instead:
Illuminate\Support\Collection^ {#2368
#items: array:1 [
"ethnicity" => array:2 [
"value" => array:2 [
0 => array:2 [
"id" => 1
"type" => "ethnicity"
]
1 => array:2 [
"id" => 2
"type" => "ethnicity"
]
]
]
"race" => array:2 [
"value" =>
0 => array:2 [
"id" => 6
"type" => "race"
]
1 => array:2 [
"id" => 7
"type" => "race"
]
]
]
#escapeWhenCastingToString: false
}

Store top categories from an API request into the database

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
}

How to get value from unserialized data

I'm transforming my data into an array structure. Now I have the meta which already unserialized and I just need to take a specific value in meta.
Code
$item->meta;
$itm['attrs'] = #unserialize($item->meta);
$serial = #unserialize($item->meta);
$itm['product']['attrs'] = $itm['attrs'];
$itm['product']['serial'] = $serial['serial'];
Result
"product" => array:2 [
"attrs" => array:1 [
"serial" => array:1 [
0 => array:3 [
"id" => 848
"text" => "12345wf"
"trade_in" => 0
]
]
]
"serial" => array:1 [
0 => array:3 [
"id" => 848
"text" => "12345wf"
"trade_in" => 0
]
]
]
Expected Result
"product" => array:2 [
"attrs" => array:1 [
"serial" => array:1 [
0 => array:3 [
"id" => 848
"text" => "12345wf"
"trade_in" => 0
]
]
]
"serial" => "12345wf"
]
I'm not sure how to get the value and pass to the object.

How to merge two arrays?

After loop made and using following array I can create a new array.
$data[$val['gid']][$val['rid']][$val['aid']][$teno][$userid]= array();
This results in the array below:
array:1 [
"FS OTHER" => array:1 [
"FS OTHER" => array:1 [
"FS OTHER" => array:1 [
"FS OTHER" => array:1 [
"D111" => []
]
]
]
]
]
I also have another array:
array:41 [
0 => array:2 [
"sid" => "D111"
"desc1" => "BANGKOK"
]
1 => array:2 [
"sid" => "D111"
"desc1" => "NONTHABURI"
]
2 => array:2 [
"sid" => "D112"
"desc1" => "PATHUM THANI"
]
Now I need to merge this array based on the 'sid' to get the following result:
array:1 [
"FS OTHER" => array:1 [
"FS OTHER" => array:1 [
"FS OTHER" => array:1 [
"FS OTHER" => array:1 [
"BANGKOK" => []
"NONTHABURI"=> []
]
]
]
]
]
You can prepare another structure of the second array
$new = [];
foreach ($arr2 as $x) {
$new[$x['sid']][$x['desc1']] = [];
}
// [D111 => [ BANGKOK => [], NONTHABURI => [] ],..
and then just create the 1st one by:
$data[$val['gid']][$val['rid']][$val['aid']][$teno][$userid]= $new[$userid];

I have an array with multi-dimension, I want to get the name of the array and the value of array

Problem in splitting array in PHP.
I need pradm_policy_risk_attr_details_motor_id => 20170825113749907.
but in array only. Below is my example how to I needed in array
$array = [pradm_policy_risk_attr_details_motor_id => 20170825113749907,
column_11 => BP-2-B1534,
column_14 => Mahindra];
How can get this?
This is my array
array:19 [
"pradm_policy_risk_attr_details_motor_id" => array:1 [
0 => "20170825113749907"
]
"column_11" => array:1 [
0 => "BP-2-B1534"
]
"column_14" => array:1 [
0 => "Mahindra"
]
"column_15" => array:1 [
0 => "Bolero-Camper 2WD (2523 cc)"
]
"column_61" => array:1 [
0 => ""
]
"column_92" => array:1 [
0 => "0.000000"
]
"column_28" => array:1 [
0 => "[SELECT]"
]
"column_29" => array:1 [
0 => "Closed"
]
"column_30" => array:1 [
0 => "0"
]
"column_32" => array:1 [
0 => "Owner Driver"
]
"column_33" => array:1 [
0 => ""
]
"column_35" => array:1 [
0 => "Excavator"
]
"column_36" => array:1 [
0 => ""
]
"column_69" => array:1 [
0 => ""
]
"column_70" => array:1 [
0 => ""
]
"column_24" => array:1 [
0 => ""
]
"column_16" => array:1 [
0 => "Select"
]
"column_121" => array:1 [
0 => ""
]
"column_122" => array:1 [
0 => ""
]
]
If you need to 'flatten' the whole array, try array_map. The way it works is you define a function, that receives a single array item, transforms it and returns transformed value, then you pass that function to array_map in order to perform said transformation on all elements of the array:
function flatten($item){
return $item[0];
}
$original_array = Array(...); // assign your original array here
$flattened_array = array_map('flatten', $original_array);
Or using anonymous function syntax:
$flattened_array = array_map(function($item){return $item[0]}, $original_array);

Categories