ホームページ  >  記事  >  バックエンド開発  >  助けを求める

助けを求める

WBOY
WBOYオリジナル
2016-06-23 14:05:14786ブラウズ

以下代码$result = array(	'x' => 'xxx',	'y' => 'yyy',);$aList = array(55,99,77,44);	//对应id字段,实际上个数不可预料$bList = array('1-20', '3-55', '88-99', '40-45');	//对应percent字段,个数不可预料,但和aList元素个数相同//................//................//要将第二和第三个数组加工到第一个数组里,形成这样的结果,一下子想不清楚,有点急着完成~谢谢了/*$result = array(	'x' => 'xxx',	'y' => 'yyy',	'other' = array(		'id' => 55,		'percent' => '1-20',		'other' => array(			'id' => 99,			'percent' => '3-55',			'other' => array(				'id' => 77,				'percent' => '88-99',				'other' => array(					'id' => 44,					'percent' => '40-45',				),			),				),	),);*/


ディスカッションに返信(解決策)

$result = array(    'x' => 'xxx',    'y' => 'yyy',);$aList = array(55,99,77,44);$bList = array('1-20', '3-55', '88-99', '40-45');$last = array();$f = array();foreach(array_reverse($aList, true) as $k=>$id){    $last = array('id'=>$id, 'percent'=>$bList[$k], 'other'=>$last);}// 若想去除最底的other=>array(),需要你在foreach中判断一下$result['other'] = $last;

$result = array(    'x' => 'xxx',    'y' => 'yyy',);$aList = array(55,99,77,44);$bList = array('1-20', '3-55', '88-99', '40-45');function listMerge($aList = array(), $bList = array()){	if(count($aList) != count($bList)){		return false;	}	$newList = array();	if(!empty($aList) && !empty($bList)){		$newList['id'] = $aList[0];		$newList['pcenter'] = $bList[0];		array_shift($aList);		array_shift($bList);		$newList['other'] = listMerge($aList, $bList);	}	return $newList;	}$result['other'] = listMerge($aList, $bList);echo "<pre class="brush:php;toolbar:false">";print_r($result);die;

メソッドは上記に示されており、再帰的メソッドです。上の階よりも少し複雑です...参考までに。同様に、一番下のother=>array()を削除したい場合は、判断する必要があります

ありがとうございます

$result['other'] = array(); $curPoint = &$ Result ['その他'];
for ($ i = 0; $ i & lt; count ($ Alist); $ i ++) {
$ resultitem = Array (); $ resultitem ['ID' ] = $ Alist i]
$resultItem ['percent'] = $bList[$i]; $curPoint = &$curPoint ['_complex'];

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。