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;
})
)
Related
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 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
)
)
How do I merge these two arrays:
Array
(
[uczrrtawpxfjanycwwlqygoq] => Array
(
[user_id] => 53
[value] => Boris
[key] => uczrrtawpxfjanycwwlqygoq
)
[dreamhack] => Array
(
[user_id] => 263
[value] => More
[key] => dreamhack
)
)
And my second array which needs to be added to the keys of the first
Array
(
[dreamhack] => Array
(
[viewers] => 32229
[channel] => Array
(
[broadcaster_language] => en
[display_name] => Dreamhack
[_id] => 22859340
[created_at] => 2011-06-09T06:11:52Z
[updated_at] => 2016-08-14T18:34:36Z
[delay] =>
[banner] =>
[background] =>
[partner] => 1
[views] => 36258931
[followers] => 79892
[_links] => Array
(
[self] =>
[teams] =>
)
)
)
)
Doing a simple array merge gives the original array and not a combined one. So for dreamhack I would require one aeeay with all the tags combined [user_id], [value], [key], [viewers], [channel] and subarray.
as asked in the comment .. is this what you want ?
<pre>
<?php
$array1 = [
'uczrrtawpxfjanycwwlqygoq' => [
'user_id' => 53,
'value' => 'Boris',
'key' => 'uczrrtawpxfjanycwwlqygoq'
],
'dreamhack' => [
'user_id' => 263,
'value' => 'More',
'key' => 'dreamhack'
]
];
$array2 = [
'dreamhack' => [
'viewers' => 32229,
'channel' => [
'broadcaster_language' => 'en',
'display_name' => 'Dreamhack',
'_id' => 22859340,
'created_at' => '2011-06-09T06:11:52Z',
'updated_at' => '2016-08-14T18:34:36Z',
'delay' => '',
'banner' => '',
'background' => '',
'partner' => 1,
'views' => 36258931,
'followers' => 79892,
'_links' => [
'self' => '',
'teams' => ''
]
]
]
];
$result = array_merge_recursive ($array1, $array2);
var_dump($result);
?>
</pre>
result looks like:
array(2) {
["uczrrtawpxfjanycwwlqygoq"]=>
array(3) {
["user_id"]=>
int(53)
["value"]=>
string(5) "Boris"
["key"]=>
string(24) "uczrrtawpxfjanycwwlqygoq"
}
["dreamhack"]=>
array(5) {
["user_id"]=>
int(263)
["value"]=>
string(4) "More"
["key"]=>
string(9) "dreamhack"
["viewers"]=>
int(32229)
["channel"]=>
array(12) {
["broadcaster_language"]=>
string(2) "en"
["display_name"]=>
string(9) "Dreamhack"
["_id"]=>
int(22859340)
["created_at"]=>
string(20) "2011-06-09T06:11:52Z"
["updated_at"]=>
string(20) "2016-08-14T18:34:36Z"
["delay"]=>
string(0) ""
["banner"]=>
string(0) ""
["background"]=>
string(0) ""
["partner"]=>
int(1)
["views"]=>
int(36258931)
["followers"]=>
int(79892)
["_links"]=>
array(2) {
["self"]=>
string(0) ""
["teams"]=>
string(0) ""
}
}
}
}
Use array_merge_recursive, which is specifically designed to do this sort of thing. To quote the docs:
array_merge_recursive() merges the elements of one or more arrays
together so that the values of one are appended to the end of the
previous one. It returns the resulting array.
If the input arrays have the same string keys, then the values for
these keys are merged together into an array, and this is done
recursively, so that if one of the values is an array itself, the
function will merge it with a corresponding entry in another array
too. If, however, the arrays have the same numeric key, the later
value will not overwrite the original value, but will be appended.
I have an associative array , i would like to add some more key and values
Array
(
[0] => Array
(
[NUMBER] => 67
[TYPE] => Other
[DATE] => 3/31/2011
)
[1] => Array
(
[NUMBER] => 87
[TYPE] => something
[DATE] => 3/28/2011
)
[2] => Array
(
[NUMBER] => 67
[TYPE] => Other
[DATE] => 3/2/2011
)
)
In Above array i want to add another key named STATUS and value before DATE
so that finally iget
Array
(
[0] => Array
(
[NUMBER] => 67
[TYPE] => Other
[STATUS] => waiting
[DATE] => 3/31/2011
)
}
canPlease give me proper direction
$arr = Array(
0 => Array('NUMBER' => 67, 'TYPE' => Other, 'DATE' => '3/32/2011'),
1 => Array('NUMBER' => 87, 'TYPE' => something, 'DATE' => '3/28/2011'),
2 => Array('NUMBER' => 67, 'TYPE' => Other, 'DATE' => '3/2/2011')
);
foreach($arr as $key => $value) {
$arr[$key] = array_slice($value, 0, 2) +
array('Status' => 'waiting') +
array_slice($value, -1);
}
var_dump($arr);
gives the following array:
array(3) {
[0]=>
array(4) {
["NUMBER"]=>
int(67)
["TYPE"]=>
string(5) "Other"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(9) "3/32/2011"
}
[1]=>
array(4) {
["NUMBER"]=>
int(87)
["TYPE"]=>
string(9) "something"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(9) "3/28/2011"
}
[2]=>
array(4) {
["NUMBER"]=>
int(67)
["TYPE"]=>
string(5) "Other"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(8) "3/2/2011"
}
}
I'm having difficulty constructing a multidimensional array based on queries fetched from a mysql database. The purpose is to prepare the result for json encoding. Having problems here.
Structure I’m aiming for:
Array (
[68] => Array (
[0] => Array (
[id] => 64
[description] => yada, yada, yada... )
[1] => Array (
[id] => 65
[description] => yada, yada, yada...)
[2] => Array (
[id] => 66
[description] => yada, yada, yada... ) )
[69] => Array (
[0] => Array (
[id] => 64
[description] => yada, yada, yada... )
[1] => Array (
[id] => 65
[description] => yada, yada, yada...)
[2] => Array (
[id] => 66
[description] => yada, yada, yada... ) )
...etc
)
From:
//this is a list of unknown length
$array_ids = (68, 69, 70, etc... ); // or:
Array (
[0] => 68
[1] => 69
[2] => 70
[3] => 71
etc..
)
//this is a known length
$array_contents = ( array ( array ( [id], [description]) ); // or:
Array (
[0] => Array (
[id] => 64
[description] =>yada, yada, yada... )
[1] => Array (
[id] => 65
[description] => yada, yada, yada... )
[2] => Array (
[id] => 66
[description] => yada, yada, yada... ) )
My attempt:
foreach($array_ids as $row){
$result = array($row=>array());
foreach($array_contents as $key => $value){
$result [$row][$key] = $value;
}
}
Result:
Array (
[68] => Array (
[0] => Array (
[id] => 64
[description] => yada, yada, yada... )
[1] => Array (
[id] => 65
[description] => yada, yada, yada...)
[2] => Array (
[id] => 66
[description] => yada, yada, yada... ) )
)
...and that’s where it stops. It doesn’t go on to the next row id of 69, 70, etc...
What am I doing wrong?
This line seems to be the problem:
$result = array($row=>array());
Every pass through $array_ids it's completely wiping out $result and repopulating it with one single item from $array_contents, rather than appending it to the end.
Replacing it with this should work:
$result[$row] = array();
Here's a full working version:
$array_ids = array(68,69,70);
$array_contents = array(
array(
'id' => 64,
'description' => 'yada, yada, yada...'
),
array(
'id' => 65,
'description' => 'yada, yada, yada2...'
),
array(
'id' => 66,
'description' => 'yada, yada, yada3...'
),
);
foreach($array_ids as $row){
$result[$row] = array();
foreach($array_contents as $key => $value){
$result [$row][$key] = $value;
}
}
var_dump($result);
?>
Here's the output:
array(3) {
[68]=>
array(3) {
[0]=>
array(2) {
["id"]=>
int(64)
["description"]=>
string(19) "yada, yada, yada..."
}
[1]=>
array(2) {
["id"]=>
int(65)
["description"]=>
string(20) "yada, yada, yada2..."
}
[2]=>
array(2) {
["id"]=>
int(66)
["description"]=>
string(20) "yada, yada, yada3..."
}
}
[69]=>
array(3) {
[0]=>
array(2) {
["id"]=>
int(64)
["description"]=>
string(19) "yada, yada, yada..."
}
[1]=>
array(2) {
["id"]=>
int(65)
["description"]=>
string(20) "yada, yada, yada2..."
}
[2]=>
array(2) {
["id"]=>
int(66)
["description"]=>
string(20) "yada, yada, yada3..."
}
}
[70]=>
array(3) {
[0]=>
array(2) {
["id"]=>
int(64)
["description"]=>
string(19) "yada, yada, yada..."
}
[1]=>
array(2) {
["id"]=>
int(65)
["description"]=>
string(20) "yada, yada, yada2..."
}
[2]=>
array(2) {
["id"]=>
int(66)
["description"]=>
string(20) "yada, yada, yada3..."
}
}
}
I tried with following array structure and logic its working , My logic is bit different
$array = array(68,69,70);
$nextArray = array(
array(
'id'=>64,
'desc'=>'test'
),
array(
'id'=>65,
'desc'=>'test'
),
array(
'id'=>66,
'desc'=>'test'
),
array(
'id'=>67,
'desc'=>'test'
),
);
$tempArray = array();
foreach($array as $value){
foreach($nextArray as $key => $value1){
$tempArray[$value][$key] = $value1;
}
}
var_dump($tempArray);
output :
array
68 =>
array
0 =>
array
'id' => int 64
'desc' => string 'test' (length=4)
1 =>
array
'id' => int 65
'desc' => string 'test' (length=4)
2 =>
array
'id' => int 66
'desc' => string 'test' (length=4)
3 =>
array
'id' => int 67
'desc' => string 'test' (length=4)
69 =>
array
0 =>
array
'id' => int 64
'desc' => string 'test' (length=4)
1 =>
array
'id' => int 65
'desc' => string 'test' (length=4)
2 =>
array
'id' => int 66
'desc' => string 'test' (length=4)
3 =>
array
'id' => int 67
'desc' => string 'test' (length=4)
70 =>
array
0 =>
array
'id' => int 64
'desc' => string 'test' (length=4)
1 =>
array
'id' => int 65
'desc' => string 'test' (length=4)
2 =>
array
'id' => int 66
'desc' => string 'test' (length=4)
3 =>
array
'id' => int 67
'desc' => string 'test' (length=4)
Try to change your code following way & shorter
$result = array();
foreach($array_ids as $row){
foreach($array_contents as $key => $value){
$result [$row][$key] = $value;
}
}
OK, having re-read the question, try the following:
$outputArray = array();
foreach( $array_ids as $id ){
$outputArray[$id] = $array_contents;
}