<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ersin Acar &#187; multi-dimensional</title>
	<atom:link href="http://ersinacar.com/tag/multi-dimensional/feed" rel="self" type="application/rss+xml" />
	<link>http://ersinacar.com</link>
	<description>PHP and Web Technologies Freak</description>
	<lastBuildDate>Fri, 06 Jan 2012 14:19:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Searching strings in a multi-dimensional array</title>
		<link>http://ersinacar.com/searching-strings-in-a-multi-dimensional-array_103.html</link>
		<comments>http://ersinacar.com/searching-strings-in-a-multi-dimensional-array_103.html#comments</comments>
		<pubDate>Tue, 12 May 2009 21:45:45 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[multi-dimensional]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=103</guid>
		<description><![CDATA[I&#8217;m working on a security and trying to make searches in a multi-dimensional array Here is the function; &#60;?php function multi_array_search&#40;$needle, $haystack, $key, &#38;$result, $searchlevel = 0&#41; &#123; while&#40;is_array&#40;$haystack&#41; &#38;&#38; isset&#40;$haystack&#91;key&#40;$haystack&#41;&#93;&#41;&#41; &#123; if&#40;$searchlevel == 0 &#38;&#38; key&#40;$haystack&#41; == $key &#38;&#38; $haystack&#91;$key&#93; == $needle&#41; &#123; $result = $haystack; &#125; elseif&#40;$searchlevel &#62; 0&#41; &#123; multi_array_search&#40;$needle, $haystack&#91;key&#40;$haystack&#41;&#93;, $key, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a security and trying to make searches in a multi-dimensional array<br />
Here is the function;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> multi_array_search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$needle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> <span style="color: #000088;">$searchlevel</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$searchlevel</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$haystack</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$haystack</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$searchlevel</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      multi_array_search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$needle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$haystack</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$result</span><span style="color: #339933;">,</span> <span style="color: #000088;">$searchlevel</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #990000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Example;<br />
2-dimensional array, search by both key and value<br />
$arr1 = array(<br />
  1 => array(&#8216;id&#8217; => 1, &#8216;name&#8217; => &#8216;Ersin&#8217;, &#8216;gender&#8217; => &#8216;male&#8217;),<br />
  &#8230;<br />
  12 => array(&#8216;id&#8217; => 12, &#8216;name&#8217; => &#8216;Emel&#8217;, &#8216;gender&#8217; => &#8216;female&#8217;),<br />
);</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> array_search_in_level<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Emel'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arr1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$result</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>$result is:<br />
array(<br />
  &#8216;id&#8217; => int 12<br />
  &#8216;name&#8217; => string &#8216;Emel&#8217; (length=4)<br />
  &#8216;gender&#8217; => string &#8216;female&#8217; (length=4)<br />
) </p>
]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/searching-strings-in-a-multi-dimensional-array_103.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

