Codeigniter : Database to multidimensional array

最后都变了- 提交于 2019-12-25 08:14:41

问题


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

  1. Details about user from specific id
  2. Get all the books assigned to the user from books_assigned table
  3. Get details of the assigned books from books table

This is what i am doing

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

This is how i get result for 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 
                        )

                )

        )

)

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 ,


回答1:


I think you should store data from two different arrays in a single multidimensional array. The syntax is right here.

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

I use this in charts where I need both x and y axis.




回答2:


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

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

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 
        )

)

Source Array after unset

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
        )

)

Preview

  1. phpfiddle.org


来源:https://stackoverflow.com/questions/38708986/codeigniter-database-to-multidimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!