Codeignter:数据库到多维数组 [英] Codeignter : Database to multidmensional array

查看:99
本文介绍了Codeignter:数据库到多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i am fetching data from a database like this in a Library Management System


  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结果);

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应该在assigned_books数组中,例如如果id为1的书被分配我想获得这本书的详细信息assigned_books索引反对id 1,而不是在一个不同的索引名为books_details,有人可以帮助我改变我的逻辑,并解决这个问题,

Now what i want is books_details should be within assigned_books array , for example if book with id 1 is assigned i want to get details for this book on assigned_books index against id 1 instead of getting it on a different index called books_details can someone help me to change my logic and fix this ,

推荐答案

< h2>假设

您的数组有一组数据,您的数组类似于此

Assumption

Your array having one set of data and your array is like this

$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 ', 
        ), 
    );






解决方案




Solution

$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...
    }
}

不含< pre> 源代码

Source Code without <pre>

$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...
    }
}






输出



源数组 b
$ b


Outputs

Source 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 
        )

)

取消设置后的源数组

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
        )

)

取消设置元素的临时数组

Temp Array of Unset element

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

新阵列数组推送 >

New Array Array push

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

  1. phpfiddle.org

这篇关于Codeignter:数据库到多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆