...

View Full Version : Array after a json decode



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

["file1.jpg","file2.jpg","file3.jpg"

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;
}
else
{
$message_data->attachments['is_file'] = FALSE;
}
$message_data->attachments['file_name'] = $file_name;
}

patryk
10-11-2012, 01:59 PM
<?php
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));
//print_r($array)
$i = 0;
while(isset($array[$i])){
$fname = $dir . $array[$i];
$output[$i][0] = $array[$i];
if(file_exists($fname)){
if(is_file($fname)){
$output[$i][1] = true;
}else{
$output[$i][1] = false;
}
}else{
$output[$i][1] = false;
}
$i++;
}

//print_r($output);
?>

will do ;) ?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum