Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts

    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;


  • #2
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    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 ?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •