R
R
RussianMan2015-03-09 00:34:30
PHP
RussianMan, 2015-03-09 00:34:30

How to concatenate array rows?

The database contains entries in the style:

Array ( [id] => 1 [data] => 03.03.15 09:28:46 [latitude] => 49.23342514 [longitude] => 28.40869522 [ppm] => ppm= ) 
Array ( [id] => 2 [data] => 03.03.15 09:28:46 [latitude] => 49.23342514 [longitude] => 28.40869522 [ppm] => 701 ) 
Array ( [id] => 3 [data] => 03.03.15 09:28:47 [latitude] => 49.23335648 [longitude] => 28.40859604 [ppm] => ppm ) 
Array ( [id] => 4 [data] => 03.03.15 09:28:47 [latitude] => 49.23335648 [longitude] => 28.40859604 [ppm] => =679 ) 
Array ( [id] => 5 [data] => 03.03.15 09:28:48 [latitude] => 49.23325729 [longitude] => 28.40863609 [ppm] => pp ) 
Array ( [id] => 6 [data] => 03.03.15 09:28:48 [latitude] => 49.23325729 [longitude] => 28.40863609 [ppm] => m=65 ) 
Array ( [id] => 7 [data] => 03.03.15 09:28:48 [latitude] => 49.23325729 [longitude] => 28.40863609 [ppm] => 6 )

The output should be something like this:
Array ( [id] => 1 [data] => 03.03.15 09:28:46 [latitude] => 49.23342514 [longitude] => 28.40869522 [ppm] => ppm=701 ) 
Array ( [id] => 3 [data] => 03.03.15 09:28:47 [latitude] => 49.23335648 [longitude] => 28.40859604 [ppm] => ppm=679) 
Array ( [id] => 5 [data] => 03.03.15 09:28:48 [latitude] => 49.23325729 [longitude] => 28.40863609 [ppm] => ppm=656 )

those. combine what is in the ppm column by the common data column
how to implement this? can't think of

Answer the question

In order to leave comments, you need to log in

5 answer(s)
K
Kirill Saksin, 2015-03-09
@saksmt

And it's even easier - to store classes at once or filter at the sampling stage.

<?php

class GeoPosition {
  private $data;

  public function __construct(array $data)
  {
    $this->data = $data;
  }

  public function getData()
  {
    return $this->data;
  }

  public function __toString()
  {
    return $data['data'];
  }
}

/** @var array[] $dbResponse */
$normalizedData = array_map(function (array $geoPosition) {
  return new GeoPosition($geoPosition);
}, $dbResponse);

$filteredData = array_unique($normalizedData, SORT_STRING);
$denormalized = array_map(function (GeoPosition $geoPosition) {
  return $geoPosition->getData();
}, $filteredData);

K
kompi, 2015-03-09
@kompi

if(($length = count($arr)) > 1)
    for($i = 1, $row = $arr[0]; $i < $length; $i++) {
      if($row['data'] === $arr[$i]['data'])
        $arr[$i]['ppm'] = $row['ppm'] . $arr[$i]['ppm'];
      else
        $row = $arr[$i];
    }

V
Vladimir Shiklgruber, 2015-03-09
@mnjghgmjyt

immediately normal database design ...

M
Mykola, 2015-03-09
@iSensetivity

array_unique() ?

D
Denis, 2015-03-11
@prototype_denis

// $array = [ ['id' => 1...
$result = [];
foreach ($array as $item) {
    if (isset($result[$item['data']])) {
        $result[$item['data']]['ppm'] .= $item['ppm'];
    } else {
        $result[$item['data']] = $item;
    }
}
$result = array_values($result);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question