1

I'm using an older version of Cake ( 1.3 ).

I have an array which returns the structure of a directory, I have then flattened the array using Set::Flatten() and it outputs the following example;

Array
(
    [0] => Screen Shot 2017-10-09 at 7.08.52 pm-1.png
    [projects.raiders.0] => Webserver_requests_graph.jpg
    [images.0] => Screen Shot 2017-10-09 at 7.08.52 pm.png
    [triggers.0] => Screen_Shot_2017-10-09_at_4_52_32_pm.png
    [providers.0] => testScreen Shot 2017-10-09 at 7.08.52 pm.png
)

The 'key' represents the file structure, so 0 is /, projects is a directory, the raider is a subdirectory of projects and so on....

I need to create a JSON string which walks through each layer of the above array and put it into the following format;

[ ['fullpathname', 'displaytext', 'isfile'] ]

I also need to add dots to represent the original directory structure, so the above array will turn into the following JSON string;

[
    ['/', '/', 'false'],
    ['/Screen Shot 2017-10-09 at 7.08.52 pm-1.png', 'Screen Shot 2017-10-09 at 7.08.52 pm-1.png', 'true'],
    ['/projects', '.. projects', 'false'],
    ['/projects/raiders', '.. .. raiders', 'false'],
    ['/projects/raiders/Webserver_requests_graph.jpg', '.. .. .. Webserver_requests_graph.jpg', 'true'],
    ['/images', '.. images', 'false'],
    ['/images/Screen Shot 2017-10-09 at 7.08.52 pm.png', '.. .. Screen Shot 2017-10-09 at 7.08.52 pm.png', 'true'],
    ['/triggers', '.. triggers', 'false'],
    ['/triggers/Screen_Shot_2017-10-09_at_4_52_32_pm.png', '.. .. Screen_Shot_2017-10-09_at_4_52_32_pm.png', 'true'],
    ['/providers', '.. providers', 'false'],
    ['/providers/testScreen Shot 2017-10-09 at 7.08.52 pm.png', '.. .. testScreen Shot 2017-10-09 at 7.08.52 pm.png', true]
]

I was wondering if I could get some guidance, I'm really really stuck on how to go about this. I have heard about array_walk_recursive, but I dont understand it. any help would be greatly appreciated.

Thank you

4
  • WHAT'S THIS .. .. .. Webserver_requests_graph.jpg Isn't it invalid. I think it need to be ../../../Webserver_requests_graph.jpg and so on for others Commented Nov 1, 2017 at 4:32
  • Have a look at the format i've described ... that is the 'Display Text' what is output to the user Commented Nov 1, 2017 at 4:33
  • Why is your input 5 items and the output 11, or am I missing something. Commented Nov 1, 2017 at 4:53
  • The 'key' represents the file structure ... So it needs to display each directory instance, hence the 11 outputs Commented Nov 1, 2017 at 4:59

1 Answer 1

1

You need to do it like below:-

<?php

$final_array = [['/', '/', 'false']]; // i have taken first value from output by-default because i am unable to create any logic for first value through the given input

foreach($array as $key=>$val){
    if($key=='0'){
        $final_array[] = ['/'.$val,$val,'true'];
    }else{
        $exploded_key = explode('.',$key);
        foreach ($exploded_key as $k=>$v){
            if($v =='0'){
                $dots = '.. ';
                for ($i=0;$i<count(array_slice($exploded_key, 0, $k));$i++){
                    $dots .= '.. ';
                }
                $final_array[] = [ '/'. join('/', array_slice($exploded_key, 0, $k)).'/'.$val,$dots.$val,'true'];
            }else{
                $dots = '.. ';
                for ($i=0;$i<count(array_slice($exploded_key, 0, $k));$i++){
                    $dots .= '.. ';
                }
                $final_array[] = [ '/'. join('/', array_slice($exploded_key, 0, $k+1)),$dots.$v,'false'];
            }
        }
    }
}

echo "<pre/>";print_r($final_array);

Output:- https://eval.in/890536

Note:- if you want json as output then use json_encode() like below:-

echo json_encode($final_array);

Output:- https://eval.in/890584

Sign up to request clarification or add additional context in comments.

4 Comments

Thats just turning it back into an array, it needs to be turned into a JSON string similar to the output that is at the bottom of my question
use json_encode() to get json output. Check this:- eval.in/890584
Oh yes, thats exactly it ... I thank you so much, your help with this is greatly greatly appreciated!!
@BigJobbies glad to help you :):)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.