2013-04-25 5 views
13

चलो कहते हैं कि मैं कुछ स्रोत से इस उत्पादन जहाँ मैं मूल पीएचपी बनाया सरणी के लिए पहुँच नहीं है करते हैं:को पुन: बनाएं print_r उत्पादन से मूल पीएचपी सरणी

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

+0

वर्तमान में मैं एक sub_str() और regex स्टेटमेंट्स के बारे में सोच रहा हूं जो डेटा खींचते हैं और इसे उचित स्थान पर रखते हैं। आगे जाने से पहले, क्या पहले से ही लिखित कोड या PHP प्लगइन के माध्यम से यह एक आसान तरीका है, जो मेरे लिए पहले से ही ऐसा करता है? – Dan

+3

यह है [var_export] (http://www.php.net/var_export) print_r के बजाय है। –

उत्तर

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

मैंने इसे थोड़ा अलग किया: आपकी स्ट्रिंग में आपको लाइन-ब्रेक की आवश्यकता नहीं है, इसलिए मैं वर्णों से घूमता हूं। एक नुकसान: यदि आपके सरणी-मूल्यों में ब्रैकेट या कंस्ट्रैसिस हैं तो यह काम नहीं करेगा।

संबंधित मुद्दे