Home  >  Article  >  Backend Development  >  数组取值解决思路

数组取值解决思路

WBOY
WBOYOriginal
2016-06-13 12:05:541052browse

数组取值

Array<br />(<br />    [iso.3.6.1.4.1.2021.4.1.0] => INTEGER: 0<br />    [iso.3.6.1.4.1.2021.4.2.0] => STRING: "swap"<br />    [iso.3.6.1.4.1.2021.4.3.0] => INTEGER: 16777208<br />    [iso.3.6.1.4.1.2021.4.4.0] => INTEGER: 16777208<br />    [iso.3.6.1.4.1.2021.4.5.0] => INTEGER: 16324040<br />    [iso.3.6.1.4.1.2021.4.6.0] => INTEGER: 4484096<br /> <br />    [iso.3.6.1.4.1.2021.9.1.6.1] => INTEGER: 1032088<br />    [iso.3.6.1.4.1.2021.9.1.6.2] => INTEGER: 458942624<br />    [iso.3.6.1.4.1.2021.9.1.6.3] => INTEGER: 8162020<br />    [iso.3.6.1.4.1.2021.9.1.6.4] => INTEGER: 99150<br />    [iso.3.6.1.4.1.2021.9.1.6.5] => INTEGER: 3096336<br />    [iso.3.6.1.4.1.2021.9.1.6.6] => INTEGER: 1032088<br />  <br />)

怎么取键值是iso.3.6.1.4.1.2021.9.1.6.* 的值  就是把前面 键值是iso.3.6.1.4.1.2021.9.1.6的所有值 加起来
------解决方案--------------------
$arr=array(<br />    'iso.3.6.1.4.1.2021.4.1.0' => '0',<br />    'iso.3.6.1.4.1.2021.4.2.0' => 'swap',<br />    'iso.3.6.1.4.1.2021.4.3.0' => '16777208',<br />    'iso.3.6.1.4.1.2021.4.4.0' => '16777208',<br />    'iso.3.6.1.4.1.2021.4.5.0' => '16324040',<br />    'iso.3.6.1.4.1.2021.4.6.0' => '4484096',<br />    'iso.3.6.1.4.1.2021.9.1.6.1' => '1032088',<br />    'iso.3.6.1.4.1.2021.9.1.6.2' => '458942624',<br />    'iso.3.6.1.4.1.2021.9.1.6.3' => '8162020',<br />    'iso.3.6.1.4.1.2021.9.1.6.4' => '99150',<br />    'iso.3.6.1.4.1.2021.9.1.6.5' => '3096336',<br />    'iso.3.6.1.4.1.2021.9.1.6.6' => '1032088'<br />);<br />$num=0;<br />foreach($arr as $k=>$v){<br />	if(version_compare($k, 'iso.3.6.1.4.1.2021.9.1.6.0', '>')){<br />		$num+=$v;<br />	}<br />}<br />echo $num;<br />/*<br />472364306<br />*/

------解决方案--------------------
$ar = 你的数组<br />$res = array();<br />foreach($ar as $k=>$v){<br />    if(strpos($k, 'iso.3.6.1.4.1.2021.9.1.6') !== false){<br />	     $t  = explode(': ', $v);<br />	    $res[] = $t[1];<br />	}<br />}<br />echo array_sum($res);

------解决方案--------------------
$ar = array (<br />  'iso.3.6.1.4.1.2021.4.1.0' => 'INTEGER: 0',<br />  'iso.3.6.1.4.1.2021.4.2.0' => 'STRING: "swap"',<br />  'iso.3.6.1.4.1.2021.4.3.0' => 'INTEGER: 16777208',<br />  'iso.3.6.1.4.1.2021.4.4.0' => 'INTEGER: 16777208',<br />  'iso.3.6.1.4.1.2021.4.5.0' => 'INTEGER: 16324040',<br />  'iso.3.6.1.4.1.2021.4.6.0' => 'INTEGER: 4483856',<br />  'iso.3.6.1.4.1.2021.4.11.0' => 'INTEGER: 21261064',<br />  'iso.3.6.1.4.1.2021.4.12.0' => 'INTEGER: 16000',<br />  'iso.3.6.1.4.1.2021.4.14.0' => 'INTEGER: 72720',<br />  'iso.3.6.1.4.1.2021.4.15.0' => 'INTEGER: 11089928',<br />  'iso.3.6.1.4.1.2021.4.100.0' => 'INTEGER: 0',<br /><br />  'iso.3.6.1.4.1.2021.9.1.6.1' => 'INTEGER: 1032088',<br />  'iso.3.6.1.4.1.2021.9.1.6.2' => 'INTEGER: 458942624',<br />  'iso.3.6.1.4.1.2021.9.1.6.3' => 'INTEGER: 8162020',<br />  'iso.3.6.1.4.1.2021.9.1.6.4' => 'INTEGER: 99150',<br />  'iso.3.6.1.4.1.2021.9.1.6.5' => 'INTEGER: 3096336',<br />  'iso.3.6.1.4.1.2021.9.1.6.6' => 'INTEGER: 1032088',<br />);<br /><br />$num = 0;<br />foreach($ar as $k=>$v){<br />  if($k >= 'iso.3.6.1.4.1.2021.9.1.6.1'){<br />    strtok($v, ':');<br />    $num += strtok('');<br />  }<br />}<br />echo $num;<br />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn