问题描述
我正在图书馆管理系统中从这样的数据库中获取数据
I am fetching data from a database like this in a Library Management System
- 有关特定ID的用户的详细信息
- 从books_assigned表中获取分配给用户的所有图书
- 从图书"表中获取已分配图书的详细信息
这就是我在做什么
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] => [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
)
)
)
)
现在我想要的是books_details应该在assigned_books数组内,例如,如果分配了ID为1的书,我想在id为1的assigned_books索引上获取这本书的详细信息,而不是将其放在名为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 ,
推荐答案
假设
您的数组具有一组数据,而您的数组就是这样
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 ',
),
);
解决方案
$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...
}
}
输出
源阵列
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
)
)
未设置后的源阵列
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
)
)
未设置元素的临时数组
Array
(
[id] => 1
[title] => PHP Made Easy
[author] => ietel & Dietel
)
新阵列阵列推送
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
)
)
预览
这篇关于Codeigniter:数据库到多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!