Reputation: 5933
Problem:
I have two array where one produce a category and the second produce items for the category. I would like the items to be inside each category but I can't make it work.
PHP code:
foreach ($current['Children'] as $key => $value)
{
$query = "SELECT * FROM betyg_items WHERE CID = '{$key}' AND Status = '1' ORDER BY CID ASC";
$result = mysql_query($query) or die ('Database Error (' . mysql_errno() . ') ' . mysql_error());
$_SESSION['keys'][$key] = $value;
while ($row = mysql_fetch_assoc($result))
{
$_SESSION['items'][$row['IID']] = array(
'ID' => $row['IID'],
'CID' => $row['CID'],
'Description' => $row['Description']
);
}
}
$_SESSION['keys'] will contain:
Array
(
[2] => Integration av källorna
[3] => Belysning av egna resultat
[4] => Referenser
)
$_SESSION['items'] will contain:
Array
(
[1] => Array
(
[ID] => 1
[CID] => 2
[Description] => Källorna refereras separat
)
[2] => Array
(
[ID] => 2
[CID] => 2
[Description] => Vissa försök till sammanbindning
)
[3] => Array
(
[ID] => 6
[CID] => 3
[Description] => Inga jämförelser mellan egna och andras resultat
)
[4] => Array
(
[ID] => 7
[CID] => 3
[Description] => Kort redovisning av likheter och skillnader mellan egna och andras resultat
)
[5] => Array
(
[ID] => 11
[CID] => 4
[Description] => Många formella felaktigheter i referens- och litteraturförteckning; ej primärkällor
)
[6] => Array
(
[ID] => 12
[CID] => 4
[Description] => Vissa formella fel, vissa källor av mindre kvalitet
)
)
Question:
How do I get each item into the array index it correspond by using the CID value?
Scenario:
For instance, all CID's that contain the number 2 should go into "[2] => Integration av källorna", and so forth.
Desired output:
Array
(
[2] => Integration av källorna
[1] => Array
(
[ID] => 1
[CID] => 2
[Description] => Källorna refereras separat
)
[2] => Array
(
[ID] => 2
[CID] => 2
[Description] => Vissa försök till sammanbindning
)
[3] => Belysning av egna resultat
[3] => Array
(
[ID] => 6
[CID] => 3
[Description] => Inga jämförelser mellan egna och andras resultat
)
[4] => Array
(
[ID] => 7
[CID] => 3
[Description] => Kort redovisning av likheter och skillnader mellan egna och andras resultat
)
[4] => Referenser
[5] => Array
(
[ID] => 11
[CID] => 4
[Description] => Många formella felaktigheter i referens- och litteraturförteckning; ej primärkällor
)
[6] => Array
(
[ID] => 12
[CID] => 4
[Description] => Vissa formella fel, vissa källor av mindre kvalitet
)
)
Upvotes: 0
Views: 1020
Reputation: 4511
$final_arr = array();
$i = 0;
foreach ($current['Children'] as $key => $value)
{
$query = "SELECT * FROM betyg_items WHERE CID = '{$key}' AND Status = '1' ORDER BY CID ASC";
$result = mysql_query($query) or die ('Database Error (' . mysql_errno() . ') ' . mysql_error());
$final_arr[$i]['key'] = $value;
while ($row = mysql_fetch_assoc($result))
{
$final_arr[$i]['items'][] = array(
'ID' => $row['IID'],
'CID' => $row['CID'],
'Description' => $row['Description']
);
}
$i++;
}
FINAL OUTPUT WILL BE: $final_arr
Array
(
[0] =>
['key']=> Integration av källorna
['items'] =>
[0] => Array
(
[ID] => 1
[CID] => 2
[Description] => Källorna refereras separat
)
[1] => Array
(
[ID] => 2
[CID] => 2
[Description] => Vissa försök till sammanbindning
)
Upvotes: 1