Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What I'm trying to do is create a 3 dimensional array. I have a table links and a table hashtags. A link can contain many hashtags. How would I go about creating an array like the following?

How to create a 3 dimensional array?

Desired Array Output:

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]  [email protected]
        [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
    )

)

Current Array Output:

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]  [email protected]
        [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
    )

)

Model:

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;

}

Controller:

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);
}

View:

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

}

?>

I may not have made myself extremely clear but if you are unsure please get in touch and I will try clarify what I mean. Any help is greatly appreciated.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Generally speaking you can create an array by simply adressing it with the indices. For example you do

for ($x = 0; $z < 10); $z++) // X-Axis

        {

        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
             }

         }>                     }`

So you have to iterate over your db results and store it in the array.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.