Pass array to Modal

1

I have the following array $detalhes :

 Array ( [0] => Array ( [dt_detalhes] => 2016-03-09 [desc_detalhes] => Viabilidade [tp_processo] => Viabilidade [vl_protocolo] => 1234 ) 
         [1] => Array ( [dt_detalhes] => 2016-03-12 [desc_detalhes] => Sincronizado [tp_processo] => Sincronizado [vl_protocolo] => 12345 ) 
         [2] => Array ( [dt_detalhes] => 2016-03-11 [desc_detalhes] => Integrador [tp_processo] => Integrador [vl_protocolo] => 123456 ) )

I would like to pass it into a modal, like this:

<button type="button" data-toggle="modal" data-target="#detalhes" 
data-id="<?=$linha['id_processo']?>" data-detalhes="<?=$details?>">

How can I do this? In the way tried above the following error:

  

Array to String Conversion

OBS: I need to pass the entire array.

    
asked by anonymous 09.03.2016 / 17:49

2 answers

1

The echo is used to print scalar values (int, string float etc) except arrays and objects.

You can get the array and transform it into a json string with php and by javascript to convert an object.

<button id="button" type="button" data-toggle="modal" data-target="#detalhes" 
data-id="<?=$linha['id_processo']?>" data-detalhes="<?=json_encode($detalhes);?>">

No javascript:

var data = document.getElementById('button');
var json = JSON.parse(data.getAttribute('data-detalhes'));
console.log(json);
    
09.03.2016 / 18:07
1

If you want to print specific data from the array, you need to print like this:

echo $details[0]['dt_detalhes'];

It will print the dt_detalhes field of the first item in the array 0

If you want to print all fields and all arrays, you should run them using some loop function, for example:

for($i=0; $i < count($details); $i++) {
    echo $details[$i]['dt_detalhes']; // campo desejado, exemplo 'vl_protocolo'
}

To better understand how an array works read the documentation , if in doubt, post here .

    
09.03.2016 / 18:22