How can I write a php two-dimensional array to find the same value? This is an array
The main function is the query results in the Thinkphp view model.
Query the same title value in each array and then merge and merge the array. it seems that array_merge () is used ()
How can I find the same title? Can I retrieve the index value in the array through foreach traversal? does it look like the key () function has this function?
Can the position of the element in the same title array be used directly when the elements are returned for merging?
Array_merge (array [0], array [1])
The same title is then merged with the connection fields of the image, so pictureurl is different.
To put it simply, if you want to find thousands of articles such as multigraph Upload and publishing, the array will be several times as efficient as computing ~
This way
Array (2) {[0] => array (5) {["picTitle"] => string (33) "Title 1" ["picCategroy"] => string (6) "category" ["picAuthor"] => string (12) "author" ["picPostTime"] => string (19) "16:05:16" ["pictureurl"] => string (55) "attachment/picture/uploadify/20141120/546 da0784831c.png"} [1] => array (5) {["picTitle"] => string (33) "Title 1" ["picCategroy"] => string (6) "category" ["picAuthor"] => string (12) "author" ["picPostTime"] => string (19) "16:05:16" ["pictureurl"] => string (55) "attachment/picture/uploadify/20141120/546 da0746edb8.png "}}
Reply to discussion (solution)
What kind of result will you achieve?
Different purposes and algorithms
What kind of result will you achieve?
Different purposes and algorithms
Array (2) {[0] => array (5) {["picTitle"] => string (33) "Title 1" ["picCategroy"] => string (6) "category" ["picAuthor"] => string (12) "author" ["picPostTime"] => string (19) "16:05:16" ["pictureurl"] => string (55) "attachment/picture/uploadify/20141120/546 da0784831c.png, attachment/picture/uploadify/20141120/546 da0746edb8.png "}}
Pictureurl is the same title.
Merge to solve the problem ~
$ Ar = array ("picTitle" => "Title 1", "picCategroy" => "category", "picAuthor" => "author ", "picPostTime" => "16:05:16", "pictureurl" => "attachment/picture/uploadify/20141120/546 da0784831c.png",), array ("picTitle" => "Title 1 ", "picCategroy" => "category", "picAuthor" => "author", "picPostTime" => "16:05:16 ", "pictureurl" => "attachment/picture/uploadify/20141120/546 da0746edb8.png",),); foreach (call_user_func_array ('Array _ merge_recursive ', $ ar) as $ key => $ item) {$ res [$ key] = join (',', array_unique ($ item);} var_export ($ res );
Array ('pictitle' => 'Title 1', 'piccategroy '=> 'Category', 'picauthor' => 'authorization ', 'picposttime' => '2017-11-20 16:05:16 ', 'ubuntuurl' => 'attachment/picture/uploadify/2014 da0784831c.png, attachment/picture/uploadify/20141120/546 da0746edb8.png ',)
$ Ar = array ("picTitle" => "Title 1", "picCategroy" => "category", "picAuthor" => "author ", "picPostTime" => "16:05:16", "pictureurl" => "attachment/picture/uploadify/20141120/546 da0784831c.png",), array ("picTitle" => "Title 1 ", "picCategroy" => "category", "picAuthor" => "author", "picPostTime" => "16:05:16 ", "pictureurl" => "attachment/picture/uploadify/20141120/546 da0746edb8.png",),); foreach (call_user_func_array ('Array _ merge_recursive ', $ ar) as $ key => $ item) {$ res [$ key] = join (',', array_unique ($ item);} var_export ($ res );
Array ('pictitle' => 'Title 1', 'piccategroy '=> 'Category', 'picauthor' => 'authorization ', 'picposttime' => '2017-11-20 16:05:16 ', 'ubuntuurl' => 'attachment/picture/uploadify/2014 da0784831c.png, attachment/picture/uploadify/20141120/546 da0746edb8.png ',)
It seems complicated for moderators to merge under the same title.
Array (6) {[0] => array (5) {["picTitle"] => string (7) "Title 2" ["picCategroy"] => string (6) "poster" ["picAuthor"] => string (12) "Xingyao school" ["picPostTime"] => string (19) "11:59:50" ["pictureurl"] => string (55) "attachment/picture/uploadify/20141126/547550278 b7db.jpg"} [1] => array (5) {["picTitle"] => string (7) "Title 2" ["picCategroy"] => string (6) "poster" ["picAuthor"] => string (12) "Xingyao school" ["picPostTime"] => string (19) "11:59:50" ["pictureurl"] => string (55) "attachment/picture/uploadify/20141126/54755027 ab89b.jpg"} [2] => array (5) {["picTitle"] => string (7) "Title 2" ["picCategroy"] => string (6) "poster" ["picAuthor"] => string (12) "Xingyao school" ["picPostTime"] => string (19) "11:59:50" ["pictureurl"] => string (55) "attachment/picture/uploadify/20141126/547550273 b753.jpg"} [3] => array (5) {["picTitle"] => string (7) "Title 2" ["picCategroy"] => string (6) "poster" ["picAuthor"] => string (12) "Xingyao school" ["picPostTime"] => string (19) "11:59:50" ["pictureurl"] => string (55) "attachment/picture/uploadify/20141126/54755027 d8488.jpg"} [4] => array (5) {["picTitle"] => string (33) "synchronous write information and attachment table" ["picCategroy"] => string (6) "poster" ["picAuthor"] => string (12) "Xingyao school" ["picPostTime"] => string (19) "16:05:16" ["pictureurl"] => string (55) "attachment/picture/uploadify/20141120/546 da0746edb8.png"} [5] => array (5) {["picTitle"] => string (33) "synchronous write information and attachment table" ["picCategroy"] => string (6) "poster" ["picAuthor"] => string (12) "Xingyao school" ["picPostTime"] => string (19) "16:05:16" ["pictureurl"] => string (55) "attachment/picture/uploadify/20141120/546 da0784831c.png "}}
After you complete the processing, all of them are merged. I think different titles are different. Titles 1 with the same title are merged together. an array with the same title 2 is merged together into an array. this is the merge of the image link pictureurl. how can we judge that the title is duplicated in many titles? this is definitely a title with multiple repeated arrays, it mainly distinguishes multiple titles.
Array ('pictitle' => 'Title 2, synchronous writing to information and accessories table', 'piccategroy '=> 'poster', 'picauthor' => 'xingyao Emy ', 'picposttime' => '2017-11-26 11:59:50, 2014 16:05:16 ', 'pictureurl' => 'attachment/picture/uploadify/20141126/547550278 b7db.jpg, attachment/picture/uploadify/20141126/54755027 ab89b.jpg, attachment/picture/uploadify/20141126/547550273 b753.jpg, attachment/picture/uploadify/20141126/54755027 d8488.jpg, attachment/picture/uploadify/20141120/546 parts, attachment/picture/uploadify/20141120/546 da0784831c.png ',)
$ Ar = array ("picTitle" => "title 2", "picCategroy" => "poster", "picAuthor" => "", "picPostTime" => "11:59:50", "pictureurl" => "attachment/picture/uploadify/20141126/547550278 b7db.jpg",), array ("picTitle" => "title 2 ", "picCategroy" => "poster", "picAuthor" => "Xingyao School", "picPostTime" => "11:59:50 ", "pictureurl" => "attachment/picture/uploadify/20141126/54755027 ab89b.jpg",), arra Y ("picTitle" => "title 2", "picCategroy" => "poster", "picAuthor" => "Xingyao School ", "picPostTime" => "11:59:50", "pictureurl" => "attachment/picture/uploadify/20141126/547550273 b753.jpg",), array ("picTitle" => "title 2 ", "picCategroy" => "poster", "picAuthor" => "Xingyao School", "picPostTime" => "11:59:50 ", "pictureurl" => "attachment/picture/uploadify/20141126/54755027 d8488.jpg",), array ("picTitle" => "Write information and attachments synchronously", "picCategroy" => "poster", "picAuthor" => "Xingyao Emy-Park", "picPostTime" => "16:05:16 ", "pictureurl" => "attachment/picture/uploadify/20141120/546 da0746edb8.png",), array ("picTitle" => "synchronous write information and attachment table ", "picCategroy" => "poster", "picAuthor" => "Xingyao School", "picPostTime" => "16:05:16 ", "pictureurl" => "attachment/picture/uploadify/20141120/546 da0784831c.png",),); $ res = array (); fore Ach ($ 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' => 'Title 2', 'piccategroy '=> 'poster', 'picauthor' => 'xingyao Emy ', 'picposttime' => '2017-11-26 11:59:50 ', 'ubuntuurl' => 'attachment/picture/uploadify/2014 b7db.jpg, attachment/picture/uploadify/20141126/547550278 ab89b.jpg, attachment/picture/uploadify/20141126/547550273 b753.jpg, attachment/picture/uploadify/20141126/54755027 d8488.jpg ',), 1 => array ('pictitle' => 'synchronize write information and attachment table ', 'piccategroy '=> 'poster', 'picauthor' => 'xingyao Emy ', 'picposttime' => '2017-11-20 16:05:16 ', 'pictureurl' => 'attachment/picture/uploadify/20141120/546 da0746edb8.png, attachment/picture/uploadify/20141120/546 da0784831c.png ',),)
I wrote it casually, but I haven't tested it. it's such a thought.
$ Ar = array (
Array (
"PicTitle" => "Title 1 ",
"PicCategroy" => "category ",
"PicAuthor" => "author ",
"PicPostTime" => "16:05:16 ",
"Pictureurl" => "attachment/picture/uploadify/20141120/546 da0784831c.png ",
),
Array (
"PicTitle" => "Title 1 ",
"PicCategroy" => "category ",
"PicAuthor" => "author ",
"PicPostTime" => "16:05:16 ",
"Pictureurl" => "attachment/picture/uploadify/20141120/546 da0746edb8.png ",
),
);
// Title => array index array
$ Map_arr = array ();
Foreach ($ ar as $ key => $ item)
{
// Determine whether the index is in the index array.
If (isset ($ map_arr [$ item ['pictitle'])
{
// Merge
$ Ar [$ key] = array_merge ($ ar [$ key], $ map_arr [$ item ['pictitle']);
}
Else
{
// If not, store the ing index
$ Map_arr [$ item ['pictitle'] = $ item;
Unset ($ ar [$ key]); // delete the original value in the array
}
}
Supplement the unset ($ ar [$ key]); // delete the original value in the array
This sentence is moved below, and it is neglected to write an error.
// Merge
$ Ar [$ key] = array_merge ($ ar [$ key], $ map_arr [$ item ['pictitle']);
$ Arr = array ("picTitle" => "Title 1", "picCategroy" => "category", "picAuthor" => "author ", "picPostTime" => "16:05:16", "pictureurl" => "attachment/picture/uploadify/20141120/546 da0784831c.png",), array ("picTitle" => "Title 1 ", "picCategroy" => "category", "picAuthor" => "author", "picPostTime" => "16:05:16 ", "pictureurl" => "attachment/picture/uploadify/20141120/546 da0746edb8.png",); $ item = Rray (); foreach ($ arr as $ key => $ value) {foreach ($ value as $ k => $ v) {if (! Isset ($ item [$ k]) {$ item [$ k] = $ v ;}} print_r ($ item );
$ Ar = array ("picTitle" => "title 2", "picCategroy" => "poster", "picAuthor" => "", "picPostTime" => "11:59:50", "pictureurl" => "attachment/picture/uploadify/20141126/547550278 b7db.jpg",), array ("picTitle" => "title 2 ", "picCategroy" => "poster", "picAuthor" => "Xingyao School", "picPostTime" => "11:59:50 ", "pictureurl" => "attachment/picture/uploadify/20141126/54755027 ab89b.jpg",), arra Y ("picTitle" => "title 2", "picCategroy" => "poster", "picAuthor" => "Xingyao School ", "picPostTime" => "11:59:50", "pictureurl" => "attachment/picture/uploadify/20141126/547550273 b753.jpg",), array ("picTitle" => "title 2 ", "picCategroy" => "poster", "picAuthor" => "Xingyao School", "picPostTime" => "11:59:50 ", "pictureurl" => "attachment/picture/uploadify/20141126/54755027 d8488.jpg",), array ("picTitle" => "Write information and attachments synchronously", "picCategroy" => "poster", "picAuthor" => "Xingyao Emy-Park", "picPostTime" => "16:05:16 ", "pictureurl" => "attachment/picture/uploadify/20141120/546 da0746edb8.png",), array ("picTitle" => "synchronous write information and attachment table ", "picCategroy" => "poster", "picAuthor" => "Xingyao School", "picPostTime" => "16:05:16 ", "pictureurl" => "attachment/picture/uploadify/20141120/546 da0784831c.png",),); $ res = array (); fore Ach ($ 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' => 'Title 2', 'piccategroy '=> 'poster', 'picauthor' => 'xingyao Emy ', 'picposttime' => '2017-11-26 11:59:50 ', 'ubuntuurl' => 'attachment/picture/uploadify/2014 b7db.jpg, attachment/picture/uploadify/20141126/547550278 ab89b.jpg, attachment/picture/uploadify/20141126/547550273 b753.jpg, attachment/picture/uploadify/20141126/54755027 d8488.jpg ',), 1 => array ('pictitle' => 'synchronize write information and attachment table ', 'piccategroy '=> 'poster', 'picauthor' => 'xingyao Emy ', 'picposttime' => '2017-11-20 16:05:16 ', 'pictureurl' => 'attachment/picture/uploadify/20141120/546 da0746edb8.png, attachment/picture/uploadify/20141120/546 da0784831c.png ',),)
Thank you! the moderator has solved the problem, but the final var_export function has been removed as a result ~