2013-04-25 55 views
13

比方說,我有一些源這個輸出,我沒有訪問原始PHP創建數組:重新創建print_r的輸出原始的PHP陣列

Array 
(
    [products] => Array 
     (
      [name] => Arduino Nano Version 3.0 mit ATMEGA328P 
      [id] => 10005 
     ) 

    [listings] => Array 
     (
      [category] => 
      [title] => This is the first line 
This is the second line 
      [subtitle] => This is the first subtitle 
This is the second subtitle 
      [price] => 24.95 
      [quantity] => 
      [stock] => 
      [shipping_method] => Slow and cheap 
      [condition] => New 
      [defects] => 
     ) 

    [table_count] => 2 
    [tables] => Array 
     (
      [0] => products 
      [1] => listings 
     ) 

) 

現在我想輸入的數據和有一個算法重新創建它正在打印的原始數組,然後我可以將它用於我自己的應用程序。

目前我正在考慮sub_str()和正則表達式來提取數據並將其正確放置。在我進一步討論之前,是否有一種更簡單的方法,通過已經編寫的代碼或php插件爲我做到了這一點?

+0

顯示你試過的東西... – michi 2013-04-25 22:55:34

+0

目前我正在考慮一個sub_str()和正則表達式來提取數據並將其正確放置。在我進一步探討之前,有沒有更簡單的方法,通過已經編寫的代碼或者php插件爲我做到了這一點? – Dan 2013-04-25 22:56:08

+3

這是[var_export](http://www.php.net/var_export)的用途,而不是print_r。 – 2013-04-25 23:00:34

回答

19
function print_r_reverse($in) { 
    $lines = explode("\n", trim($in)); 
    if (trim($lines[0]) != 'Array') { 
     // bottomed out to something that isn't an array 
     return $in; 
    } else { 
     // this is an array, lets parse it 
     if (preg_match("/(\s{5,})\(/", $lines[1], $match)) { 
      // this is a tested array/recursive call to this function 
      // take a set of spaces off the beginning 
      $spaces = $match[1]; 
      $spaces_length = strlen($spaces); 
      $lines_total = count($lines); 
      for ($i = 0; $i < $lines_total; $i++) { 
       if (substr($lines[$i], 0, $spaces_length) == $spaces) { 
        $lines[$i] = substr($lines[$i], $spaces_length); 
       } 
      } 
     } 
     array_shift($lines); // Array 
     array_shift($lines); // (
     array_pop($lines); //) 
     $in = implode("\n", $lines); 
     // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one) 
     preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); 
     $pos = array(); 
     $previous_key = ''; 
     $in_length = strlen($in); 
     // store the following in $pos: 
     // array with key = key of the parsed array's item 
     // value = array(start position in $in, $end position in $in) 
     foreach ($matches as $match) { 
      $key = $match[1][0]; 
      $start = $match[0][1] + strlen($match[0][0]); 
      $pos[$key] = array($start, $in_length); 
      if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1; 
      $previous_key = $key; 
     } 
     $ret = array(); 
     foreach ($pos as $key => $where) { 
      // recursively see if the parsed out value is an array too 
      $ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0])); 
     } 
     return $ret; 
    } 
} 

不是我的代碼,發現這裏的評論:print_r 「馬特」是主人

0

我也摸索出瞭解決您的問題,因爲它是在這裏重現問題的一個非常有用的工具堆棧溢出。我的來源位於:https://github.com/etalon/aprp

我做了一點不同:在你的字符串中,你不需要換行符,所以我走過了字符。一個缺點:如果你的數組中有括號或括號,它將不起作用。