How to get value from unserialized data - php

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.

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 remove a parent index array and change it to object in laravel

I have an array like below :
array:3 [
"2021-08-07" => array:3 [
"id" => "1"
"date" => "2021-08-07"
"numbers" => array:2 [
0 => 1
1 => 2
]
]
"2021-08-08" => array:3 [
"id" => "1"
"date" => "2021-08-08"
"numbers" => array:2 [
0 => 1
1 => 2
]
]
]
What I want to do is simply to remote the parent 2021-08-08 items because I have the date inside the array already . Now, what I have tried so far is :
$result = array_map(function ($el){
return $el[0];
},$test);
dd($result);
But it gives me error of undefined index[0] . what I want this array to look like is like below :
array:3 [
"id" => "1"
"date" => "2021-08-07"
"numbers" => array:2 [
0 => 1
1 => 2
],
"id" => "1"
"date" => "2021-08-08"
"numbers" => array:2 [
0 => 1
1 => 2
]
]
Won't just array_values() do?
array_values($test);
$test = array_values(yourArray);
$test = (object)$yourArray;

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

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