Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP: array_unshift after array_unique returns integer
    primarykey
    data
    text
    <p>I recently tried to create an array with multiple Strings, representing the AcceptLanguage header. I need to push another user-specified language to the start of the array, to make it max priority.</p> <p>So far I have</p> <pre><code>function getRequestLangs(){ //get languages from browser $accLangs = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']); $requestedLanguages = array(); foreach($accLangs as $key =&gt; $lang){ $lang = substr($lang,0,2); // p is a quality param, we won't need it, since the // preferred languages are already sorted by default if($lang != 'p='){ array_push($requestedLanguages,$lang); } } // we only need each language once, this function keeps the order return array_unique($requestedLanguages); } </code></pre> <p>Now I want to add the user-specified language as first key (in case the language is not supportet, I may fall back to another accepted language)</p> <pre><code>//language from path, pushed as first index (highest priority) if(isset($_GET['lang']) &amp;&amp; $_GET['lang'] != ""){ $requestedLanguages = array_unshift($requestedLanguages,$_GET['lang']); } </code></pre> <p><code>var_dump($requestedLanguages)</code> before array_unshift: </p> <pre><code> array(2) { [0]=&gt; string(2) "de" [2]=&gt; string(2) "en" } </code></pre> <p><code>var_dump($requestedLanguages)</code> after array_unshift: </p> <pre><code> int(3) </code></pre> <p>I think it might have to do with the index-hole between 0 and 2, but that is only a guess. </p> <p>[EDIT] i need more caffeine...</p> <pre><code>//wrong: $requestedLanguages = array_unshift($requestedLanguages,$_GET['lang']); //right: array_unshift($requestedLanguages,$_GET['lang']); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload