我如何从两个表格中提取数据

时间:2018-01-24 08:31:28

标签: php mysql database laravel

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来自这两个表的两个数据吗?

1 个答案:

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

&#13;
&#13;
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;
&#13;
&#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