Mysql
 SQL >> Database >  >> RDS >> Mysql

Salvare un array multidimensionale CodeIgniter in una colonna MySQL

Il messaggio indica che non esiste la chiave $data['view_count']; il valore si trova in $data[2017][6][10]['count']. Se la struttura segue sempre $data[anno][mese][giorno]['count'], puoi usare un iteratore ricorsivo:

private function getCount($arr, $search) {
  $iterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator($arr)
  );
  foreach ($iterator as $key => $value) {
    if ($search == $key && $value !== '') {
      return $value;
    }
  }
  return false;
}

Poi nel metodo di salvataggio:

public function setView($data) {
  $count = $this->getCount($data, 'count');
  if ($count !== false) {
    return $this->db->where('short', 'honwl')
      ->update('ci_links', $count);
  }
  return false;
}