2

How would I go about building output like this in PHP:

$items[] = array(
    'section_name' => 'Section 1 Name',
    'items' => array(
        array(
            'item_name' => 'Item 1 Name - Section 1'
            // there will be more in here
        ),
        array(
            'item_name' => 'Item 2 Name - Section 1'
            // there will be more in here
        )
    ) 
);
$items[] = array(
    'section_name' => 'Section 2 Name',
    'items' => array(
        array(
            'item_name' => 'Item 1 Name - Section 2'
            // there will be more in here
        ),
        array(
            'item_name' => 'Item 2 Name - Section 2'
            // there will be more in here
        )
    ) 
);
// and so on

with this as the input

[section] => Array (
    [0] => Array (
        [name] => Section 1 Name
        [item] => Array (
             [0] => Array (
                [name] => Item 1 Name - Section 1
                // there will be more in here
             )
             [1] => Array (
                [name] => Item 2 Name - Section 1
                // there will be more in here                   
             )

        )
    )
    [1] => Array (
        [name] => Section 2 Name
        [item] => Array (
            [0] => Array (
                [name] => Item 1 Name - Section 2
                // there will be more in here
            )

       )
   )

)

There will never be a set number of items in a section and the number of sections will vary too so I need something iterative then a fixed number.

1
  • So what do you want is just to change the keys names ? Commented Aug 10, 2014 at 14:16

3 Answers 3

1

Something like this ? :

$sections = [];
for ($sectionIndex = 0; $sectionIndex < 10; ++$sectionIndex) {
  $items = [];
  for ($itemIndex = 0; $itemIndex < 10; ++$itemIndex) {
    $items[] = [
      'item_name' => sprintf('Item %d Name - Section %d', $itemIndex, $sectionIndex);       
    ]; 
  }
  $sections[] = [
    'section_name' => sprintf("Section %d Name", $sectionIndex),
    'items' => $items
  ];
}

Replace [] by array, since I don't know which PHP version you're using.

Sign up to request clarification or add additional context in comments.

Comments

1

Pass this your input array $section

 $items=array();
 $item=array();
 foreach ($section as $row) { 
    $tmp=array();
   $tmp['section_name']=$row['name'];
   foreach($row['item'] as $key){
          $item[]['item_name']=$key['name'];
   }
   $tmp['items']=$item;
   $items[]=$tmp;
 }

 print_r($items);

1 Comment

This seems to mostly work although it compounds so the second Items array includes all the items from the first as well as the ones from the second
0

Solved with a slightly modified version of the answer from @NoDataFound

    $sections = [];
    $s = 0;
    foreach ($_POST['section'] as $section) {
        $s++;
        $items = [];
        $i = 0;
        foreach ($section['item'] as $item) {
            $i++;
            $items[] = [
                'item_name' => sprintf('Item %d Name - Section %d', $i, $s)   
            ]; 
        }
        $sections[] = [
            'section_name' => sprintf("Section %d Name", $s),
            'items' => $items
        ];
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.