简体   繁体   English

将目录路径转换为javascript对象

[英]converting directory path to javascript object

I have a list of directory paths/files in PHP, like so: 我有一个PHP目录路径/文件列表,如下所示:

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

and I want to convert these to javascript objects. 我想将这些转换为javascript对象。 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, ie I can't say: files.001.foo = "bar"; 我唯一的问题是,如果文件名或目录以数字开头,即我不能说: 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 = {}; 我本质上是试图解析形式为"x1x2...xn/y1y2...yn/..."的php字符串,其中x_i,y_i,z_i等是“/”以外的字符,嵌套成嵌套javascript对象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 "_"). 我想我在这里发现了一些解决方案因为这个用户也试图根据分隔的字符串动态添加javascript对象(在他的情况下为“_”)。

Where do I start: Don't use new Array() to create a new array, just use the literal [] . 我从哪里开始:不要使用new Array()来创建一个新数组,只需使用文字[] Google the reasons. 谷歌的原因。

An array isn't the same as an object - it's an augmentation of Object . 数组与对象不同 - 它是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... 此外,您的PHP代码已关闭:引用的$files['name']不明确(PHP不知道要打印的内容:$ file的值,后跟['name']作为简单字符串,或$file['name'] 。但不止于此:它不在php标签中......

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); 如果您希望属性具有前导零,则可以使用str_padsubstr('000'.$key,-3);

Here's what I'd do: 这是我要做的:

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

That's it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM