栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何从用print_r打印的数组的输出创建数组?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从用print_r打印的数组的输出创建数组?

我实际上写了一个将“字符串数组”解析为实际数组的函数。显然,它有点笨拙,但可以在我的测试用例上使用。这是http://prepad.org/idlXdij3上功能原型的链接。

对于那些不想单击链接的人,我也会在内联发布代码:

<?php     ?><?php    //The array we begin with    $start_array = array('foo' => 'bar', 'bar' => 'foo', 'foobar' => 'barfoo');    //Convert the array to a string    $array_string = print_r($start_array, true);    //Get the new array    $end_array = text_to_array($array_string);    //Output the array!    print_r($end_array);    function text_to_array($str) {        //Initialize arrays        $keys = array();        $values = array();        $output = array();        //Is it an array?        if( substr($str, 0, 5) == 'Array' ) { //Let's parse it (hopefully it won't clash) $array_contents = substr($str, 7, -2); $array_contents = str_replace(array('[', ']', '=>'), array('#!#', '#?#', ''), $array_contents); $array_fields = explode("#!#", $array_contents); //For each array-field, we need to explode on the delimiters I've set and make it look funny. for($i = 0; $i < count($array_fields); $i++ ) {     //First run is glitched, so let's pass on that one.     if( $i != 0 ) {         $bits = explode('#?#', $array_fields[$i]);         if( $bits[0] != '' ) $output[$bits[0]] = $bits[1];     } } //Return the output. return $output;        } else { //Duh, not an array. echo 'The given parameter is not an array.'; return null;        }    }?>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/433174.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号