<?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>潘锦的空间 &#187; Bucket</title>
	<atom:link href="https://www.phppan.com/tag/bucket/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.phppan.com</link>
	<description>SaaS SaaS架构 团队管理 技术管理 技术架构 PHP 内核 扩展 项目管理</description>
	<lastBuildDate>Sun, 28 Jun 2026 02:05:27 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>PHP源码阅读笔记十：array_keys,array_values函数</title>
		<link>https://www.phppan.com/2010/02/php-source-10-array_keys-array_values/</link>
		<comments>https://www.phppan.com/2010/02/php-source-10-array_keys-array_values/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 02:04:25 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Bucket]]></category>
		<category><![CDATA[HashTable]]></category>
		<category><![CDATA[PHP源码]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=537</guid>
		<description><![CDATA[PHP源码阅读笔记十：array_keys,array_values函数 新年的第一篇文章，首先给各位朋友拜年 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>PHP源码阅读笔记十：array_keys,array_values函数<br />
新年的第一篇文章，首先给各位朋友拜年<br />
今天有空看了下代码，写点东西，留作纪念！<br />
<br />
<strong>array array_keys ( array input [, mixed search_value [, bool strict]] )</strong></p>
<p>array_keys() 返回 input 数组中的数字或者字符串的键名。</p>
<p>如果指定了可选参数 search_value，则只返回该值的键名。否则 input 数组中的所有键名都会被返回。自 PHP 5 起，可以用 strict 参数来进行全等比较（===）。</p>
<p>array_keys 函数的实现在standard/array.c文件的2416行 PHP_FUNCTION(array_keys)</p>
<p>程序依照PHP一贯的风格，先判断输入是否正确，如果有第三个参数，则判断大小的函数使用is_identical_function（默认情况下是使用 is_equal_function函数）<br />
然后初始化返回的数组，遍历所给的数组，取每个元素的key值，赋值给返回的数组，这个key值又分为数字和字符串两种，其中最主要的函数是hash操作函数zend_hash_get_current_key_ex（取当前元素的key值）</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">ZEND_API <span style="color: #993333;">int</span> zend_hash_get_current_key_ex<span style="color: #009900;">&#40;</span>HashTable <span style="color: #339933;">*</span>ht<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>str_index<span style="color: #339933;">,</span> uint <span style="color: #339933;">*</span>str_length<span style="color: #339933;">,</span> ulong <span style="color: #339933;">*</span>num_index<span style="color: #339933;">,</span> zend_bool duplicate<span style="color: #339933;">,</span> HashPosition <span style="color: #339933;">*</span>pos<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    Bucket <span style="color: #339933;">*</span>p<span style="color: #339933;">;</span>
&nbsp;
    p <span style="color: #339933;">=</span> pos <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>pos<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> ht<span style="color: #339933;">-&gt;</span>pInternalPointer<span style="color: #339933;">;</span>
&nbsp;
    IS_CONSISTENT<span style="color: #009900;">&#40;</span>ht<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>p<span style="color: #339933;">-&gt;</span>nKeyLength<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>    <span style="color: #666666; font-style: italic;">//    数字型的nKeyLength长度为0</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>duplicate<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #339933;">*</span>str_index <span style="color: #339933;">=</span> estrndup<span style="color: #009900;">&#40;</span>p<span style="color: #339933;">-&gt;</span>arKey<span style="color: #339933;">,</span> p<span style="color: #339933;">-&gt;</span>nKeyLength <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #339933;">*</span>str_index <span style="color: #339933;">=</span> p<span style="color: #339933;">-&gt;</span>arKey<span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//    /* arKey存储key名称（字符串类型的key）必须是最后一个成员，*/</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>str_length<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #339933;">*</span>str_length <span style="color: #339933;">=</span> p<span style="color: #339933;">-&gt;</span>nKeyLength<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">return</span> HASH_KEY_IS_STRING<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #339933;">*</span>num_index <span style="color: #339933;">=</span> p<span style="color: #339933;">-&gt;</span>h<span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//    存储数字key值</span>
            <span style="color: #b1b100;">return</span> HASH_KEY_IS_LONG<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> HASH_KEY_NON_EXISTANT<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>对于此函数的理解主要是对bucket定义的了解</strong><br />
与bucket相关的内容请移步<a href="http://www.phppan.com/2009/12/php-hashtable-demo" target="_blank"> http://www.phppan.com/2009/12/php-hashtable-demo/</a></p>
<p>
<strong>array array_values ( array input )</strong></p>
<p>array_values() 返回 input 数组中所有的值并给其建立数字索引。<br />
array_values 函数与array_keys的函数实现基本类似，并且还少了一个zend_hash_get_current_key_ex操作和判断值类型的操作，</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/02/php-source-10-array_keys-array_values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
