[英]Codeigniter : Database to multidimensional array
我正在图书馆管理系统中从这样的数据库中获取数据
这就是我在做什么
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应该在assigned_books数组之内,例如,如果分配了ID为1的书,我想针对id为1的assign_books索引获取这本书的详细信息,而不是将其放在名为books_details的其他索引上,有人可以帮助我改变我的逻辑并解决这个问题,
我认为您应该将来自两个不同数组的数据存储在一个多维数组中。 语法就在这里。
$multiArr[]=[$arr1['value'],$arr2['value2']];
我在需要x和y轴的图表中使用它。
您的数组具有一组数据,并且您的数组是这样的
$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...
}
}
不带<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] => 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
)
)
预习
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.