I have a PHP question that I don't understand. It's about arrays. I'll explain it in detail below↓
刘毅
刘毅 2019-02-15 21:50:26
0
5
1209

Excuse me, what is $values[$k] and $v[$key] in this string of code $values[$k]= isset($v[$key]) ? $v[$key] : ''; What's the meaning. Sorry for the trouble, gentlemen.

Source code attached:

<?php
		function test($array=array(),$key='',$paixu=true){
			$result=array();
			
			foreach($array as $k => $v){
				$values[$k]= isset($v[$key]) ? $v[$key] : '';
			}
			unset($v);
			$paixu ? asort($values) : arsort($values);
			
			foreach ($values as $k => $v){
				$result[$k] = $array[$k];
			}
			return $result;
		}
	
		$data = array(
				array('post_id'=>1,'title'=>'如何学好PHP','reply_num'=>582),
				array('post_id'=>2,'title'=>'PHP数组常用函数汇总','reply_num'=>182),
				array('post_id'=>3,'title'=>'PHP字符串常用函数汇总','reply_num'=>982),
			);

		$paixuhou=test($data,'reply_num',true);
		echo "<pre>";
		print_r($paixuhou);
?>


刘毅
刘毅

reply all(1)
phpcn_u146783

$values ​​is the newly opened array in the function, $k comes from the traversal of foreach and is the array key value, $v is the array value corresponding to the key value, $key comes from the $key in the function parameter,

You can take a look at the foreach function

  • reply I would like to ask what does it mean to add a square bracket after a variable. Like $v[$key] and $values[$k] like this.
    刘毅 author 2019-02-16 13:24:09
  • reply thank you.
    刘毅 author 2019-02-16 13:24:32
  • reply Each data will have a key value corresponding to the value $k->$v
    jjJ author 2019-02-17 10:00:26
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!