سؤال

I have the following code in one of my pages. Prior to this I execute a query that returns multiple rows keyed off of alias_code. This code creates an array of string arrays to me echoed into a javascript function for populating points on a graph. I've profiled this multiple times, but I still have the feeling that there's a more efficient way to do this. I do realize that I run the risk of running out of memory if my strings are too big, but I'll constrain this in my query since I'd like to avoid an additional sub array or the use of implode/join. Does anyone have any thoughts on speeding this up?

$detailArray = array();
$prevAliasCode = '';
$valuesStr = '';

while ($detailRow = mysqli_fetch_array($detailResult)) {
  $aliasCode = $detailRow['alias_code'];

  if ($aliasCode <> $prevAliasCode) {
    if ($valuesStr <> '')
      $detailArray[$prevAliasCode] = $valuesStr;

    $valuesStr = '';
  }

  if ($valuesStr <> '')
    $valuesStr = $valuesStr . ', ';

  $valuesStr = $valuesStr .
    "['" .
    $detailRow['as_of_date'] . "', " .
    $detailRow['difficulty'] . ", " .
    $detailRow['price_usd'] . "]";

  $prevAliasCode = $aliasCode;
}

$detailArray[$prevAliasCode] = $valuesStr;

لا يوجد حل صحيح

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top