2

I want to get the data from table tbl_devedor_parcela, using the field negociacao_id, so I need to retrieve the data from table tbl_devedor_parcela as array. The first ID of the table tbl_devedor_parcela, is being retrieved as an array, but the others are being retrieved as an object.

enter image description here

How do I recover everything as an array?

Following tables;

tbl_devedor_negociacao

DROP TABLE IF EXISTS `tbl_devedor_negociacao`;
CREATE TABLE IF NOT EXISTS `tbl_devedor_negociacao` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `dt_negociacao` DATE NULL DEFAULT NULL,
  `atualizar` VARCHAR(5) NULL DEFAULT NULL,
  `id_finalizacao` INT(11) NULL DEFAULT NULL,
  `contrato_id` INT(11) NULL DEFAULT NULL,
  `crud` VARCHAR(2) NULL DEFAULT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB
AUTO_INCREMENT = 1;

tbl_devedor_parcela

DROP TABLE IF EXISTS `tbl_devedor_parcela`;
CREATE TABLE IF NOT EXISTS `tbl_devedor_parcela` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `dt_vencimento` DATE NULL DEFAULT NULL,
  `num_parcela` VARCHAR(20) NULL DEFAULT NULL,
  `valor` VARCHAR(20) NULL DEFAULT NULL,
  `negociacao_id` INT(11) NULL DEFAULT NULL,
  `atraso` VARCHAR(20) NULL DEFAULT NULL,
  `crud` VARCHAR(2) NULL DEFAULT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB
AUTO_INCREMENT = 1;

Source Model

//Excerpt from Source- Home
//Negotiations
$id_contratos = array_map(function($item) {
    return $item['id'];
}, $result['contratos']);
$negociacoes = $this->obter_negociacao($id_contratos);    

//Parcelas
$id_parcelas = array_map(function($item) {
    return $item['id'];
}, $negociacoes);
$parcelas = $this->obter_parcela($id_parcelas);

//view
$id_contratos = array_map(function($item) {
    return $item['id'];
}, $result['contratos']);
$empresa = "Banco Semear";
$view["empresa"] = $empresa;

for ($i = 0; $i < count($result['contratos']); $i++) {
    $id = $result['contratos'][$i]['id'];
    $result['contratos'][$i]['view'] =
        array_filter($view, function($a) {
            return $a;
        });
}

for ($i = 0; $i < count($negociacoes); $i++) {
    $id = $negociacoes[$i]['id'];
    $negociacoes[$i]['parcelas'] =
        array_filter($parcelas, function($a) use($id) {
            return $a['negociacao_id'] == $id;
        });
}

for ($i = 0; $i < count($result['contratos']); $i++) {
    $id = $result['contratos'][$i]['id'];
    $result['contratos'][$i]['negociacoes'] =
        array_filter($negociacoes, function($a) use($id) {
            return $a['contrato_id'] == $id;
        });
}

return $result;
//Excerpt from source - End

//get Negotiations
public function obter_negociacao($id) {
    $this->db->from($this->tbl_devedor_negociacao);
    $this->db->select("tbl_devedor_negociacao.*, IF(tbl_devedor_negociacao.crud = 'C', 'R', 'C') as crud", FALSE);
    if (is_array($id)) {
        $this->db->where_in('contrato_id', $id);
    }
    else
    {
        $this->db->where('contrato_id', $id);
    }
    $this->db-> order_by('contrato_id');
    $query = $this->db->get();
    return $query->result_array();
}
//get parcela
public function obter_parcela($id) {
    $this->db->from($this->tbl_devedor_parcela);
    $this->db->select("tbl_devedor_parcela.*, IF(tbl_devedor_parcela.crud = 'C', 'R', 'C') as crud", FALSE);
    if (is_array($id)) {
        $this->db->where_in('negociacao_id', $id);
    }
    else
    {
        $this->db->where('negociacao_id', $id);
    }

    $query = $this->db->get('');
    return $query->result_array();
}
0

1 Answer 1

1

I got success with array_values

for ($i = 0; $i < count($negociacoes); $i++)
{ 
    $id = $negociacoes[$i]['id'];
    $negociacoes[$i]['parcelas'] =
       array_values(array_filter($parcelas, function($a) use($id)
       {
           return $a['negociacao_id'] == $id;
       }
   )); 
}

answer here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.