Problem adding a date to an array index

3

I am passing a date into an index of a Array . As looping is performed, this Array is being fed. The index is also generated through this looping. However, when starting the index, it displays only 1 character (as if it were "fragmented" within Array ). I think it's a simple mistake, though, I'm hitting myself for not having it yet.

$pcb_data = array();
while($r = $DB->fetchArray($result)){  
    $pcb_cmb_id = $r['pcb_cmb_id'];


    $pcb_data[$pcb_cmb_id] = date("d/m/Y",  strtotime($r['pcb_data']));
}

If I print $pcb_data[4] , for example, it will display "2", instead of a date (eg 12/12/1988).

Note: I cut the while code because the rest of the information has no relevance, in this case.

Can anyone help me, please?

    
asked by anonymous 28.08.2015 / 14:36

3 answers

3

I tried to replicate the same error but without success, see:

<?php 
$pcb_data = array();
$i = 0;

while($i < 2){                          
  $pcb_data[$i] = date("d/m/Y",  strtotime('2000-10-10'));
  $i++;
}

Output

Array
(
  [0] => 10/10/2000
  [1] => 10/10/2000
)

obs: I tried emulating in different versions of PHP here

Is the data coming in from the bank correct?

Does this index you have actually have this rule?

Are there any logs or errors displaying?

    
28.08.2015 / 15:01
1

Try this, I think you'll find out where the problem is occurring:

function formatDate($data) {
   return implode('-',array_reverse(explode("-", $data)));
}

$pcb_data = array();

while($r = $DB->fetchArray($result)){  
    $pcb_cmb_id = $r['pcb_cmb_id'];
    $pcb_data[] = array(
            'id'  => $r['pcb_cmb_id'],
            'data'=>  formatDate($r['pcb_data'])
           ); 
}

echo '<pre>';
print_r($pcb_data);
    
28.08.2015 / 15:42
-1
$pcb_data = array();
while($r = $DB->fetchArray($result)){
    $pcb_data[$r['pcb_cmb_id']] = date("d/m/Y",  strtotime($r['pcb_data']));
}

echo '<pre>';
print_r($pcb_data);
echo '<pre>';

There is no need to create variables for everything. It already puts right inside the array key and its value.

    
28.08.2015 / 15:51