title: PHP Array Undefined Offset, Even Though The Code Executes
body: <p>I'm trying to extract some variables from my <code>REQUEST_URI</code>:</p>
  
  <pre><code>liquidfinger.com/key1-value1/key2-value2/key3-value3
  </code></pre>
  
  <p>I want to transform these values into a php associative array. My code is as follows:</p>
  
  <pre><code>$pState = [];
  $stateString = substr($_SERVER['REQUEST_URI'],1);//remove leading slash
  if($stateString){
      $statePairs = explode("/",$stateString);
      foreach($statePairs as $statePair){
          $statePairArray = explode("-",$statePair);
          $pState[$statePairArray[0]] = $statePairArray[1];
      }
  }
  </code></pre>
  
  <p>The <code>$pState</code> array is being created correctly and I can <code>echo</code> all the keys and values. However, I am getting an <code>error_log</code>:</p>
  
  <blockquote>
    <p>Undefined offset: 1</p>
  </blockquote>
  
  <p>I am even getting an <code>error_log</code> when there are no key-value pairs, so the <code>IF</code> statement shouldn't be executed, but possibly that is a characteristic of the <code>error_log</code>?</p>
  
  <p>Okay, just to recap, the code was working but I was getting error messages. Further tests yielded the following:</p>
  
  <pre><code>url: www.liquidfinger.com
  print_r($pState): Array ( )
  [11-Mar-2016 10:01:02 UTC] PHP Notice:  Undefined offset: 1 in /home/adamglynsmith/public_html/index.php on line 20
  
  
  url: http://www.liquidfinger.com/user-2/tab-browseAll/marker-101
  print_r($pState): Array ( [user] =&gt; 2 [tab] =&gt; browseAll [marker] =&gt; 101 )
  print_r($statePairArray): Array ( [0] =&gt; user [1] =&gt; 2 ) Array ( [0] =&gt; tab     [1] =&gt; browseAll ) Array ( [0] =&gt; marker [1] =&gt; 101 )
  [11-Mar-2016 10:14:41 UTC] PHP Notice:  Undefined offset: 1 in /home/adamglynsmith/public_html/index.php on line 20
  [11-Mar-2016 10:14:43 UTC] PHP Notice:  Undefined offset: 1 in /home/adamglynsmith/public_html/index.php on line 20
  </code></pre>
  
  <p>Since I have spent quite enough time on it and since I ultimately wanted to end up with a JavaScript array, I solved - or avoided - the problem by using the php to construct a string for the JavaScript like so:</p>
  
  <pre><code>    $stateString = str_replace("/","', ",$stateString);//get rid of slashes
      $stateString = str_replace("-",":'",$stateString);//get rid of dashes
      $stateString .= "'";//add final single quote
      &lt;script&gt;
          jState = {&lt;?php echo $stateString; ?&gt;};
      &lt;/script&gt;
  </code></pre>
  
  <p>Thanks.</p>