How does PHP merge the following two arrays into an array?

Source: Internet
Author: User
The first array, such as:


A second array:


The effect of merging two arrays into an array is as follows:



Ask a master, how to realize, thank you!!


Reply to discussion (solution)

$arr 1 = Array ("0" =>1, ' 1 ' =>20, ' 2 ' =>37, ' 3 ' =>43); $arr 2 = Array (' 0 ' =>array (' times ' = ' 2014-02-25 ', ' Counts ' = ' + '), ' 1 ' =>array (' times ' = ' 2014-02-26 ', ' counts ' + ') ', ' 2 ' =>array (' times ' = ') 2014-02-27 ', ' counts ' = ' 6 '), ' 3 ' =>array (' times ' = ' 2014-02-28 ', ' counts ' = ' 8 ')); $arr = Array_intersect_ Key ($arr 2, $arr 1);  foreach ($arr 2 as $key = + $value) {$arr 2[$key] [' incnum '] = $arr 1[$key];unset ($arr 2[$key] [' counts ']);} Var_dump ($arr 2);

 
  ' 2014-02-25 ', ' counts ' = ' + '), Array (' Times ' = ' 2014-02-26 ', ' counts ' = ' + '), Array (' times ' = ') 2014-02-27 ', ' counts ' = ' 6 '), Array (' Times ' = ' 2014-02-28 ', ' counts ' = ' 8 ')); $c = Array (); for ($i =0, $len =count ($a); $i < $len; $i + +) {    Array_push ($c, Array (' times ' = $b [$i] [' Times '], ' incnum ' + $a [$i])} Var_dump ($c);? >

$a = Array (1,20,37,43), $b = Array (' Times ' = ' 2014-02-25 ', ' counts ' = ' + '), Array (' Times ' = ' 2014-02-26 ') , ' counts ' = ' + '), Array (' Times ' = ' 2014-02-27 ', ' counts ' = ' 6 '), Array (' Times ' = ' 2014-02-28 ', ' counts ' = > ' 8 '); $c = Array_map (function ($a, $b) {return array (' times ' = $b [' Times '], ' incnum ' + $a);}, $a, $b); Var_dump ( $C);

$arr 1 = Array ("0" =>1, ' 1 ' =>20, ' 2 ' =>37, ' 3 ' =>43);
$arr 2 = Array (
' 0 ' =>array (' times ' = ' 2014-02-25 ', ' counts ' = ' 19 '),
' 1 ' =>array (' times ' = ' 2014-02-26 ', ' counts ' = ' 17 '),
' 2 ' =>array (' times ' = ' 2014-02-27 ', ' counts ' = ' 6 '),
' 3 ' =>array (' times ' = ' 2014-02-28 ', ' counts ' = ' 8 ')
);
$arr 3=array ();
function my ($arr 1, $arr 2, $arr 3) {
$i = 0;
foreach ($arr 2 as $k = = $v) {
foreach ($v as $k _k = $v _v) {
if ($k _k== ' Times ') {
$arr 3[$i] [' date_time ']= $v _v;
$arr 3[$i] [' Incnum ']= $arr 1[$i];
$i + +;
}
}
}
return $arr 3;
}
Print_r (My ($arr 1, $arr 2, $arr 3));

Thank you: microlab2009,fdipzone,xuzuning Moderator, luo19880415 these four heroes, your code can run out of the effect I want, thank you. Divide it to everyone split!! Thank you again!!!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.