I have an array:
array(
"A1" => 4,
"A2" =>5,
"B1" => 2,
"B2" =>7,
"C1" => 4,
"C2" =>5,
"D1" => 4,
"D2" =>5,
.....
)
Now I just want to new array:
$arr1 = array("A"=>array("A1"=>4, "A2"=>5), "B" => array("B1" => 2, "B2" => "7"), ...)
$arr2 = array(
0=>array("A1"=>"4","B1"=>2,"C1"=>4,"D1"=>4),
1=>array("A2"=>"4","B2"=>2,"C2"=>4,"D2"=>4)
)
Help me, how can I group the array?
$arr = array(
"A1" => 4,
"A2" =>5,
"B1" => 2,
"B2" =>7,
"C1" => 4,
"C2" =>5,
"D1" => 4,
"D2" =>5,
);
ksort($arr, SORT_NATURAL);
$arr1 = $arr2 = array();
foreach ($arr as $key => $value) {
$letter = substr($key, 0, 1);
$index = (int)substr($key, 1) - 1;
!isset($arr1[$letter]) && $arr1[$letter] = array();
$arr1[$letter][$key] = $value;
!isset($arr2[$index]) && $arr2[$index] = array();
$arr2[$index][$key] = $value;
}
ksort($arr1, SORT_NATURAL);
ksort($arr2, SORT_NATURAL);
var_dump($arr1, $arr2);
Outputs:
array(4) {
["A"]=>
array(2) {
["A1"]=>
int(4)
["A2"]=>
int(5)
}
["B"]=>
array(2) {
["B1"]=>
int(2)
["B2"]=>
int(7)
}
["C"]=>
array(2) {
["C1"]=>
int(4)
["C2"]=>
int(5)
}
["D"]=>
array(2) {
["D1"]=>
int(4)
["D2"]=>
int(5)
}
}
array(2) {
[0]=>
array(4) {
["A1"]=>
int(4)
["B1"]=>
int(2)
["C1"]=>
int(4)
["D1"]=>
int(4)
}
[1]=>
array(4) {
["A2"]=>
int(5)
["B2"]=>
int(7)
["C2"]=>
int(5)
["D2"]=>
int(5)
}
}
This should work for you:
<?php
$old = array(
"A1" => 4,
"A2" =>5,
"B1" => 2,
"B2" =>7,
"C1" => 4,
"C2" =>5,
"D1" => 4,
"D2" =>5,
);
$arr1 = array();
$arr2 = array();
foreach($old as $k => $v) {
$arr1[substr($k, 0 ,1)][$k] = $v;
ksort($arr1);
ksort($arr1[substr($k, 0 ,1)]);
}
foreach($old as $k => $v) {
$arr2[substr($k, 1 ,2)-1][$k] = $v;
ksort($arr2[substr($k, 1 ,2)-1]);
}
print_r($arr1);
print_r($arr2);
?>
Output:
//Array 1
Array
(
[A] => Array
(
[A1] => 4
[A2] => 5
)
[B] => Array
(
[B1] => 2
[B2] => 7
)
[C] => Array
(
[C1] => 4
[C2] => 5
)
[D] => Array
(
[D1] => 4
[D2] => 5
)
)
//Array2
Array
(
[0] => Array
(
[A1] => 4
[B1] => 2
[C1] => 4
[D1] => 4
)
[1] => Array
(
[A2] => 5
[B2] => 7
[C2] => 5
[D2] => 5
)
)
Related
I'm trying to merge these two arrays:
$array1 = array (
123 => array (
'minutes_watched' => 192.0,
'impressions_count' => 18
),
456 => array (
'minutes_watched' => 200.0,
'impressions_count' => 20
)
);
$array2 = array (
123 => array (
'ingested_trailers_count' => 3,
'ingested_shorts_count' => 2,
'ingested_features_count' => 1
),
456 => array (
'ingested_trailers_count' => 10,
'ingested_shorts_count' => 10,
'ingested_features_count' => 10
)
);
I would like to end up with this:
$merged = array (
123 => array (
'minutes_watched' => 192.0,
'impressions_count' => 18,
'ingested_trailers_count' => 3,
'ingested_shorts_count' => 2,
'ingested_features_count' => 1
),
456 => array (
'minutes_watched' => 200.0,
'impressions_count' => 20,
'ingested_trailers_count' => 10,
'ingested_shorts_count' => 10,
'ingested_features_count' => 10
)
);
And eventually with this:
$merged = array (
array(
'id' => 123
'minutes_watched' => 192.0,
'impressions_count' => 18,
'ingested_trailers_count' => 3,
'ingested_shorts_count' => 2,
'ingested_features_count' => 1
),
array(
'id' => 456
'minutes_watched' => 200.0,
'impressions_count' => 20,
'ingested_trailers_count' => 10,
'ingested_shorts_count' => 10,
'ingested_features_count' => 10
)
);
I can't seem to find a concise way of doing this without some clunky for loops. Surely there's some PHP array function that would handle this?
You could try something like this in a single loop. See comments for explanation. Output:
array(2) {
[0]=>
array(6) {
["minutes_watched"]=>
float(192)
["impressions_count"]=>
int(18)
["ingested_trailers_count"]=>
int(3)
["ingested_shorts_count"]=>
int(2)
["ingested_features_count"]=>
int(1)
["id"]=>
int(123)
}
[1]=>
array(6) {
["minutes_watched"]=>
float(200)
["impressions_count"]=>
int(20)
["ingested_trailers_count"]=>
int(10)
["ingested_shorts_count"]=>
int(10)
["ingested_features_count"]=>
int(10)
["id"]=>
int(456)
}
}
Code:
<?php
// Your input array.
$array1 = array (
123 => array (
'minutes_watched' => 192.0,
'impressions_count' => 18
),
456 => array (
'minutes_watched' => 200.0,
'impressions_count' => 20
)
);
$array2 = array (
123 => array (
'ingested_trailers_count' => 3,
'ingested_shorts_count' => 2,
'ingested_features_count' => 1
),
456 => array (
'ingested_trailers_count' => 10,
'ingested_shorts_count' => 10,
'ingested_features_count' => 10
)
);
// Output array
$output = [];
// Loop over first array, assuming all keys from $array1
// exist in $array2.
foreach ($array1 as $key => $child)
{
// Combine the two arrays, preserving keys.
$merged = $array1[$key] + $array2[$key];
// Copy key to 'id' element.
$merged['id'] = $key;
// Add to output.
$output[] = $merged;
}
var_dump($output);
/*
array(2) {
[0]=>
array(6) {
["minutes_watched"]=>
float(192)
["impressions_count"]=>
int(18)
["ingested_trailers_count"]=>
int(3)
["ingested_shorts_count"]=>
int(2)
["ingested_features_count"]=>
int(1)
["id"]=>
int(123)
}
[1]=>
array(6) {
["minutes_watched"]=>
float(200)
["impressions_count"]=>
int(20)
["ingested_trailers_count"]=>
int(10)
["ingested_shorts_count"]=>
int(10)
["ingested_features_count"]=>
int(10)
["id"]=>
int(456)
}
}
*/
I ended up with this solution:
$merged = array_replace_recursive($arrayOne, $arrayTwo);
$flattened = array_map(function($key, $value) {
return array_merge($value, [
'id' => $key
]);
}, array_keys($merged), $merged);
I don't feel great about the array_map portion, however...
Here's my code.
<?php
$data['test1'][0] = array('total' => 67, 'edition' => 2, 'pkg_version' => "2.5.0" );
$data['test1'][1] = array('total' => 67, 'edition' => 2, 'pkg_version' => "0.1.0" );
$data['test1'][2] = array('total' => 67, 'edition' => 2, 'pkg_version' => "0.3.0" );
$data['test2'][0] = array('total' => 86, 'edition' => 1, 'pkg_version' => "1.5.0");
$data['test2'][1] = array('total' => 85, 'edition' => 6, 'pkg_version' => "0.53.0");
$data['test2'][2] = array('total' => 98, 'edition' => 2, 'pkg_version' => "0.3");
$data['test2'][3] = array('total' => 98, 'edition' => 2, 'pkg_version' => "0.2");
$data['test3'][0] = array('total' => 60, 'edition' => 6, 'pkg_version' => "0.3");
$data['test3'][1] = array('total' => 60, 'edition' => 7, 'pkg_version' => "0.1.1");
$data['test3'][2] = array('total' => 60, 'edition' => 7, 'pkg_version' => "0.25");
foreach ($data as $row) {
foreach ($row as $k){
foreach ($k as $key => $value){
${$key}[] = $value;
}
}
}
array_multisort($pkg_version, SORT_DESC, $data);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
I am trying to sort a multidimensional array using the array_multisort function
I would like to sort the pkg_version of each element to be ordered
The returned order is not as expected. Not sure, I have misunderstood how
array_multisort works? or my code is wrong. could you guys help me? I try to
solve this problem for a long time. it's quite a complex dimension array.
Here's the result after running the code above.
Array
(
[test1] => Array
(
[0] => Array
(
[total] => 67
[edition] => 2
[pkg_version] => 2.5.0
)
[1] => Array
(
[toal] => 67
[edition] => 2
[pkg_version] => 0.1.0
)
[2] => Array
(
[total] => 67
[edition] => 2
[pkg_version] => 0.3.0
)
)
[test2] => Array
(
[0] => Array
(
[total] => 86
[edition] => 1
[pkg_version] => 1.5.0
)
[1] => Array
(
[total] => 85
[type] => 2
[pkg_version] => 0.53.0
)
[2] => Array
(
[total] => 98
[type] => 2
[pkg_version] => 0.3
)
[3] => Array
(
[total] => 98
[edition] => 2
[pkg_version] => 0.2
)
)
[test3] => Array
(
[0] => Array
(
[total] => 60
[edition] => 6
[pkg_version] => 0.3
)
[1] => Array
(
[total] => 60
[edition] => 7
[pkg_version] => 0.1.1
)
[2] => Array
(
[total] => 60
[edition] => 7
[pkg_version] => 0.25
)
)
)
This is what I expected.
Array
(
[test1] => Array
(
[0] => Array
(
[total] => 67
[edition] => 2
[pkg_version] => 2.5.0
)
[1] => Array
(
[total] => 67
[edition] => 2
[pkg_version] => 0.3.0
)
[2] => Array
(
[toal] => 67
[edition] => 2
[pkg_version] => 0.1.0
)
)
[test2] => Array
(
[0] => Array
(
[total] => 86
[edition] => 1
[pkg_version] => 1.5.0
)
[1] => Array
(
[total] => 85
[type] => 2
[pkg_version] => 0.53.0
)
[2] => Array
(
[total] => 98
[type] => 2
[pkg_version] => 0.3
)
[3] => Array
(
[total] => 98
[edition] => 2
[pkg_version] => 0.2
)
)
[test3] => Array
(
[0] => Array
(
[total] => 60
[edition] => 6
[pkg_version] => 0.3
)
[1] => Array
(
[total] => 60
[edition] => 7
[pkg_version] => 0.25
)
[2] => Array
(
[total] => 60
[edition] => 7
[pkg_version] => 0.1.1
)
)
)
You can loop over the array and use usort()
<?php
$data['test1'][0] = array('total' => 67, 'edition' => 2, 'pkg_version' => "2.5.0" );
$data['test1'][1] = array('total' => 67, 'edition' => 2, 'pkg_version' => "0.1.0" );
$data['test1'][2] = array('total' => 67, 'edition' => 2, 'pkg_version' => "0.3.0" );
$data['test2'][0] = array('total' => 86, 'edition' => 1, 'pkg_version' => "1.5.0");
$data['test2'][1] = array('total' => 85, 'edition' => 6, 'pkg_version' => "0.53.0");
$data['test2'][2] = array('total' => 98, 'edition' => 2, 'pkg_version' => "0.3");
$data['test2'][3] = array('total' => 98, 'edition' => 2, 'pkg_version' => "0.2");
$data['test3'][0] = array('total' => 60, 'edition' => 6, 'pkg_version' => "0.3");
$data['test3'][1] = array('total' => 60, 'edition' => 7, 'pkg_version' => "0.1.1");
$data['test3'][2] = array('total' => 60, 'edition' => 7, 'pkg_version' => "0.25");
// use a reference to the sub array
// |
// |
// v
foreach ($data as &$row)
{
usort($row, function($a, $b)
{
// make a desc sort by comparing $b against $a instead of $a against $b
return strcmp($b['pkg_version'], $a['pkg_version']);
});
}
var_dump($data);
This outputs :
array(3) {
["test1"]=>
array(3) {
[0]=>
array(3) {
["total"]=>
int(67)
["edition"]=>
int(2)
["pkg_version"]=>
string(5) "2.5.0"
}
[1]=>
array(3) {
["total"]=>
int(67)
["edition"]=>
int(2)
["pkg_version"]=>
string(5) "0.3.0"
}
[2]=>
array(3) {
["total"]=>
int(67)
["edition"]=>
int(2)
["pkg_version"]=>
string(5) "0.1.0"
}
}
["test2"]=>
array(4) {
[0]=>
array(3) {
["total"]=>
int(86)
["edition"]=>
int(1)
["pkg_version"]=>
string(5) "1.5.0"
}
[1]=>
array(3) {
["total"]=>
int(85)
["edition"]=>
int(6)
["pkg_version"]=>
string(6) "0.53.0"
}
[2]=>
array(3) {
["total"]=>
int(98)
["edition"]=>
int(2)
["pkg_version"]=>
string(3) "0.3"
}
[3]=>
array(3) {
["total"]=>
int(98)
["edition"]=>
int(2)
["pkg_version"]=>
string(3) "0.2"
}
}
["test3"]=>
&array(3) {
[0]=>
array(3) {
["total"]=>
int(60)
["edition"]=>
int(6)
["pkg_version"]=>
string(3) "0.3"
}
[1]=>
array(3) {
["total"]=>
int(60)
["edition"]=>
int(7)
["pkg_version"]=>
string(4) "0.25"
}
[2]=>
array(3) {
["total"]=>
int(60)
["edition"]=>
int(7)
["pkg_version"]=>
string(5) "0.1.1"
}
}
}
Note that in the last array, the version 0.3 is higher than the version 0.25. Since it's the order in your expected output, I left it as is, but if not, you can use instead of strcmp(), version_compare(), this will provide this output for $data['test3'] :
["test3"]=>
&array(3) {
[0]=>
array(3) {
["total"]=>
int(60)
["edition"]=>
int(7)
["pkg_version"]=>
string(4) "0.25"
}
[1]=>
array(3) {
["total"]=>
int(60)
["edition"]=>
int(6)
["pkg_version"]=>
string(3) "0.3"
}
[2]=>
array(3) {
["total"]=>
int(60)
["edition"]=>
int(7)
["pkg_version"]=>
string(5) "0.1.1"
}
}
}
i do have currently following problem. I have to check if the array contains the exact same values and if they were found before.
int(3) wasn´t found before so it is 0,
int(8) wasn´t found before so it is 0,
int(5) wasn´t found before so it is 0,
int(8) was found before so it is 1,
int(3) and int(8) was not found together so it is 0, and so on.
I already tried it with array_unique but that didn´t work as i wanted
For example:
array(7) {
[2] => array(1) {
[0] => int(3)
}
[3] => array(1) {
[0] => int(8)
}
[4] => array(1) {
[0] => int(5)
}
[5] => array(1) {
[0] => int(8)
}
[6] => array(2) {
[0] => int(3)
[1] => int(8)
}
[7] => array(2) {
[0] => int(2)
[1] => int(5)
}
[8] => array(2) {
[0] => int(3)
[1] => int(8)
}
}
it must look something like this
array(7) {
[2] => array(1) {
[0] => int(0)
}
[3] => array(1) {
[0] => int(0)
}
[4] => array(1) {
[0] => int(0)
}
[5] => array(1) {
[0] => int(1)
}
[6] => array(1) {
[0] => int(0)
}
[7] => array(1) {
[0] => int(0)
}
[8] => array(1) {
[0] => int(1)
}
}
You could use array_map() and serialize():
<?php
$data = [
2 => [
3,
],
3 => [
8,
],
4 => [
5,
],
5 => [
8,
],
6 => [
3,
8,
],
7 => [
2,
5,
],
8 => [
3,
8,
],
];
$occurrences = [];
$mapped = array_map(function (array $values) use (&$occurrences) {
// create serialized representation of the values
// which we can use as an index
$index = serialize($values);
// haven't seen these values before
if (!array_key_exists($index, $occurrences)) {
$occurrences[$index] = 1;
return 0;
}
// increase our counter
$occurrences[$index]++;
return $occurrences[$index] - 1;
}, $data);
var_dump($mapped);
For reference, see:
http://php.net/manual/en/function.array-map.php
http://php.net/manual/en/function.serialize.php
For an example, see:
https://3v4l.org/oWBcS
<?php
$new_array = array();
$indicator = array();
$current_array = array(
"2" => array(3),
"3" => array(8),
"4" => array(5),
"5" => array(8),
"6" => array(3,8),
"7" => array(2,5),
"8" => array(3,8),
);
foreach($current_array as $key => $value){
if(!in_array($value, $new_array, true)){
$new_array[$key] = $value;
$indicator[$key] = false;
} else {
$indicator[$key] = true;
}
}
var_dump($indicator);
I have arrays in one submission, please see below details:
array(5) {
["ambition_id"]=>
array(2) {
[55]=> string(2) "55"
[60]=> string(2) "60"
}
["target"]=>
array(1) {
[0]=> string(8) "target 1"
[1]=> string(8) "target 2"
}
["strides"]=>
array(1) {
[0]=> string(1) "1"
[1]=> string(1) "1"
}
["date"]=>
array(1) {
[0]=> string(10) "2017-02-08"
[1]=> string(10) "2017-03-08"
}
["frequency"]=>
array(1) {
[0]=> string(1) "1"
[1]=> string(1) "2"
}
}
Actually, I have two tables in mysql, 'ambition' and 'target'. Ambition is a group of targets ('ambition_id' is foreign key in 'target' table). That array will be stored in 'target' table. That's why there is an 'ambition_id'
I've tried many times but failed (using foreach), now I need someone who can give me a help.
By brute force, It's easy! I solved it already but I need "more advanced" array manipulation.
How can I come up into this?
array(2) {
[0] => array('ambition_id' => 55,
'target' => 'target 1',
'strides' => 1,
'date' => '2017-02-08',
'frequency' => 1
),
[1] => array('ambition_id' => 60,
'target' => 'target 2',
'strides' => 2,
'date' => '2017-03-08',
'frequency' => 2)
}
Please do help, many thanks!
You have to pivot your data:
$data = array (
"ambition_id" =>
array (
55 => "55",
60 => "60"
),
"target" =>
array (
0 => "target 1",
1 => "target 2"
),
"strides" =>
array (
0 => "1",
1 => "1"
),
"date" =>
array (
0 => "2017-02-08",
1 => "2017-03-08"
),
"frequency" =>
array (
0 => "1",
1 => "2"
)
);
// pivot data
$pivot = array();
foreach ($data as $datum => $values) {
$value_index = 0;
foreach ($values as $value) {
$pivot[$value_index][$datum] = $value;
$value_index++;
}
}
print_r($pivot);
This assumes you only have two levels of data and that the data is well behaved.
Not the best answer, but it solves your problem
<?php
$array = [
"ambition_id" =>
[
55 => "55",
60 => "60"
],
"target" =>
[
0 => "target 1",
1 => "target 2"
],
"strides" =>
[
0 => "1",
1 => "1"
],
"date" =>
[
0 => "2017-02-08",
1 => "2017-03-08"
],
"frequency" =>
[
0 => "1",
1 => "2"
],
];
$result = array();
foreach ($array as $k => $v) {
foreach ($v as $kk => $vv) {
if ($k == "ambition_id") {
$result[] = array($k => $vv);
} else {
$result[$kk][$k] = $vv;
}
}
}
Here is the test https://3v4l.org/UdHH8
Just use loop the array and user array_values to re-index the loop the inner array and store it into new array like below .
<?php
$new_array =array();
foreach($array as $key1=>$row1 )
{
$ss =array_values($row1);
foreach($ss as $key2=>$row2)
{
$new_array[$key2][$key1]=$row2;
}
}
echo "<pre>";
print_r($new_array);
?>
Output :
Array
(
[0] => Array
(
[ambition_id] => 55
[target] => target 1
[strides] => 1
[date] => 2017-02-08
[frequency] => 1
)
[1] => Array
(
[ambition_id] => 60
[target] => target 2
[strides] => 1
[date] => 2017-03-08
[frequency] => 2
)
)
I have the following query result:
Array
(
[0] => stdClass Object
(
[TreatmentLog_ID] => 131
[DateAdministered] => 2016-07-15
[Notes] =>
[Treatment_ID] => 144
[AmountAdministered] => 1.5
[Injectable_ID] => 2
[InjectableName] => Baytril
)
[1] => stdClass Object
(
[TreatmentLog_ID] => 133
[DateAdministered] => 2016-07-12
[Notes] =>
[Treatment_ID] => 146
[AmountAdministered] => 1.2
[Injectable_ID] => 20
[InjectableName] => Vitamin C
)
[2] => stdClass Object
(
[TreatmentLog_ID] => 133
[DateAdministered] => 2016-07-12
[Notes] =>
[Treatment_ID] => 147
[AmountAdministered] => 1.3
[Injectable_ID] => 21
[InjectableName] => Vitamin E
)
)
I'd like to be able to restructure the array into something like this:
Array
(
[0] => stdClass Object
(
[TreatmentLog_ID] => 131
[DateAdministered] => 2016-07-15
[Notes] =>
[Treatments] => Array
(
[0] => stdClass Object
(
[Treatment_ID] => 144
[AmountAdministered] => 1.5
[Injectable_ID] => 2
[InjectableName] => Baytril
)
)
)
[1] => stdClass Object
(
[TreatmentLog_ID] => 133
[DateAdministered] => 2016-07-12
[Notes] =>
[Treatments] => Array
(
[0] => stdClass Object
(
[Treatment_ID] => 146
[AmountAdministered] => 1.2
[Injectable_ID] => 20
[InjectableName] => Vitamin C
)
[1] => stdClass Object
(
[Treatment_ID] => 147
[AmountAdministered] => 1.3
[Injectable_ID] => 21
[InjectableName] => Vitamin E
)
)
)
)
Notice how the second array looks merges the InjectableName, AmountAdministered, Injectable_ID, and Treatment_ID into the array Treatments if the TreatmentLog_ID is a match. Typically working with arrays isn't a problem, but this one has me stumped. Also I cannot change the query.
How could I pull this off in PHP?
The solution using isset and array_values functions:
// $arr is your initial array
$result = [];
foreach ($arr as $obj) {
$innerObj = (object)[ 'Treatment_ID' => $obj->Treatment_ID, 'AmountAdministered' => $obj->AmountAdministered,
'Injectable_ID' => $obj->Injectable_ID, 'InjectableName' => $obj->InjectableName ];
if (!isset($result[$obj->TreatmentLog_ID])) {
$result[$obj->TreatmentLog_ID] = (object)[
'TreatmentLog_ID' => $obj->TreatmentLog_ID,
'DateAdministered' => $obj->DateAdministered,
'Notes' => $obj->Notes,
'Treatments' => [$innerObj]
];
} else {
$result[$obj->TreatmentLog_ID]->Treatments[] = $innerObj;
}
}
$result = array_values($result);
print_r($result); // will output the expected result
Try this.
We use array_filter() to fetch all of the elements from the $inputArray with the same TreatmentLog_ID. Then we transform those filtered elements with array_map(). We have to create a copy of each element with clone, since they're objects and objects are passed by reference. Then we unset() the keys we don't need in the copy.
<?php
$inputArray = [
0 => (object) [
'TreatmentLog_ID' => 131,
'DateAdministered' => '2016-07-15',
'Notes' => '',
'Treatment_ID' => 144,
'AmountAdministered' => 1.5,
'Injectable_ID' => 2,
'InjectableName' => 'Baytril'
],
1 => (object) [
'TreatmentLog_ID' => 133,
'DateAdministered' => '2016-07-12',
'Notes' => '',
'Treatment_ID' => 146,
'AmountAdministered' => 1.2,
'Injectable_ID' => 20,
'InjectableName' => 'Vitamin C'
],
2 => (object) [
'TreatmentLog_ID' => 133,
'DateAdministered' => '2016-07-12',
'Notes' => '',
'Treatment_ID' => 147,
'AmountAdministered' => 1.3,
'Injectable_ID' => 21,
'InjectableName' => 'Vitamin E'
],
];
$transformedArray = [];
foreach ($inputArray as $key => $value)
{
$transformedArray[$key] = [
'TreatmentLog_ID' => $value->TreatmentLog_ID,
'DateAdministered' => $value->DateAdministered,
'Notes' => $value->Notes,
'Treatments' => array_map(
function ($v) {
$copy = clone $v;
unset($copy->Notes);
unset($copy->DateAdministered);
unset($copy->TreatmentLog_ID);
return $copy;
},
array_filter($inputArray, function ($v) use ($value) {
return $v->TreatmentLog_ID == $value->TreatmentLog_ID;
})
)
];
}
var_dump($transformedArray);
This gives me:
array(3) {
[0]=>
array(4) {
["TreatmentLog_ID"]=>
int(131)
["DateAdministered"]=>
string(10) "2016-07-15"
["Notes"]=>
string(0) ""
["Treatments"]=>
array(1) {
[0]=>
object(stdClass)#5 (4) {
["Treatment_ID"]=>
int(144)
["AmountAdministered"]=>
float(1.5)
["Injectable_ID"]=>
int(2)
["InjectableName"]=>
string(7) "Baytril"
}
}
}
[1]=>
array(4) {
["TreatmentLog_ID"]=>
int(133)
["DateAdministered"]=>
string(10) "2016-07-12"
["Notes"]=>
string(0) ""
["Treatments"]=>
array(2) {
[1]=>
object(stdClass)#6 (4) {
["Treatment_ID"]=>
int(146)
["AmountAdministered"]=>
float(1.2)
["Injectable_ID"]=>
int(20)
["InjectableName"]=>
string(9) "Vitamin C"
}
[2]=>
object(stdClass)#7 (4) {
["Treatment_ID"]=>
int(147)
["AmountAdministered"]=>
float(1.3)
["Injectable_ID"]=>
int(21)
["InjectableName"]=>
string(9) "Vitamin E"
}
}
}
[2]=>
array(4) {
["TreatmentLog_ID"]=>
int(133)
["DateAdministered"]=>
string(10) "2016-07-12"
["Notes"]=>
string(0) ""
["Treatments"]=>
array(2) {
[1]=>
object(stdClass)#8 (4) {
["Treatment_ID"]=>
int(146)
["AmountAdministered"]=>
float(1.2)
["Injectable_ID"]=>
int(20)
["InjectableName"]=>
string(9) "Vitamin C"
}
[2]=>
object(stdClass)#9 (4) {
["Treatment_ID"]=>
int(147)
["AmountAdministered"]=>
float(1.3)
["Injectable_ID"]=>
int(21)
["InjectableName"]=>
string(9) "Vitamin E"
}
}
}
}
Let's break down how we build Treatments:
'Treatments' => array_map(
function ($v) {
$copy = clone $v;
unset($copy->Notes);
unset($copy->DateAdministered);
unset($copy->TreatmentLog_ID);
return $copy;
},
array_filter($inputArray, function ($v) use ($value) {
return $v->TreatmentLog_ID == $value->TreatmentLog_ID;
})
)