来源:
http://stackoverflow.com/questions/7025909/how-create-an-array-from-the-output-of-an-array-printed-with-print-r

<?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;
        }
        
    }
?>  

更多相关文章

  1. 在php中获取引荐来源网址(包括参数)
  2. 跟踪来自特定来源的用户
  3. 无法弄清楚mySQL语法错误的来源
  4. js 区分浏览器来源是PC端还是移动端

随机推荐

  1. thinkPHP 获得当前请求的全部常量信息
  2. 如何使用ajax向php发送与每个按钮相关的
  3. PHP展示KMP拓展算法思想
  4. php标准库 SPL 数据结构之-----SplDoubly
  5. PHPCMS V9 的手机门户wap绑定单页面
  6. phpstorm取消自动保存并标识修改的文件为
  7. 尽管“SQL语法错误”消息仍然成功执行
  8. 使用mysql验证在php中登录表单
  9. php学习笔记四:使用 xampp-部署php网站
  10. JRoute::_() -函数在哪里?在joomla