ホームページ  >  記事  >  バックエンド開発  >  同じ値を見つけて結合するためにPHPで2次元配列を記述する方法を教えてください。

同じ値を見つけて結合するためにPHPで2次元配列を記述する方法を教えてください。

WBOY
WBOYオリジナル
2016-06-23 13:44:321014ブラウズ

これは配列です
main 関数は Thinkphp ビューモデルのクエリの結果です

各配列で同じタイトル値をクエリしてからマージしたいです 配列をマージするには、array_merge() を使用するようです
同じタイトルを見つけるにはどうすればよいですか? 配列内の値にインデックスを付けるのは問題ありませんか? key() 関数にこの機能があるようです

同じタイトルとその位置を詳しく書いてください。結合するときは、このフォームを直接使用するだけで大​​丈夫ですか?
簡単に言うと、このように写真を複数アップロードして記事を投稿するというものです 検索して記事が数千件あると配列が数倍になります 効率の良い計算量が使えるのでアドバイスお願いします〜
この方法です

array(2) {  [0] => array(5) {    ["picTitle"] => string(33) "标题1"    ["picCategroy"] => string(6) "分类"    ["picAuthor"] => string(12) "作者"    ["picPostTime"] => string(19) "2014-11-20 16:05:16"    ["pictureurl"] => string(55) "attachment/picture/uploadify/20141120/546da0784831c.png"  }  [1] => array(5) {    ["picTitle"] => string(33) "标题1"    ["picCategroy"] => string(6) "分类"    ["picAuthor"] => string(12) "作者"    ["picPostTime"] => string(19) "2014-11-20 16:05:16"    ["pictureurl"] => string(55) "attachment/picture/uploadify/20141120/546da0746edb8.png"  }}



ディスカッション(解決策)への返信
それで、最終的にどのような結果を達成したいですか?

異なる目的、異なるアルゴリズム


それで、最終的にどのような結果を達成したいですか?

目的が異なり、アルゴリズムも異なります

array(2) {  [0] => array(5) {    ["picTitle"] => string(33) "标题1"    ["picCategroy"] => string(6) "分类"    ["picAuthor"] => string(12) "作者"    ["picPostTime"] => string(19) "2014-11-20 16:05:16"    ["pictureurl"] => string(55) "attachment/picture/uploadify/20141120/546da0784831c.png,attachment/picture/uploadify/20141120/546da0746edb8.png"  }}

同じタイトルです。唯一の違いは画像の URL です

モデレータにアドバイスをお願いします~
$ar = array(  array(    "picTitle" => "标题1",    "picCategroy" => "分类",    "picAuthor" => "作者",    "picPostTime" => "2014-11-20 16:05:16",    "pictureurl" => "attachment/picture/uploadify/20141120/546da0784831c.png",  ),  array(    "picTitle" => "标题1",    "picCategroy" => "分类",    "picAuthor" => "作者",    "picPostTime" => "2014-11-20 16:05:16",    "pictureurl" => "attachment/picture/uploadify/20141120/546da0746edb8.png",  ),);foreach(call_user_func_array('array_merge_recursive', $ar) as $key=>$item) {  $res[$key] = join(',', array_unique($item));}var_export($res);
array (  'picTitle' => '标题1',  'picCategroy' => '分类',  'picAuthor' => '作者',  'picPostTime' => '2014-11-20 16:05:16',  'pictureurl' => 'attachment/picture/uploadify/20141120/546da0784831c.png,attachment/picture/uploadify/20141120/546da0746edb8.png',)


$ar = array(  array(    "picTitle" => "标题1",    "picCategroy" => "分类",    "picAuthor" => "作者",    "picPostTime" => "2014-11-20 16:05:16",    "pictureurl" => "attachment/picture/uploadify/20141120/546da0784831c.png",  ),  array(    "picTitle" => "标题1",    "picCategroy" => "分类",    "picAuthor" => "作者",    "picPostTime" => "2014-11-20 16:05:16",    "pictureurl" => "attachment/picture/uploadify/20141120/546da0746edb8.png",  ),);foreach(call_user_func_array('array_merge_recursive', $ar) as $key=>$item) {  $res[$key] = join(',', array_unique($item));}var_export($res);
array (  'picTitle' => '标题1',  'picCategroy' => '分类',  'picAuthor' => '作者',  'picPostTime' => '2014-11-20 16:05:16',  'pictureurl' => 'attachment/picture/uploadify/20141120/546da0784831c.png,attachment/picture/uploadify/20141120/546da0746edb8.png',)

モデレータはまだ苦労する必要があります見てください、これは同じタイトルの下に結合されています。少し複雑です

array(6) {  [0] => array(5) {    ["picTitle"] => string(7) "标题2"    ["picCategroy"] => string(6) "海报"    ["picAuthor"] => string(12) "星耀学园"    ["picPostTime"] => string(19) "2014-11-26 11:59:50"    ["pictureurl"] => string(55) "attachment/picture/uploadify/20141126/547550278b7db.jpg"  }  [1] => array(5) {    ["picTitle"] => string(7) "标题2"    ["picCategroy"] => string(6) "海报"    ["picAuthor"] => string(12) "星耀学园"    ["picPostTime"] => string(19) "2014-11-26 11:59:50"    ["pictureurl"] => string(55) "attachment/picture/uploadify/20141126/54755027ab89b.jpg"  }  [2] => array(5) {    ["picTitle"] => string(7) "标题2"    ["picCategroy"] => string(6) "海报"    ["picAuthor"] => string(12) "星耀学园"    ["picPostTime"] => string(19) "2014-11-26 11:59:50"    ["pictureurl"] => string(55) "attachment/picture/uploadify/20141126/547550273b753.jpg"  }  [3] => array(5) {    ["picTitle"] => string(7) "标题2"    ["picCategroy"] => string(6) "海报"    ["picAuthor"] => string(12) "星耀学园"    ["picPostTime"] => string(19) "2014-11-26 11:59:50"    ["pictureurl"] => string(55) "attachment/picture/uploadify/20141126/54755027d8488.jpg"  }  [4] => array(5) {    ["picTitle"] => string(33) "同步写入信息和附件表里"    ["picCategroy"] => string(6) "海报"    ["picAuthor"] => string(12) "星耀学园"    ["picPostTime"] => string(19) "2014-11-20 16:05:16"    ["pictureurl"] => string(55) "attachment/picture/uploadify/20141120/546da0746edb8.png"  }  [5] => array(5) {    ["picTitle"] => string(33) "同步写入信息和附件表里"    ["picCategroy"] => string(6) "海报"    ["picAuthor"] => string(12) "星耀学园"    ["picPostTime"] => string(19) "2014-11-20 16:05:16"    ["pictureurl"] => string(55) "attachment/picture/uploadify/20141120/546da0784831c.png"  }}

あなたの方法を使用すると、タイトルが異なるものと同じタイトルのものはすべて結合されると思います。タイトル 2 同じものを配列に結合します。これは、画像リンクです。これは、複数のタイトルの中で重複するタイトルがあるかどうかを判断する方法についてアドバイスをお願いします。 1 つのタイトルの複数の重複を含む配列の主な目的は、複数の異なるタイトルを区別することです。

array ( 'picTitle' => '标题2,同步写入信息和附件表里', 'picCategroy' => '海报', 'picAuthor' => '星耀学园', 'picPostTime' => '2014-11-26 11:59:50,2014-11-20 16:05:16', 'pictureurl' => 'attachment/picture/uploadify/20141126/547550278b7db.jpg,attachment/picture/uploadify/20141126/54755027ab89b.jpg,attachment/picture/uploadify/20141126/547550273b753.jpg,attachment/picture/uploadify/20141126/54755027d8488.jpg,attachment/picture/uploadify/20141120/546da0746edb8.png,attachment/picture/uploadify/20141120/546da0784831c.png', )


$ar = array(  array(    "picTitle" => "标题2",    "picCategroy" => "海报",    "picAuthor" => "星耀学园",    "picPostTime" => "2014-11-26 11:59:50",    "pictureurl" => "attachment/picture/uploadify/20141126/547550278b7db.jpg",  ),  array(    "picTitle" => "标题2",    "picCategroy" => "海报",    "picAuthor" => "星耀学园",    "picPostTime" => "2014-11-26 11:59:50",    "pictureurl" => "attachment/picture/uploadify/20141126/54755027ab89b.jpg",  ),  array(    "picTitle" => "标题2",    "picCategroy" => "海报",    "picAuthor" => "星耀学园",    "picPostTime" => "2014-11-26 11:59:50",    "pictureurl" => "attachment/picture/uploadify/20141126/547550273b753.jpg",  ),  array(    "picTitle" => "标题2",    "picCategroy" => "海报",    "picAuthor" => "星耀学园",    "picPostTime" => "2014-11-26 11:59:50",    "pictureurl" => "attachment/picture/uploadify/20141126/54755027d8488.jpg",  ),  array(    "picTitle" => "同步写入信息和附件表里",    "picCategroy" => "海报",    "picAuthor" => "星耀学园",    "picPostTime" => "2014-11-20 16:05:16",    "pictureurl" => "attachment/picture/uploadify/20141120/546da0746edb8.png",  ),  array(    "picTitle" => "同步写入信息和附件表里",    "picCategroy" => "海报",    "picAuthor" => "星耀学园",    "picPostTime" => "2014-11-20 16:05:16",    "pictureurl" => "attachment/picture/uploadify/20141120/546da0784831c.png",  ),);$res = array();foreach($ar as $item) {  if(! isset($res[$item['picTitle']])) $res[$item['picTitle']] = $item;  else $res[$item['picTitle']]['pictureurl'] .= ',' . $item['pictureurl'];}var_export(array_values($res));
array (  0 =>   array (    'picTitle' => '标题2',    'picCategroy' => '海报',    'picAuthor' => '星耀学园',    'picPostTime' => '2014-11-26 11:59:50',    'pictureurl' => 'attachment/picture/uploadify/20141126/547550278b7db.jpg,attachment/picture/uploadify/20141126/54755027ab89b.jpg,attachment/picture/uploadify/20141126/547550273b753.jpg,attachment/picture/uploadify/20141126/54755027d8488.jpg',  ),  1 =>   array (    'picTitle' => '同步写入信息和附件表里',    'picCategroy' => '海报',    'picAuthor' => '星耀学园',    'picPostTime' => '2014-11-20 16:05:16',    'pictureurl' => 'attachment/picture/uploadify/20141120/546da0746edb8.png,attachment/picture/uploadify/20141120/546da0784831c.png',  ),)


書き留めただけで、まだテストしていません。これは大文字のアイデアです

$ar = array(

array(

“picTitle” => "タイトル 1",
"picCategroy" => "カテゴリ"、
"picAuthor" => "作成者"、
"picPostTime" => "2014-11-20 16:05:16"、
"pictureurl" => attachment/picture/uploadify/ 20141120/546da0784831c.png",
),
array(
"picTitle" => "タイトル 1",
"picCategroy" => "カテゴリ",
"picAuthor" => "作成者"、
"picPostTime " => "2014-11-20 16:05:16"、
"pictureurl" => "attachment/picture/uploadify/20141120/546da0746edb8.png"、
)、
) ;

//Title= >配列インデックス array
$map_arr=array();
foreach($ar as $key=>$ite​​m)
{
//title=>配列内にあるかどうかを判定Index array
if(isset($map_arr [$item['picTitle']]))
{
//
があればマージ $ar[$key]=array_merge($ar[$key],$map_arr[$ item['picTitle']]);
}
else
{
//そうでない場合は、マッピングインデックスを保存します
$map_arr[$item['picTitle']]=$item; ); //配列にもともと存在していた値を削除します
}
}


補足 unset($ar[$key]); //配列にもともと存在していた値を削除します
うっかり間違って書いてしまいました
//中の単語をマージします
$ar[ $key]=array_merge($ar[$key],$map_arr[$item['picTitle']]);

$arr = array(  array(    "picTitle" => "标题1",    "picCategroy" => "分类",    "picAuthor" => "作者",    "picPostTime" => "2014-11-20 16:05:16",    "pictureurl" => "attachment/picture/uploadify/20141120/546da0784831c.png",  ),  array(    "picTitle" => "标题1",    "picCategroy" => "分类",    "picAuthor" => "作者",    "picPostTime" => "2014-11-20 16:05:16",    "pictureurl" => "attachment/picture/uploadify/20141120/546da0746edb8.png",  ));$item=array();foreach($arr as $key=>$value){    foreach($value as $k=>$v){        if(!isset($item[$k])){            $item[$k]=$v;        }    }}print_r($item);



$ar = array(  array(    "picTitle" => "标题2",    "picCategroy" => "海报",    "picAuthor" => "星耀学园",    "picPostTime" => "2014-11-26 11:59:50",    "pictureurl" => "attachment/picture/uploadify/20141126/547550278b7db.jpg",  ),  array(    "picTitle" => "标题2",    "picCategroy" => "海报",    "picAuthor" => "星耀学园",    "picPostTime" => "2014-11-26 11:59:50",    "pictureurl" => "attachment/picture/uploadify/20141126/54755027ab89b.jpg",  ),  array(    "picTitle" => "标题2",    "picCategroy" => "海报",    "picAuthor" => "星耀学园",    "picPostTime" => "2014-11-26 11:59:50",    "pictureurl" => "attachment/picture/uploadify/20141126/547550273b753.jpg",  ),  array(    "picTitle" => "标题2",    "picCategroy" => "海报",    "picAuthor" => "星耀学园",    "picPostTime" => "2014-11-26 11:59:50",    "pictureurl" => "attachment/picture/uploadify/20141126/54755027d8488.jpg",  ),  array(    "picTitle" => "同步写入信息和附件表里",    "picCategroy" => "海报",    "picAuthor" => "星耀学园",    "picPostTime" => "2014-11-20 16:05:16",    "pictureurl" => "attachment/picture/uploadify/20141120/546da0746edb8.png",  ),  array(    "picTitle" => "同步写入信息和附件表里",    "picCategroy" => "海报",    "picAuthor" => "星耀学园",    "picPostTime" => "2014-11-20 16:05:16",    "pictureurl" => "attachment/picture/uploadify/20141120/546da0784831c.png",  ),);$res = array();foreach($ar as $item) {  if(! isset($res[$item['picTitle']])) $res[$item['picTitle']] = $item;  else $res[$item['picTitle']]['pictureurl'] .= ',' . $item['pictureurl'];}var_export(array_values($res));
array (  0 =>   array (    'picTitle' => '标题2',    'picCategroy' => '海报',    'picAuthor' => '星耀学园',    'picPostTime' => '2014-11-26 11:59:50',    'pictureurl' => 'attachment/picture/uploadify/20141126/547550278b7db.jpg,attachment/picture/uploadify/20141126/54755027ab89b.jpg,attachment/picture/uploadify/20141126/547550273b753.jpg,attachment/picture/uploadify/20141126/54755027d8488.jpg',  ),  1 =>   array (    'picTitle' => '同步写入信息和附件表里',    'picCategroy' => '海报',    'picAuthor' => '星耀学园',    'picPostTime' => '2014-11-20 16:05:16',    'pictureurl' => 'attachment/picture/uploadify/20141120/546da0746edb8.png,attachment/picture/uploadify/20141120/546da0784831c.png',  ),)


モデレータさん、解決してくれてありがとう、でも最後のは var_export 関数を削除することが私が望む結果です〜

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