search

Home  >  Q&A  >  body text

I have a PHP question that I don’t understand. It’s about arrays. I’ll explain it in detail below↓

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);
?>


刘毅刘毅2111 days ago1237

reply all(5)I'll reply

  • phpcn_u146783

    phpcn_u1467832019-02-15 23:25:05

    $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
    1
  • 刘毅

    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.

    刘毅 · 2019-02-16 13:24:09
    刘毅

    thank you.

    刘毅 · 2019-02-16 13:24:32
    jjJ

    Each data will have a key value corresponding to the value $k->$v

    jjJ · 2019-02-17 10:00:26
  • Cancelreply