检索数组的子数组

时间:2013-07-12 06:42:43

标签: php arrays codeigniter opencart

我正在尝试使用数组对我的标题进行分类。但似乎我碰壁了。我的数组是数组中的数组,第一个数组包含group_id,子数组包含有关标题的所有信息。我正在尝试的是返回与group_id对应的表中的数组。目前有第1组和第2组,但将来我会添加更多组。我被卡住的部分是我不能或我不知道如何以某种方式在数组中选择组的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 |
+-------+------------+--------+

3 个答案:

答案 0 :(得分:0)

你在寻找类似的东西 -

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>';

答案 1 :(得分:0)

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

运行此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 ?>

答案 2 :(得分:0)

我希望这会奏效。

<?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 ?>