View Full Version : Array after a json decode

10-11-2012, 10:28 AM
My goal here is to take the value of the attachments field which looks like this:


And then run the for loop over the items. Attachments should also have an array inside of it. One index being is_file and the other being file_name. For each of the items I want to check to see if the files do exist in my directory. If they do then I want the attachment to have a value of TRUE for the is_file and FALSE if it is not. And for the file_name I want it to obviously have the file name.

$attachments = json_decode($attachments, TRUE);
for ($x = 0; $x < count($attachments); $x++)
$file_name = $attachments[$x];
if ($this->functions_model->is_file('assets/downloads/'.$file_name, FALSE) === TRUE)
$message_data->attachments['is_file'] = TRUE;
$message_data->attachments['is_file'] = FALSE;
$message_data->attachments['file_name'] = $file_name;

10-11-2012, 12:59 PM
function object_to_array($data){
if (is_array($data) || is_object($data)){
$result = array();
foreach($data as $key => $value){
$result[$key] = object_to_array($value);
return $result;
return $data;

$json = '["a.txt", "b.txt", "something.jpg"]';
$dir = './';

$array = object_to_array(json_decode($json));
$i = 0;
$fname = $dir . $array[$i];
$output[$i][0] = $array[$i];
$output[$i][1] = true;
$output[$i][1] = false;
$output[$i][1] = false;


will do ;) ?