public function queries($companyID, $entityType, $entityValue)
{
$data = [];
$details = DiraQuestion::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();
$detailsAns = DiraResponses::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();
foreach($details as $key => $detailsValue)
{
if(!array_key_exists($detailsValue->intent, $data))
{
$data[$detailsValue->intent] = [];
}
if(!array_key_exists($detailsValue->reply, $data[$detailsValue->intent]))
{
$data[$detailsValue->intent]['answer'][$detailsValue->reply] = $detailsValue->id;
}
if(!array_key_exists($detailsValue->queries, $data[$detailsValue->intent]))
{
$data[$detailsValue->intent]['question'][$detailsValue->queries] = $detailsValue->id;
}
}
ksort($data);
return view('AltHr.Chatbot.queries', compact('data','entityType','entityValue','companyID'));
}
我已经编写了一个代码来查看数据,但我遇到了一些问题。
从我写的代码中,它查看了一个表$details
中的数据。
但我也想查看$detailsAns
。
我该怎么做?我也可以foreach
来自这两个表的两个数据吗?
答案 0 :(得分:0)
第一种方法是使用普通for
而不是foreach
,
例如:
public function test(){
$data = [];
$details = ["details0", "details1", "details2", "details3", "details4", "details5", "details6", "details7"];
$detailsAns = ["details0Ans", "details1Ans", "details2Ans", "details3Ans", "details4Ans", "details5Ans", "details6Ans", "details7Ans"];
for ($i = 0 ; $i < sizeof($details) || $i < sizeof($detailsAns) ;$i++) {
echo $details[$i];
echo $detailsAns[$i];
}
}
第二种方式使用MultipleIterator::attachIterator
不是最有效的,而是SPL的multipleIterator
的演示$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($a));
$mi->attachIterator(new ArrayIterator($b));
$newArray = array();
foreach ( $mi as $value ) {
list($value1, $value2) = $value;
echo $value1 , '-' , $value2 , PHP_EOL;
}
public function queries($companyID, $entityType, $entityValue)
{
$data = [];
$details = DiraQuestion::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();
$detailsAns = DiraResponses::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();
$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($details));
$mi->attachIterator(new ArrayIterator($detailsAns));
foreach($mi as $value ) {
list($detailsValue, $detailAnsValue) = $value;
if(!array_key_exists($detailsValue->intent, $data))
{
$data[$detailsValue->intent] = [];
}
if(!array_key_exists($detailsValue->reply, $data[$detailsValue->intent]))
{
$data[$detailsValue->intent]['answer'][$detailsValue->reply] = $detailsValue->id;
}
if(!array_key_exists($detailsValue->queries, $data[$detailsValue->intent]))
{
$data[$detailsValue->intent]['question'][$detailsValue->queries] = $detailsValue->id;
}
}
ksort($data);
return view('AltHr.Chatbot.queries', compact('data','entityType','entityValue','companyID'));
}
&#13;
示例:强>
public function test(){
$data = [];
$details = ["details0", "details1", "details2", "details3", "details4", "details5", "details6", "details7"];
$detailsAns = ["details0Ans", "details1Ans", "details2Ans", "details3Ans", "details4Ans", "details5Ans", "details6Ans", "details7Ans"];
$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($details));
$mi->attachIterator(new ArrayIterator($detailsAns));
foreach($mi as $value ) {
list($detailsValue, $detailAnsValue) = $value;
echo($detailsValue);
echo($detailAnsValue);
}
}
//output: details0details0Ansdetails1details1Ansdetails2details2Ansdetails3details3Ansdetails4details4Ansdetails5details5Ansdetails6details6Ansdetails7details7Ans