snipt

Ctrl+h for KB shortcuts

PHP

php - finding keys in an array that match a pattern

1
2
3
<?php

$vals = array_intersect_key( $this->input->post(), array_flip( preg_grep( '/^title/i', array_keys( $this->input->post() ) ) ) );
https://snipt.net/embed/45421794a489aedf059e12a82ccd6aec/
/raw/45421794a489aedf059e12a82ccd6aec/
45421794a489aedf059e12a82ccd6aec
php
PHP
3
2019-06-13T03:53:39
True
False
False
Mar 08, 2016 at 03:00 AM
/api/public/snipt/144620/
php-finding-keys-in-an-array-that-match-a-pattern
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="nv">$vals</span> <span class="o">=</span> <span class="nb">array_intersect_key</span><span class="p">(</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">input</span><span class="o">-&gt;</span><span class="na">post</span><span class="p">(),</span> <span class="nb">array_flip</span><span class="p">(</span> <span class="nb">preg_grep</span><span class="p">(</span> <span class="s1">&#39;/^title/i&#39;</span><span class="p">,</span> <span class="nb">array_keys</span><span class="p">(</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">input</span><span class="o">-&gt;</span><span class="na">post</span><span class="p">()</span> <span class="p">)</span> <span class="p">)</span> <span class="p">)</span> <span class="p">);</span> </span></pre></div> </td></tr></table>
key, match, php