1

I have a list of directory paths/files in PHP, like so:

path1/path2/foo1_jpg
path1/foo2_png
path2/path3/bar_pdf
path2/
path1/

and I want to convert these to javascript objects. I thought about just converting the "/" to periods and then saying something like:

<script>
    var files = new Array();
    <?php $dirlist = getFileList("images",true); ?> //returns array of directory paths (and other info)
    <?php foreach($dirlist as $file): ?>
        files.<?=$file['name']?> = "$file['name']"; 

        //where $file['name'] returns, for example, path1.path2.foo1_jpg

    <?php endforeach; ?>
console.log(files);
</script>

The ONLY issue I have is if a file name or directory begins with a number, i.e. I can't say: files.001.foo = "bar";

I believe that I need to overload the dot operator to handle names beginning with numbers. Is that the best way, or is there another way?

I'm essentially trying to parse a php string of the form "x1x2...xn/y1y2...yn/...", where x_i,y_i,z_i,etc are characters other than "/", into a nested javascript object x1x2x3...xn.y1y2...yn.z1z2z3...zn = {};. I think I found something of a solution here Since this user was also trying to dynamically add javascript objects based on a delimited string (in his case a "_").

Community
  • 1
  • 1
thed0ctor
  • 1,350
  • 4
  • 17
  • 34
  • I don't understand: why are you converting the "/"s? – Waleed Khan Jan 15 '13 at 17:22
  • yep, why do convert "/"s to "."s? – gopi1410 Jan 15 '13 at 17:24
  • because `file.dir1/dir2/dir3 = "bar"` isn't valid syntax in javascript. Thus converting "/"s to "."s allow me to say `file.dir1.dir2.dir3 = "bar"`. It also allows me to easily assign nested objects. Does that make sense or should I rephrase? – thed0ctor Jan 15 '13 at 17:31
  • As ugly as it may be, you can assign and access them as strings. That is, file['dir1/dir2/dir3'] is valid. – danronmoon Jan 15 '13 at 17:36

1 Answers1

3

Where do I start: Don't use new Array() to create a new array, just use the literal []. Google the reasons.

An array isn't the same as an object - it's an augmentation of Object. so it doesn't support the dot-notation as such. Also, your PHP code is off: the quoted $files['name'] is ambiguous (PHP doesn't know what to print out: the value of $file, followed by ['name'] as a simple string, or $file['name']. But more than that: it's not in php tags...

Here's one way to do what you want - but you shouldn't use it:

var files = {};//create object. properties can be numeric, too
<?php
    foreach ($dirlist as $key => $file)
    {//use $key for numeric index/properties
        echo 'files['.$key.'] = "'.$file['name'].'";';
        //or with double quotes:
        echo "files[$key] = '{$file['name']}';";//need the curly's for ambiguity, or rather avoid ambiguity
    }
?>

If you want the properties to have leading zeroes, then you can by either using str_pad or substr('000'.$key,-3);

Here's what I'd do:

var files = JSON.parse('<?= json_encode($dirlist); ?>');

That's it.

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • Yeah, echoing JSON is the way to go. +1 – Seain Malkin Jan 15 '13 at 17:36
  • @SeainMalkin: Naturally, that would have been my only answer to this question, but I can't just ignore a fairly significant syntax error, and, in my mind, quite severe abuse of JS arrays – Elias Van Ootegem Jan 15 '13 at 17:41
  • JSON won't convert a php string, for example `"dir1.dir2.dir3"` to a javascript object `dir1.dir2.dir3 = {};`. Using the syntax you used: `var files = JSON....`, this won't add a nested object to files. Let's say `$dirlist = "pizza.pie"`. I'm essentially trying to say `files.(JSON.parse($dirlist)) = {};`, i.e. `files.pizza.pie = {};` – thed0ctor Jan 15 '13 at 18:18
  • @thed0ctor: the loop through the `$dirlist` and explode the paths, to use them as array keys, and build an assoc-array the way you need it, and json_encoded that... – Elias Van Ootegem Jan 15 '13 at 18:19