2016-12-15 351 views
0

从mysql数据库中提取一些数据并将其保存到变量后,我想知道是否可以“查询”变量而不是对数据库执行另一个请求?我意识到我需要根据键和值来搜索一个对象数组。所以这里是我所拥有的一个例子。在对象数组中搜索特定键值对

<?php 
[{"customer":1,"item":1,"bought_at":"2016-12-15 11:41:11"}, 
{"customer":2,"item":1,"bought_at":"2016-12-15 11:43:21"}, 
{"customer":3,"item":1,"bought_at":"2016-12-16 13:31:11"}, 
{"customer":1,"item":2,"bought_at":"2016-12-16 12:12:21"}, 
{"customer":1,"item":3,"bought_at":"2016-12-17 15:13:58"}] 
?> 

因此,可以说我需要根据物品编号和日期(但不是时间)搜索物品时购买物品。下一步将返回结果作为另一个对象数组。因此,如果我要搜索2016-12-15购买的商品1,它会返回。

[{"customer":1,"item":1,"bought_at":"2016-12-15 11:41:11"}, 
{"customer":2,"item":1,"bought_at":"2016-12-15 11:41:21"},] 

这可能吗?如果是的话,我会怎么做呢? Regards

编辑:我最初问这个问题的原因是因为我有一个查询内嵌入的foreach循环困扰着我。这是一段代码,在后端构建一个json表,将信息传递到前端以绘制谷歌折线图。此外,我在原始问题中稍微更改了数据,以便更易于阅读。它也建在Laravel。完整的代码非常大,所以我只是发布嵌套的foreach循环。查询在第二个循环中,并被赋予变量$ activations。

foreach ($timeRange as $time){ 
     $temp = array(); 
     $timeTwentyFour = date("G", strtotime($time)); 
     $temp[] = array('v' => "Date(01,01,2000,$timeTwentyFour)"); 

     foreach($data as $row){ 
      $count = 0; 
      $activations = DB::table('customer_display')->where('display_id',$row->id)->where(DB::raw('DATE(created_at)'),$day)->get(); 

      foreach($activations as $activation){ 
       $timestamp = $activation->created_at; 
       $activationTime = explode(" ", $timestamp)[1]; 

       if (strtotime($activationTime) >= strtotime($time) && strtotime($activationTime) < strtotime($time) + 3600){ 
        $count++; 
       }; 
      } 
      $temp[] = array('v' => (float) $count); 
      //The custom tooltip 
      $temp[] = array('v' => $time . ' ' . $row->location . '. ' . $count . ($count == 1 ? ' Activation' : ' Activations')); 
     } 
     $rows[] = array('c' => $temp); 
    } 

回答

0

如果这是一个数组对象,你只想要的条目,其中item1你可以使用array_filter;

$filtered = array_filter($items, function($item){ 
    // only return objects where this is true 
    return $item->item == 1; 
}); 

如果你想购买只在第15使用

return date('d', strtotime($item->bought_at)) == 15; 

,如果你想看到的项目1 15号买你会使用

$filtered = array_filter($items, function($item){ 
    return $item->item === 1 
     && date('d', strtotime($item->bought_at)) == 15; 
}); 

项目还检查了this answer on comparing dates了解更多信息,了解如何更好地做到这一点。

+0

谢谢你,虽然我没有在数组是一个对象里面的时候意识到这一点。所以它实际上是对象 - >数组 - >对象 – Dalek

0

在大多数情况下,另一个数据库请求将是更好的方法。数据库针对查询数据进行了优化。它可以使用索引等。像MySQL这样的知名数据库有一个查询优化。手工操作效率会降低。 首先下载太多的数据,然后使用类似array_filter的东西来线性搜索所有数据的效率远低于查询中使用搜索条件查询数据的效率。

一种方式来做到这一点是:

//Prepare statement once 
$statement = $pdo->prepare("SELECT * FROM table WHERE item = ? AND bought_at = ?"); 

$statement->execute(array(1, "2016-12-15")); 
foreach ($statement->fetchAll() as $array) 
    //Do something with $array 

//reuse prepared statement with another selection criteria 
$statement->execute(array(3, "2016-12-16")); 
foreach ($statement->fetchAll() as $array) 
    //Do something with $array 
+0

我只关心我的查询当前处于foreach循环,我认为从数据库中做1拉,然后“查询”数组会更好,因为这意味着更少的调用到数据库。 – Dalek

+0

也许你可以用你的一些代码重写你的问题。这使得外部人员更容易给出答案。你看过准备好的陈述吗? –

+0

我的问题会根本不同。我试图删除这个问题来创建一个新的问题,但它不会让我:(。也许这对别人有用,你认为我应该提出一个全新的问题,并留下这个问题吗? – Dalek