[英]How to get key value from an array?
Here is the array: 这是数组:
$campaigns =
Array (
[252] =>Array (
[campaign_type_id] => 9
[company] => Array (
[company_name] => facebook
[company_type] => 2
)
[campaign] => Array(
[pitch_id] => 27
[pitch_campaign_title] => facebook mandate
[pitch_campaign_description] => desc face
[pitch_campaign_image] => db.png
)
[title] => Accelarator
[selection] => Array (
[0] => Array(
[ca_mandate_id] => 96
[ca_id] => 252
[ca_company_id] => 1
[ca_updated] => 2015-12-31 12:37:50
)
)
[campaign_created_by] => 3
[userinfo] => Array (
[0] => Array (
[user_id] => 3
[user_first_name] => CoLabs
[user_last_name] => Accelerator
[user_img] => index2.jpg
[user_designation_name] => Investor
[user_company_id] => 123
)
)
)
)
how can I get the value '252'? 如何获得值“ 252”? Its a dynamic value.
它是一个动态的价值。 I want to get whatever value is stored in place of 252.
我想获取存储在252处的任何值。
Please help? 请帮忙?
Thanks in advance. 提前致谢。
Use the array_keys function: http://php.net/array_keys 使用array_keys函数: http : //php.net/array_keys
array_keys — Return all the keys or a subset of the keys of an array
array_keys —返回数组的所有键或键的子集
Description array array_keys ( array $array [, mixed $search_value = null [, bool $strict = false ]] )
描述 array array_keys(array $ array [,混合$ search_value = null [,bool $ strict = false]])
For your code: 对于您的代码:
$keys = array_keys($campaigns);
Try array_keys
尝试
array_keys
array_keys() returns the keys, numeric and string, from the array.
$req_key = array_keys($campaigns);
You can use array_keys()
(as other suggested) and key()
functions for getting this result. 您可以使用
array_keys()
(如其他建议)和key()
函数来获得此结果。
Difference is that: 区别在于:
If you want to use array_keys()
function it will returns you an array
that consist of all keys
. 如果要使用
array_keys()
函数,它将返回一个包含所有keys
的array
。
If you want to use key()
function it will returns you the first index of array
. 如果要使用
key()
函数,它将返回array
的第一个索引。 (you can get all keys
by using loop); (您可以使用循环获取所有
keys
);
Example 1 (With array_keys): 示例1(使用array_keys):
$arry = array(1,2,3);
echo "<pre>";
print_r(array_keys($arry));
Result: 结果:
Array
(
[0] => 0
[1] => 1
[2] => 2
)
Example 2 (With key): 示例2(带密钥):
$arry = array(1,2,3);
echo key($arry);
Result: 结果:
0 // index
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.