【问题标题】:Codeigniter : Database to multidimensional arrayCodeigniter:数据库到多维数组
【发布时间】:2016-12-07 03:10:36
【问题描述】:

我正在图书馆管理系统中从这样的数据库中获取数据

  1. 来自特定 id 的用户的详细信息
  2. 从 books_assigned 表中获取分配给用户的所有图书
  3. 从书籍表中获取指定书籍的详细信息

这就是我正在做的事情

public function userhistory($id){
            $query= $this->db->get_where('user',array(
                'id'    => $id,
            ));
            $result['user']= $query->result();

            $query_books =  $this->db->get_where('book_assign', array(
               'user_id'  =>$result['user'][0]->id,

            ));

                foreach ($query_books->result() as $key => $value) {
                    $result['assigned_books']= $query_books->result(); 
                    $query_book = $this->db->get_where('books',array (
                        'id' => $query_books->result()[$key]->book_id)
                    );
                    $result['books_details'][]= $query_book->result();
                } 
                echo '<pre>';
                print_r($result);
                echo '</pre>';
                die;
   }

这就是我获得print_r($result); 的结果的方式

Array
(
    [user] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 5
                    [name] => 
                    [email] => test@test.com
                    [password] => test
                    [role] => 
                    [status] => 1
                )

        )

    [assigned_books] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1
                    [user_id] => 5
                    [book_id] => 1
                    [date_issue] => 2016-07-24 00:00:00
                    [date_return] => 2016-07-25 00:00:00
                )

        )

    [books_details] => Array
        (
            [0] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 1
                            [title] => PHP Made Easy 
                            [author] => Dietel & Dietel 
                            [serial_no] => 232323
                            [qty] => 8
                            [row_no] => 1
                            [col_no] => 2
                            [status] => 1
                            [category_id] => 1
                            [description] => This is a book about php 
                        )

                )

        )

)

现在我想要的是 books_details 应该在 assign_books 数组中,例如,如果分配了 id 为 1 的书,我想在 assign_books 索引上获取这本书的详细信息,而不是在另一个名为 books_details 的索引上帮助我改变我的逻辑并解决这个问题,

【问题讨论】:

  • 检查下面添加的答案。它解决了你的问题

标签: php mysql arrays codeigniter


【解决方案1】:

我认为您应该将来自两个不同数组的数据存储在一个多维数组中。语法就在这里。

$multiArr[]=[$arr1['value'],$arr2['value2']];

我在需要 x 和 y 轴的图表中使用它。

【讨论】:

    【解决方案2】:

    假设

    你的数组有一组数据,你的数组是这样的

    $array = array(
        'user' => array(
            'id' => 5, 
            'name' => 'test@test.com', 
            'email' => 'test', 
            'password' => '', 
            'role' => 'role', 
            ), 
        'assigned_books' => array(
            'id' => 1, 
            'user_id' => 5, 
            'book_id' => 1, 
            'date_issue' => '2016-07-24 00:00:00', 
            'date_return' => '2016-07-25 00:00:00'
            ), 
        'books_details' => array(
            'id' => 1, 
            'title' => 'PHP Made Easy' , 
            'author' => 'ietel & Dietel ', 
            ), 
        );
    

    解决方案

    $tmp = array();
    $new_array = array();
    
    for ($i = 0; $i < count($array)/3; $i++) {
    
        if ($array['assigned_books']['book_id'] == $array['books_details']['id'] ) 
        {
            echo "<pre>";
            echo "Source Array";
            print_r($array);
            echo "</pre>";
    
            $tmp = $array['books_details'];
    
            unset($array['books_details']);
    
            $new_array = $array; # Merging existing array
    
            $new_array['assigned_books']['book_id'] = array();
            array_push($new_array['assigned_books']['book_id'], $tmp);
    
            echo "<pre>";
            echo "<b>Source Array after unset</b>";
            print_r($array);
            echo "<b>Temp Array of Unset element</b>";
            print_r($tmp);
            echo "<b> <em>New Array Array push</em></b>";
            print_r($new_array);
            echo "</pre>";
        }
         else {
            # code...
        }
    }
    

    没有&lt;pre&gt;的源代码

    $tmp = array();
    $new_array = array();
    
    for ($i = 0; $i < count($array)/3; $i++) {
    
        if ($array['assigned_books']['book_id'] == $array['books_details']['id'] ) 
        {
    
            $tmp = $array['books_details'];
    
            unset($array['books_details']);
    
            $new_array = $array; # Merging existing array
    
            $new_array['assigned_books']['book_id'] = array();
            array_push($new_array['assigned_books']['book_id'], $tmp);
            print_r($new_array);
        }
         else {
            # code...
        }
    }
    

    输出

    源数组

    Array
    (
        [user] => Array
            (
                [id] => 5
                [name] => test@test.com
                [email] => test
                [password] => 
                [role] => role
            )
    
        [assigned_books] => Array
            (
                [id] => 1
                [user_id] => 5
                [book_id] => 1
                [date_issue] => 2016-07-24 00:00:00
                [date_return] => 2016-07-25 00:00:00
            )
    
        [books_details] => Array
            (
                [id] => 1
                [title] => PHP Made Easy
                [author] => ietel & Dietel 
            )
    
    )
    

    未设置后的源数组

    Array
    (
        [user] => Array
            (
                [id] => 5
                [name] => test@test.com
                [email] => test
                [password] => 
                [role] => role
            )
    
        [assigned_books] => Array
            (
                [id] => 1
                [user_id] => 5
                [book_id] => 1
                [date_issue] => 2016-07-24 00:00:00
                [date_return] => 2016-07-25 00:00:00
            )
    
    )
    

    未设置元素的临时数组

    Array
    (
        [id] => 1
        [title] => PHP Made Easy
        [author] => ietel & Dietel 
    )
    

    新数组数组推送

    Array
    (
        [user] => Array
            (
                [id] => 5
                [name] => test@test.com
                [email] => test
                [password] => 
                [role] => role
            )
    
        [assigned_books] => Array
            (
                [id] => 1
                [user_id] => 5
                [book_id] => Array
                    (
                        [0] => Array
                            (
                                [id] => 1
                                [title] => PHP Made Easy
                                [author] => ietel & Dietel 
                            )
    
                    )
    
                [date_issue] => 2016-07-24 00:00:00
                [date_return] => 2016-07-25 00:00:00
            )
    
    )
    

    预览

    1. phpfiddle.org

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-24
      • 2011-01-11
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 2021-03-07
      相关资源
      最近更新 更多