如何在PHP中使用foreach循环迭代变量x?

时间:2014-05-26 09:27:57

标签: php html foreach netsuite

如何在循环内迭代x的值?它不会迭代。谢谢你的帮助!

  $x = 0;
    foreach($searchResponse->searchResult->recordList as $customer => $value) {
        $custInternalId = $value[$x]->internalId;
        $request = new GetRequest();
        $request->baseRef = new RecordRef();
        $request->baseRef->internalId = $custInternalId;
        $request->baseRef->type = "customer";
        $getResponse = $service->get($request);
        $cust = $getResponse->readResponse->record;
        $x++;
    }

4 个答案:

答案 0 :(得分:1)

要调试它,请打印数据:

foreach($searchResponse->searchResult->recordList as $customer => $value) { 
    var_dump($customer);
    var_dump($value);
    break;/*single record printing//
}

在删除敏感数据后发布我们的问题调查信息

这就是for each在PHP (see doc)中的工作原理:

foreach (array_expression as $key => $value) { /*statement*/ }

我的猜测是,您将了解如何从这些输出中获取客户的ID。发布他们的问题,我们会帮助您。

看起来您正在使用NetSuite的recordList,它应循环通过以获取记录和记录具有字段getter函数!

foreach ( $searchResponse->recordList as $record ){
  $internalId = $record->getField("internalId");
}

根据NetSuite Doc:

,我猜你想要类似这样的东西
foreach($searchResponse->searchResult->recordList as $customerKey => $customerData) {
  $custInternalId = $customerData->getField("internalId");
  $request = new GetRequest();
  $request->baseRef = new RecordRef();
  $request->baseRef->internalId = $custInternalId;
  $request->baseRef->type = "customer";
  $getResponse = $service->get($request);
  $cust = $getResponse->readResponse->record;
}

答案 1 :(得分:0)

在循环之前和之外声明/初始化$x

  $x = 0;
  foreach($searchResponse->searchResult->recordList as $customer => $value) {
      $custInternalId = $value[$x]->internalId;
      $request = new GetRequest();
      $request->baseRef = new RecordRef();
      $request->baseRef->internalId = $custInternalId;
      $request->baseRef->type = "customer";
      $getResponse = $service->get($request);
      $cust = $getResponse->readResponse->record;
      $x++;
  }

答案 2 :(得分:0)

在循环之前声明$ x:

 $x = 0;    
 foreach($searchResponse->searchResult->recordList as $customer => $value) {

      $custInternalId = $value[$x]->internalId;
      $request = new GetRequest();
      $request->baseRef = new RecordRef();
      $request->baseRef->internalId = $custInternalId;
      $request->baseRef->type = "customer";
      $getResponse = $service->get($request);
      $cust = $getResponse->readResponse->record;

      echo $x;
      $x++;
  }

在循环中,您可以回显$ x以查看值

答案 3 :(得分:0)

您可以使用$customer变量获取当前索引值,因为它是您的foreach循环的关键。