Reputation: 611
I have the following array:
$items = array(
'item_1' => array('sub-item_1_A','sub-item_1_B', 'sub-item_1_C', 'sub-item_1_D', 'sub-item_1_E'),
'item_2' => array('sub-item_2_A','sub-item_2_B', 'sub-item_2_C'),
'item_3' => array('sub-item_3_A','sub-item_3_B', 'sub-item_3_C', , 'sub-item_3_D'),
'item_4' => array('sub-item_4_A','sub-item_4_B'),
'item_5' );
The first thing I want to do is return a list of just the items -- i.e., the following:
item_1, item_2, item_3, item_4, item_5
I need that list in and of itself.
I ALSO need to then use that list to create SEPARATE lists of each of the the sub-arrays. I.e., here is one list:
'sub-item_1_A','sub-item_1_B', 'sub-item_1_C', 'sub-item_1_D', 'sub-item_1_E'
And here is the next list:
'sub-item_2_A','sub-item_2_B', 'sub-item_2_C'
And so on.
Any ideas on how to do this?
Thanks.
UPDATE I do not want to return the sub-items as a string, but rather I just wish to be able to access the sub-items of each item individually.
Upvotes: 0
Views: 33
Reputation: 2995
$items = array(
'item_1' => array('sub-item_1_A','sub-item_1_B', 'sub-item_1_C', 'sub-item_1_D', 'sub-item_1_E'),
'item_2' => array('sub-item_2_A','sub-item_2_B', 'sub-item_2_C'),
'item_3' => array('sub-item_3_A','sub-item_3_B', 'sub-item_3_C', 'sub-item_3_D'),
'item_4' => array('sub-item_4_A','sub-item_4_B'),
'item_5' => '');
foreach($items as $key => $item)
{
echo '<h3>'.$key.'</h3>';
if(!empty($item))
{
foreach($item as $k => $v)
{
echo '<h5>'.$v.'</h5>';
}
}
}
This will give you
item_1
sub-item_1_A
sub-item_1_B
sub-item_1_C
sub-item_1_D
sub-item_1_E
item_2
sub-item_2_A
sub-item_2_B
sub-item_2_C
item_3
sub-item_3_A
sub-item_3_B
sub-item_3_C
sub-item_3_D
item_4
sub-item_4_A
sub-item_4_B
item_5
Upvotes: 1
Reputation: 9583
Online Check, A working example.
To make the "return a list of just the items" you need to use just array_keys
.
$list_of_first = array_keys($items);
return a list of just the items:
Array
(
[0] => item_1
[1] => item_2
[2] => item_3
[3] => item_4
[4] => item_5
)
You can change it to a list by using implode here.
And for the rest you need a foreach loop
and use the implode to get the array items as string like yo want.
foreach($items as $val){
echo implode(", ", $val)."<br/>";
}
List of sub array's:
sub-item_1_A, sub-item_1_B, sub-item_1_C, sub-item_1_D, sub-item_1_E
sub-item_2_A, sub-item_2_B, sub-item_2_C
sub-item_3_A, sub-item_3_B, sub-item_3_C, sub-item_3_D
sub-item_4_A, sub-item_4_B
sub-item_5_A, sub-item_5_B
Upvotes: 2
Reputation: 1130
Hope this will help:
first you need to get the all array keys and implode it to get the items list. To get sub items list you need to loop through the array values.
<?php
$items = array(
'item_1' => array('sub-item_1_A','sub-item_1_B', 'sub-item_1_C', 'sub-item_1_D', 'sub-item_1_E'),
'item_2' => array('sub-item_2_A','sub-item_2_B', 'sub-item_2_C'),
'item_3' => array('sub-item_3_A','sub-item_3_B', 'sub-item_3_C', 'sub-item_3_D'),
'item_4' => array('sub-item_4_A','sub-item_4_B'),
'item_5' => "");
echo $itemsList = implode(',',array_keys($items))."\n";
$subItemsList = array_values($items);
foreach ($subItemsList as $key=>$value) {
if (!empty($value))
echo $subItemsList = implode(',',$value)."\n";
}
Upvotes: 1