检索数组的子数组


Retrieve subarray of array

我正在尝试使用数组对标题进行分类。但我好像碰到了一堵墙。我的数组是数组中的一个数组,第一个数组包含group_id,子数组包含关于标题的所有信息。我正在尝试的是返回与group_id对应的表中的数组。目前有第一组和第二组,但将来我会增加更多的组。我陷入困境的部分是,我不能或不知道如何选择组的id及其在数组中的内容,以便将其显示在正确的表中。我的项目基于Open cart。以下是我目前掌握的代码:这是观点:

     foreach($informations as $information)
       {
            echo '<table border="1"><tr><td>';   
            print_r($information);
            echo '</table></tr></td>';  
       }

这是阵列的控制器功能:

$information_total = $this->model_catalog_information->getTotalInformations();
$results = $this->model_catalog_information->getInformations($data);
foreach ($results as $result)
 {
   $action = array();
   $action[] = array(
        'text' => $this->language->get('text_edit'),
        'href' => $this->url->link('catalog/information/update', 'token=' . $this->session->data['token'] . '&information_id=' . $result['information_id'] . $url, 'SSL')
    );
   $this->data['informations'][] = array(  $result['groupe_id'] => array(
          'information_id' => $result['information_id'],
          'title'          => $result['title'],
          'master'         => $result['master'],
          'sort_order'     => $result['sort_order'],   
          'selected'       => isset($this->request->post['selected']) && in_array($result['information_id'], $this->request->post['selected']),
          'action'         => $action                               
    ));
     }

Tnx提前寻求帮助。如果我没有足够清楚地解释这一点,我很抱歉,因为英语不是我的第一语言:)如果有什么事情我没有解释清楚,问我,我会回答。

这就是阵列输出的:

Array
(
    [1] => Array
        (
            [information_id] => 12
            [title] => ABOUT USfdsfds
            [master] => AND OTHERSfdsfds
            [sort_order] => 0
            [selected] => 
            [action] => Array
                (
                    [0] => Array
                        (
                            [text] => Edit
                            [href] => http://localhost/opencart/admin/index.php?route=catalog/information/update&token=be54d82da3beb8855eb80bc49a4b28a4&information_id=12
                        )
                     )
                  )
                )
Array
(
    [1] => Array
        (
            [information_id] => 17
            [title] => dsfsdfds
            [master] => fdsfdsf
            [sort_order] => 0
            [selected] => 
            [action] => Array
                (
                    [0] => Array
                        (
                            [text] => Edit
                            [href] => http://localhost/opencart/admin/index.php?route=catalog/information/update&token=be54d82da3beb8855eb80bc49a4b28a4&information_id=17
                        )
                      )
                   )
                )
Array
(
    [1] => Array
        (
            [information_id] => 14
            [title] => eeee
            [master] => eeeee
            [sort_order] => 0
            [selected] => 
            [action] => Array
                (
                    [0] => Array
                        (
                            [text] => Edit
                            [href] => http://localhost/opencart/admin/index.php?route=catalog/information/update&token=be54d82da3beb8855eb80bc49a4b28a4&information_id=14
                        )
                      )
                    )
                  )
Array
(
    [2] => Array
        (
            [information_id] => 15
            [title] => fffffffffffff
            [master] => fffffffffffffff
            [sort_order] => 0
            [selected] => 
            [action] => Array
                (
                    [0] => Array
                        (
                            [text] => Edit
                            [href] => http://localhost/opencart/admin/index.php?route=catalog/information/update&token=be54d82da3beb8855eb80bc49a4b28a4&information_id=15
                        )
                      )
                    )
                  )
Array
(
    [2] => Array
        (
            [information_id] => 13
            [title] => Hello 
            [master] => Hello Again
            [sort_order] => 0
            [selected] => 
            [action] => Array
                (
                    [0] => Array
                        (
                            [text] => Edit
                            [href] => http://localhost/opencart/admin/index.php?route=catalog/information/update&token=be54d82da3beb8855eb80bc49a4b28a4&information_id=13
                        )
                      )
                   )
                 )
Array
(
    [2] => Array
        (
            [information_id] => 16
            [title] => ssssssssssssssss
            [master] => ssssssssssssssss
            [sort_order] => 0
            [selected] => 
            [action] => Array
                (
                    [0] => Array
                        (
                            [text] => Edit
                            [href] => http://localhost/opencart/admin/index.php?route=catalog/information/update&token=be54d82da3beb8855eb80bc49a4b28a4&information_id=16
                        )
                      )
                    )

我想让桌子看起来像这样:

+-------+------------+--------+
| Title | Sort order | Action |
+-------+------------+--------+

所以你在寻找这样的东西吗-

echo '<table border="1">';
echo '<tr><th>Title</th><th>Sort order</th><th>Action</th></tr>';
foreach($informations as $information){
        echo '<tr><td>'.$information['title'].'</td>';
        echo '<td>'.$information['sort_order'].'</td>';
        echo '<td>'.$information['action'][0]['text'].'</td></tr>';  
}
echo '</table>';

为了能够运行它,我更改了您代码的某些部分。

运行此php代码并查看结果。这可能会对您有所帮助:

<?php
//The Data:
$theselected=12;
$results=array(
        array(
            'groupe_id'=>1,
            'information_id'=>12,
            'title'=> 'abc',
            'master'=> 'm_abc',
            'sort_order'=>0
        ),
        array(
            'groupe_id'=>1,
            'information_id'=>17,
            'title'=> 'efg',
            'master'=> 'm_efg',
            'sort_order'=>0
        ),
        array(
            'groupe_id'=>2,
            'information_id'=>14,
            'title'=> 'abcdd',
            'master'=> 'm_abcdd',
            'sort_order'=>0
        )
    );

// The Controller:

$data['informations'] = array();
foreach ($results as $result)
{
    $action = array();
    $action = array(
        'text' => 'the_text',
        'href' => 'the_href'
    );
    if ( !isset($data['informations'][$result['groupe_id']]) )
        $data['informations'][$result['groupe_id']] = array();
    array_push(
        $data['informations'][$result['groupe_id']],
        array(
        'information_id' => $result['information_id'],
        'title'          => $result['title'],
        'master'         => $result['master'],
        'sort_order'     => $result['sort_order'],   
        'selected'       => '',
        'action'         => $action                               
        )
    );
}
?>

<?php // The View: ?>

<?php foreach($data['informations'] as $group_id => $information): ?>
    <h3>Group: <?php echo $group_id ?></h3>
    <table border="1">
    <thead><tr>
    <th>Information ID</th><th>title</th><th>master</th><th>sort order</th><th>Action</th>
    </tr></thead>
    <?php foreach ($information as $item): ?>
        <tr>
            <td><?php echo $item['information_id'] ?></td>
            <td><?php echo $item['title'] ?></td>
            <td><?php echo $item['master'] ?></td>
            <td><?php echo $item['sort_order'] ?></td>
            <td><?php echo $item['action']['text'] ?><br><?php echo $item['action']['href'] ?></td>
        </tr>
    <?php endforeach ?>
    </table>
<?php endforeach ?>

我希望这能奏效。

<?php foreach($informations as $group_id=>$information_array) : ?>
  <?php  foreach($information_array as $information) : ?>
        <tr><td> <?php echo $information['title']' ?></td>
        <td><?php echo $information['sort_order'] ?></td>
        <td>
         <?php  foreach($information['action'] as $action) : ?>
                 <a href="<?php echo $action['href'];?>?id=<?php echo $information['information_id']?>"><?php echo $action['text']?></a> &nbsp
          <?php endforeach ?> 
        </td>
  <?php endforeach ?>    
 <?php endforeach ?>