I have a $variable with an array like this, is there a way to remove the key "attributes" and make it a simple array?
Array
(
[0] => stdClass Object
(
[attributes] => stdClass Object
(
[FID] => 11
[Kode_Provi] => 31
[Provinsi] => DKI Jakarta
[Kasus_Posi] => 808
[Kasus_Semb] => 50
[Kasus_Meni] => 85
)
)
[1] => stdClass Object
(
[attributes] => stdClass Object
(
[FID] => 12
[Kode_Provi] => 32
[Provinsi] => Jawa Barat
[Kasus_Posi] => 220
[Kasus_Semb] => 11
[Kasus_Meni] => 21
)
)
[2] => stdClass Object
(
[attributes] => stdClass Object
(
[FID] => 16
[Kode_Provi] => 36
[Provinsi] => Banten
[Kasus_Posi] => 152
[Kasus_Semb] => 7
[Kasus_Meni] => 14
)
)
)
``` .
Yes and no. You have an array of objects with properties there. You can not simply remove the "attributes key" because it actually represents a property of the object that is the value of the array at this point.
However, for the yes part, you can of course create an array out of your data structure and do not have objects on the levels anymore:
// Example of input from the question
$innerValue = new stdClass();
$innerValue->FID = 12;
$innerValue->Kode_Provi = 32;
$obj = new stdClass();
$obj->attributes = $innerValue;
$arr = [$obj];
// transforming it to array
$newArr = [];
foreach ($arr as $obj) {
$innerArr = [];
foreach ($obj->attributes as $prop => $value) {
$innerArr[$prop] = $value;
}
$newArr[] = $innerArr;
}
var_dump($newArr);
output looks like this:
array(1) {
[0] =>
array(2) {
'FID' =>
int(12)
'Kode_Provi' =>
int(32)
}
}
If you would like to just remove 'attributes' key it's just simple:
<?php
$variable = [
(object) [
'attributes' => (object) [
'FID' => 11,
'Kode_Provi' => 31,
'Provinsi' => 'DKI Jakarta',
'Kasus_Posi' => 808,
'Kasus_Semb' => 50,
'Kasus_Meni' => 85,
]
],
(object) [
'attributes' => (object) [
'FID' => 12,
'Kode_Provi' => 32,
'Provinsi' => 'Jawa Barat',
'Kasus_Posi' => 220,
'Kasus_Semb' => 11,
'Kasus_Meni' => 21,
]
]
];
$variable = array_map(
function($item) {
return $item->attributes;
},
$variable
);
print_r($variable);
Related
I need to convert the below 2d array in to specified 2d array format. Array contains multiple parent and multiple child array. Also, have tried to convert the code, but am not getting the expected output.
This is the code what i have tried,
$a1 = array(
'0' =>
array(
'banner_details' =>
array(
'id' => 2,
'section_id' => 24
),
'slide_details' =>
array(
0 => array(
'id' => 74,
'name' => 'Ads1'
),
1 => array(
'id' => 2,
'name' => 'Ads2'
)
)
),
'1' =>
array(
'banner_details' =>
array(
'id' => 106,
'section_id' => 92
),
'slide_details' =>
array(
0 => array(
'id' => 2001,
'name' => 'Adv1'
),
1 => array(
'id' => 2002,
'name' => 'Adv2'
)
)
)
);
$s = [];
for($i = 0; $i<2; $i++) {
foreach($a1[$i]['slide_details'] as $vs){
$s[] = $vs;
}
}
My output:
Array
(
[0] => Array
(
[id] => 74
[name] => Ads1
)
[1] => Array
(
[id] => 2
[name] => Ads2
)
[2] => Array
(
[id] => 2001
[name] => Adv1
)
[3] => Array
(
[id] => 2002
[name] => Adv2
)
)
Expected output:
Array
(
[24] => Array
(
[0] => 74
[1] => 2
)
[92] => Array
(
[0] => 2001
[1] => 2002
)
)
please check the above code and let me know.
Thanks,
You can apply next simple foreach loop with help of isset() function:
foreach($a1 as $data){
if (isset($data['banner_details']['section_id'])){
$s[$data['banner_details']['section_id']] = [];
if (isset($data['slide_details'])){
foreach($data['slide_details'] as $row){
$s[$data['banner_details']['section_id']][] = $row['id'];
}
}
}
}
Demo
If you know that indexes like banner_details or slide_details or section_id will be there always then you can skip isset() in if statements.
You can use array_column function for simple solution:
$result = [];
foreach ($a1 as $item)
{
$result[$item['banner_details']['section_id']] = array_column($item['slide_details'], 'id');
}
var_dump($result);
I have below array $billitems_taxes
[0] => Array
(
[id] => 1
[tax_name] => A
[tax_value] => 12
)
[1] => Array
(
[id] => 2
[tax_name] => A
[tax_value] => 8
)
[2] => Array
(
[id] => 3
[tax_name] => B
[tax_value] => 12
)
and I want output as below, find two common tax_name and do some of same and then create a new array.
[0] => Array
(
[id] => 1
[tax_name] => A
[tax_value] => 20
)
[1] => Array
(
[id] => 3
[tax_name] => B
[tax_value] => 12
)
I tried with below code, but it did not return a correct array.
$return_array = [];
foreach($billitems_taxes as $b)
{
$return_array['tax_name'] = $b->tax_name;
$return_array['tax_value'] += $b->tax_value;
}
First off, you have an array of arrays, not objects.
Then your loop needs to know if it has already seen a this tax name which will already be in the new array to check that I used array_key_exists()
$return_array = [];
foreach($billitems_taxes as $b)
{
if ( array_key_exists($b['tax_name'], $return_array) ) {
$return_array[$b['tax_name']]['tax_value'] += $b['tax_value'];
} else {
$return_array[$b['tax_name']] = $b;
}
}
RESULT
Array(
[A] => Array
([id] => 1
[tax_name] => A
[tax_value] => 20
)
[B] => Array
([id] => 3
[tax_name] => B
[tax_value] => 12
)
)
And if its important for the array to be numerically indexed just add
$return_array = array_values($return_array);
after the end of the loop
You must group by 'tax_name' and must sum 'tax_value'.
$billitems_taxes = [
['id' => 1, 'tax_name' => 'A', 'tax_value' => 12],
['id' => 2, 'tax_name' => 'A', 'tax_value' => 8],
['id' => 3, 'tax_name' => 'B', 'tax_value' => 12]
];
$result = [];
foreach($billitems_taxes as $row){
$groupKey = $row['tax_name'];
if(array_key_exists($groupKey,$result)){
$result[$groupKey]['tax_value'] += $row['tax_value'];
} else {
$result[$groupKey] = $row;
}
}
$result = array_values($result);
echo '<pre>';
var_export($result);
/*
array (
0 =>
array (
'id' => 1,
'tax_name' => 'A',
'tax_value' => 20,
),
1 =>
array (
'id' => 3,
'tax_name' => 'B',
'tax_value' => 12,
),
)
*/
The solution with the external class tableArray is very simple. The result is the same.
$result = tableArray::create($billitems_taxes)
->filterGroupAggregate(['tax_value' => 'SUM'],['tax_name'])
->fetchAll()
;
I want to change the laravel collection because I want to pass this collection to generate report.
How to change collection on the basis of the value in the column.
Previous:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 23
[form_type] => radio
[details] => [{"id":55 title:"AA" name:"BB"}, {"id":56 title:"CC" name:"DD"}]
)
[1] => stdClass Object
(
[id] => 24
[form_type] => checkbox
[details] => [{"id":57 title:"PP" name:"QQ"}, {"id":58 title:"RR" name:"SS"}]
)
)
)
--details column has array of objects, I want it as below format:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 23
[form_type] => radio
[title] => "AA"
[name] => "BB"
)
[1] => stdClass Object
(
[id] => 23
[form_type] => radio
[title] => "CC"
[name] => "DD"
)
[2] => stdClass Object
(
[id] => 24
[form_type] => checkbox
[title] => "PP"
[name] => "QQ"
)
[4] => stdClass Object
(
[id] => 24
[form_type] => checkbox
[title] => "RR"
[name] => "SS"
)
)
)
The only way is to use each method of Laravel collection. I expect the data in the following format and you will get the desired output.
$data = collect([
(object)["id" => 23, "form_type" => "radio", "details" => [["id" => 55, 'title' => "AA", 'name' => "BB"], ["id" => 56, 'title' => "CC", 'name' => "DD"]]]
]);
$result = collect();
$data->each(function($d) use ($result) {
$d = collect($d);
unset($t['id']);
$tmp = collect($d['details']);
$tmp->each(function($t) use (&$d, $result) {
$result->push($d->forget('details')->merge($t));
});
});
echo "<pre>";
print_r($result);
try this with ->map() function then ->push() into new collection
$data = [
[
"id" => 23,
"form_type" => "radio",
"details" => [["id" => 55, "title" => "AA", "name" => "BB"], ["id" => 56, "title" => "CC", "name" => "DD"]]
],
[
"id" => 24,
"form_type" => "checkbox",
"details" => [["id" => 57, "title" => "PP", "name" => "QQ"], ["id" => 58, "title" => "RR", "name" => "SS"]]
],
];
$data = collect($data);
$final = collect();
$data->map(function ($row) use ($data, $final) {
return collect($row['details'])->map(function ($item) use ($data, $row, $final) {
$parentData = $data->where('id', $row['id'])->first();
$final->push([
'id' => $parentData['id'],
'title' => $item['title'],
'form_type' => $parentData['form_type'],
'name' => $item['name']
]);
});
});
return $final;
I have an array that looks like this:-
Array (
[0] => Array ( [id] => 10 [group] => 11 )
[1] => Array ( [id] => 11 [group] => 13 )
[2] => Array ( [id] => 12 [group] => 13 )
[3] => Array ( [id] => 13 [group] => 13 )
[4] => Array ( [id] => 14 [group] => 16 )
[5] => Array ( [id] => 15 [group] => 16 )
[6] => Array ( [id] => 16 [group] => 16 )
)
For each different group in this array i want to create array that stores id's.
In my example i have 3 groups, so i want to get 3 arrays like this:
Array ( [0] => 10)
Array ( [0] => 11
[1] => 12
[2] => 13)
Array ( [0] => 14
[1] => 15
[2] => 16)
Is it possible if it is how can i do it?
This should help you get started.
https://iconoun.com/demo/temp_icold.php
<?php // demo/temp_icold.php
/**
* Manipulate multi-dimensional arrays
*
* https://stackoverflow.com/questions/45422046/how-to-extract-values-from-multidimensional-array-grouped-by-some-value-inside-i
*/
error_reporting(E_ALL);
echo '<pre>';
$original = Array (
'0' => Array ( 'id' => 10, 'group' => 11 ),
'1' => Array ( 'id' => 11, 'group' => 13 ),
'2' => Array ( 'id' => 12, 'group' => 13 ),
'3' => Array ( 'id' => 13, 'group' => 13 ),
'4' => Array ( 'id' => 14, 'group' => 16 ),
'5' => Array ( 'id' => 15, 'group' => 16 ),
'6' => Array ( 'id' => 16, 'group' => 16 ),
);
print_r($original);
foreach ($original as $arr)
{
$ndx = 'group' . $arr['group'];
$out[$ndx][] = $arr['id'];
}
print_r($out);
You can achieve this through many different methods, but the simplest is probably a foreach() loop. In the example below I am looping through $a (your sample array) and building up a new array called $grouped with the index as the group attributes value.
In my example I am not duplicating the ID's inside of $group. If you wanted duplicates you could remove the second if statement.
$a = [
['id' => 10, 'group' => 11],
['id' => 11, 'group' => 13],
['id' => 12, 'group' => 13],
['id' => 13, 'group' => 13],
['id' => 14, 'group' => 16],
['id' => 15, 'group' => 16],
['id' => 16, 'group' => 16],
];
$grouped = [];
foreach ($a as $entry) {
if (! array_key_exists($entry['group'], $grouped)) {
$grouped[$entry['group']] = [];
}
if (! in_array($entry['id'], $grouped[$entry['group']])) {
$grouped[$entry['group']][] = $entry['id'];
}
}
var_dump($grouped);
The example outputs the following:
array(3) {
[11]=>
array(1) {
[0]=>
int(10)
}
[13]=>
array(3) {
[0]=>
int(11)
[1]=>
int(12)
[2]=>
int(13)
}
[16]=>
array(3) {
[0]=>
int(14)
[1]=>
int(15)
[2]=>
int(16)
}
}
$result_array = array();
foreach($array_name as $sub_array){
$result_array[$sub_array['group']][] = $sub_array['id'];
}
This will loop through your input array and create a result two dimensional array indexed by your group values. To extract a group result just index for the group as such: $result_array['GROUP_NUMBER'];
Here is my take on this:
<?php
$arr = array(
array("id"=>10,"group"=>11),
array("id"=>11,"group"=>13),
array("id"=>12,"group"=>13),
array("id"=>13,"group"=>13),
array("id"=>14,"group"=>16),
array("id"=>15,"group"=>16),
array("id"=>16,"group"=>16)
);
echo "<pre>".print_r(groupsToArrays($arr),true)."</pre>";
function groupsToArrays($arr, $groupkey = "group") {
$main = array();
$group = 0;
$arr[] = array("id"=>"end", $groupkey => "0");
foreach($arr as $key => $value) {
if($group != $value[$groupkey]) {
if($key != 0) $main[$group] = $tempArray;
if($value['id'] == "end") continue;
$group = $value[$groupkey];
$tempArray = array();
}
$tempArray[] = $value;
}
return $main;
}
This function will loop through your array and check the $groupkey key and will add every match to it's own array.
This is the return:
Array
(
[11] => Array
(
[0] => Array
(
[id] => 10
[group] => 11
)
)
[13] => Array
(
[0] => Array
(
[id] => 11
[group] => 13
)
[1] => Array
(
[id] => 12
[group] => 13
)
[2] => Array
(
[id] => 13
[group] => 13
)
)
[16] => Array
(
[0] => Array
(
[id] => 14
[group] => 16
)
[1] => Array
(
[id] => 15
[group] => 16
)
[2] => Array
(
[id] => 16
[group] => 16
)
)
)
So now you can access that group array by doing:
$groups = groupsToArrays($arr);
print_r($groups[$groupID]);
I have this array
Array (
[0] => Array
(
[0] => stdClass Object
(
[id] => 226
[user_id] => 1
[name] => Eden Corner Tub by Glass - $2099
)
[1] => stdClass Object
(
[id] => 225
[user_id] => 1
[name] => Blue Quilted Leather Jacket by Minusey - $499
)
[2] => stdClass Object
(
[id] => 222
[user_id] => 1
[name] => Darling New Bathtub by Duravit - $6300
)
)
[1] => Array
(
[0] => stdClass Object
(
[id] => 226
[user_id] => 1
[name] => Eden Corner Tub by Glass - $2099
)
[1] => stdClass Object
(
[id] => 229
[user_id] => 1
[name] => Batman Tumbler Golf Cart - $50000
)
[2] => stdClass Object
(
[id] => 228
[user_id] => 1
[name] => Swirlio Frozen Fruit Dessert Maker - $60
)
) )
I have an array of products that I need to make sure are unique.
Need to make this array unique by id. These array are generated by pushing value.
I'm trying to solve this for more than a week now, but I dont get it to work. I know it should be easy...but anyway - I don't get it :D
Try this:
$array = array(
0 => array(
"name" => "test",
"id" => 4
),
1 => array(
"name" => "test2",
"id" => 152
),
2 => array(
"name" => "test2",
"id" => 152
)
);
$newArray = array();
foreach($array as $value) {
$newArray[$value['id']]['name'] = $value['name'];
$newArray[$value['id']]['id'] = $value['id'];
}
foreach($array as $key=>$inner_array)
{
foreach($array as $key_again=>$array_again)
{
if($key != $key_again)
{
if($inner_array['name'] != $array_again['name'] AND $inner_array['id'] != $array_again['id'])
{
//its okay
}
else
{
unset($array[$key]);
}
}
}
}
Not sure how the arrays are generated, but, if you can change that, you could set the array keys to the IDs directly and check if the id is already set.
Otherwise, you can do the following:
$unique = array();
foreach( $array as $values ) {
if( ! isset( $unique[$values['id']] ) ) {
$unique[$values['id']] = $values;
}
}
This will make your array unique:
$array = array(
0 => array(
"name" => "test",
"id" => 4
),
1 => array(
"name" => "test2",
"id" => 152
),
2 => array(
"name" => "test2",
"id" => 152
) );
$result = array();
$index = array();
foreach($array as $i => $elem) {
if(!isset($index[$elem['id']])) {
$result[$i] = $elem;
$index[$elem['id']] = 1;
}
}
echo var_export($result);
Output:
array (
0 =>
array (
'name' => 'test',
'id' => 4,
),
1 =>
array (
'name' => 'test2',
'id' => 152,
),
)
This will work. It could be considered more clean than a for loop, but I'm not sure about performance.
$array = [
[ "name" => "test", "id" => 4 ],
[ "name" => "test2", "id" => 152 ],
[ "name" => "test2", "id" => 152 ]
];
array_walk($array, function(&$item, $idx) use($array){
$matches = array_slice(array_keys($array, $item), 1);
if (in_array($idx, $matches)) {
$item = false;
}
});
$array = array_filter($array);
Edit Since you updated the data set to work with, you would need to flatten it 1 level:
$array = call_user_func_array('array_merge', $array);