title: PHP - Can A String Of Text That Looks Like An Array (but is not that data type) Be Converted To Array w/o str_split()/explode(), etc?
body: <p>Weird scenario I have to admit but it is actually relevant for me, so here goes. </p>
  
  <p><strong>This is a curiosity question</strong> since I have a better way to do this already.</p>
  
  <p>I am creating a remote file for other developers to read (file_get_contents) and update. I know I can do this with JSON or creative use of <code>explode()</code> or <code>str_split()</code>, but was wondering if the following was possible.</p>
  
  <p>If you have a text string formatted such that it "looks like" an array (but in PHP's eyes is not actually cast as an array):</p>
  
  <pre><code>//the following string represents the entire contents of the text file myfile.txt
  
  array('key_1' =&gt; 'string_1', 'key_2' =&gt; 'string_2' ..... 'key_n' =&gt; 'string_n');
  </code></pre>
  
  <p>The string values (eg 'string_1') can literally contain any character...no exceptions. Is there any way that the text file can be read with e.g. <code>file_get_contents()</code>:</p>
  
  <pre><code>//this is the remotescript.php file
  
  $arrayString = file_get_contents('path/to/myfile.txt');
  </code></pre>
  
  <p>and then you, the script writer, literally cast this as an array? (where the type and key => value pairs are set) That is:</p>
  
  <pre><code>$x = someFunction($arrayString);
  
  // this returns ----&gt; $x = array('key_1' =&gt; 'string_1', 'key_2' =&gt; 'string_2' ..... 'key_n' =&gt; 'string_n'); 
  
  print_r($x); // outputs  ----&gt;  Array([key_1] =&gt; string_1 [key_2] =&gt; string_2 ..... [key_n] =&gt; value_n)
  </code></pre>
  
  <p>In a bad attempt I tried casting the string using (array):</p>
  
  <pre><code>$string = file_get_contents('path/to/myfile.txt');
  
  $x = (array)$string;
  </code></pre>
  
  <p>but this only creates one key where the value is the literal string from the text file.</p>