使用php / codeigniter构建3D数组?

时间:2013-11-29 20:25:41

标签: php arrays codeigniter multidimensional-array

我要做的是创建一个三维数组。我有一个表links和一个表hashtags。链接可以包含许多主题标签。我将如何创建如下的数组?

如何创建三维数组?

所需的阵列输出:

Array
(
[0] => Array
    (
        [id] 3; 
        [title] edit.com
        [url] http://edit.com
        [user_id] 6
        [list_id]  10
        [rating]  0
        [clicks] ; 0
        [unique_clicks]  0
        [weight]  0
        [date_created] someday 
        [name] this that
        [username] @something
        [email]  any@gmail.com
        [password]  3b8751e0f3f81ea085329e44d1d28f2e105f4031efd02ff7430c05d88ce90dcbacaef2f4239882e784b3d133d8aadc88404e670b284b95ab6963b58d00a6b864
        [group]  1
        [image_name] 1383486967.jpg
        [intro]  blah blah
        [views]  1
        [last_login]  1385735401
        [link_id]  
        [rated]  down
        [list_title]  New Title
        [list_description]  New descriptions please!
        [status] 0
        [hashtag] => array(
                          [0] ['hashtag_name'] #iphones,
                          [1] ['hashtag_name'] #android,

                     ) 
        [current_link_id] => 88
        [link_date] => 2013-11-29 15:17:05
        [all_clicks] => 0
    )

)

当前数组输出:

Array
(
[0] => Array
    (
        [id] 3; 
        [title] edit.com
        [url] http://edit.com
        [user_id] 6
        [list_id]  10
        [rating]  0
        [clicks] ; 0
        [unique_clicks]  0
        [weight]  0
        [date_created] someday 
        [name] this that
        [username] @something
        [email]  any@gmail.com
        [password]  3b8751e0f3f81ea085329e44d1d28f2e105f4031efd02ff7430c05d88ce90dcbacaef2f4239882e784b3d133d8aadc88404e670b284b95ab6963b58d00a6b864
        [group]  1
        [image_name] 1383486967.jpg
        [intro]  blah blah
        [views]  1
        [last_login]  1385735401
        [link_id]  
        [rated]  down
        [list_title]  New Title
        [list_description]  New descriptions please!
        [status] 0
        [hashtag]  
        [current_link_id] => 88
        [link_date] => 2013-11-29 15:17:05
        [all_clicks] => 0
    )

)

型号:

public function get_latest(){

    $this->db->limit(100);
    $this->db->order_by('links.date_created', 'DESC');
    $this->db->select('*');
    $this->db->select('links.id as current_link_id');
    $this->db->select('links.date_created as link_date');
    $this->db->select('links.clicks as all_clicks');    
    $this->db->select('links.user_id as user_id');
    $this->db->from('links');   
    $this->db->join('users', 'links.user_id = users.id');
    $this->db->join('link_ratings', 'links.id = link_ratings.link_id','left');  
    $this->db->join('list', 'links.list_id = list.id','left');  
    $this->db->join('hashtags', 'links.id = hashtags.link_id','left');  

    $get_latest = $this->db->get();

    return $get_latest;

}

控制器:

public function index(){

    $curr_user = $this->session->userdata('id');

    $data['links'] = $this->links_model->get_latest();

    $data['main_content'] = "timeline/timeline";
    $this->load->view('templates/timeline_template', $data);
}

查看:

<?php

foreach($links->result_array() as $link){

    if($link['current_link_id'] == $link['link_id'] && $link['rated'] == "up"){
        $up = "voted_up";
        $down = "grey_down";    
        $currState = "up";                          
    }elseif($link['current_link_id'] == $link['link_id'] && $link['rated'] == "down"){
        $up = "grey_up";
        $down = "voted_down";
        $currState = "down";
    }else{
        $up = "grey_up";
        $down = "grey_down";
        $currState = "not_voted";   
    }       

    $list_url = strtolower( str_replace(" ", "-", $link['list_title']) );

    echo '<div class="eachLink item floatLeft">
          <div class="linkHdr">'.timespan(strtotime($link['link_date']), time()).' Ago</div>
          <div class="linkMain"> 
              <div class="listInfo floatLeft">'.$link['list_title'].' - '.$link['list_description'].'</div>
              <div class="rating floatRight">
              <div class="currState dinnyDisplay">'.$currState.'</div>
              <div class="up upAjax" data-id="'.$link['user_id'].'"><a href="#" data-toggle="popover"><img src="/images/'.$up.'.png"></a></div>
              <div class="dinnyDisplay the_post_id">'.$link['current_link_id'].'</div>
              <div class="dinnyDisplay list_id">'.$link['list_id'].'</div>            
              <h4>'.$link['rating'].'</h4>
              <div class="down downAjax" data-id="'.$link['user_id'].'"><a href="#" data-toggle="popover"><img src="/images/'.$down.'.png"></a></div>
              </div>    
          </div>
          <div class="seg_details">
            <img class="floatLeft" src="//a.fvicon.com/'.$link['url'].'?canAudit=false&width=30&height=30"><h3>'.$link['title'].'</h3>
            <div class="url">'.$link['url'].'</div>
          </div>
          <div class="seg_details">
            <div class="smallPostImg floatLeft"><img src="/images/user/'.$link['image_name'].'">&nbsp;&nbsp;&nbsp;'.$link['name'].'</div>
          </div>
          <div class="featureList"> 
          <div class="reuse eachFeature floatLeft" data-id="'.$link['list_id'].'"><a href="#" data-toggle="tooltip">REUSE LIST</a></div> 
          <a data-toggle="modal" href="#myModal" class="md-btn eachFeature floatLeft" data-id="'.$link['current_link_id'].'">REUSE LINK</a>
          <div class="md-btn eachFeature floatLeft">'.$link['all_clicks'].' CLICKS </div>
          </div>
          </div>';

?> 

<?php           

}

?>

我可能没有让自己非常清楚,但如果你不确定,请联系我,我会尝试澄清我的意思。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

一般来说,只需使用索引对其进行处理即可创建数组。 例如,你做

  

for($ x = 0; $ z&lt; 10); $ z ++)// X轴

        {

        for ($y = 0; $x < 10; $x++) // Y-Axis
            {
            for ($z = 0; $y < 10; $y++) // Z-Axis
                {                
                $example[$x][$y][$z] = 'Sample'; // Possible DB input here
             }

         }>                     }`

因此,您必须迭代数据结果并将其存储在数组中。