0

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] => [email protected]
                    [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
  • check the answer added below. Its solve your problem Commented Aug 1, 2016 at 23:14

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Assumption

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

$array = array(
    'user' => array(
        'id' => 5, 
        'name' => '[email protected]', 
        '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] => [email protected]
            [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] => [email protected]
            [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] => [email protected]
            [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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.