Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-11-2012, 10:28 AM   PM User | #1
CoolAsCarlito
Regular Coder

 
Join Date: Jun 2008
Posts: 679
Thanks: 114
Thanked 2 Times in 2 Posts
CoolAsCarlito can only hope to improve
Array after a json decode

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.

PHP Code:
$attachments json_decode($attachmentsTRUE);
for (
$x 0$x count($attachments); $x++)
{
    
$file_name $attachments[$x];
    if (
$this->functions_model->is_file('assets/downloads/'.$file_nameFALSE) === TRUE)
    {
        
$message_data->attachments['is_file'] = TRUE;
    }
    else
    {
        
$message_data->attachments['is_file'] = FALSE;
    }
    
$message_data->attachments['file_name'] = $file_name;

CoolAsCarlito is offline   Reply With Quote
Old 10-11-2012, 12:59 PM   PM User | #2
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
PHP Code:
<?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 ?
patryk is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:19 AM.


Advertisement
Log in to turn off these ads.