2015-04-27 129 views
0

如何元素添加到多维数组的数组,这将使我的输出如下面的代码如何将一个元素添加到维数组的每个数组?

的源代码: 看到有动态域名$ tgvalues [“名称”] = calneder 产品 金融尖 养生秘诀 在源代码中,我使用array_merge,看线的print_r(array_merge($ json1,$ newarray))个人事件

  foreach($json as $templategroup => $tgvalues){ 
        $templateval1 = "emailtemplates?expand=".$tgvalues['Name']; 
        $final1 = str_replace(' ','%20',trim($templateval1)); 
        $path1 = constant('AMT_BASE_PATH'); 
        $url1 = $path1.$final1; 
        $url1; 
        $ch1=curl_init(); 
        curl_setopt($ch1,CURLOPT_URL, $url1); 
        curl_setopt($ch1,CURLOPT_RETURNTRANSFER,1); 
        $buffer1 = curl_exec($ch1); 
        curl_close($ch1); 
        $json1 = json_decode($buffer1, true); 
        $newarray =array('templatename' =>$tgvalues['Name']); 
        print_r(array_merge($json1,$newarray)); 
        if (empty($buffer1)){ 
         print "There are some problem"; 
        } else { 
         $fetchtemplatename1 = Resources_Customers_Customer::fetchtemplategroups(); 
         $insert1 = array_diff_assoc($json1, $fetchtemplatename1); 

         //print_r($insert1); 
         $arr_tampgrop =array(); 
         foreach($json1 as $key1 => $value1) 
           { 


            $arr_tampgrop[] =array('Id'=>$value1['Id'],'Name'=>$value1['Name'],'Subject'=>$value1['Subject'],'Body'=>$value1['Body'],'Href'=>$value1['Href']); 


           } 
           //$arrtempname = array($tgvalues['Name']); 
           $arraypush = array_push($arr_tampgrop,$arrtempname["templatename"] = $tgvalues['Name']); 
           print_r($arraypush); 

           } 

          } 

;

此代码,我得到现在

 Array 
(
     [0] => Array 
    (
     [Id] => 1 
     [Name] => Merry Christmas         
     [Subject] => Merry Christmas 
     [Body] => This is the body 
     [Href] => 
    ) 

[1] => Array 
    (
     [Id] => 2 
     [Name] => Happy 4th of July         
     [Subject] => Happy 4th! 
     [Body] => This is the body of a July message 
     [Href] => 
    ) 

[templatename] => Calendar           
) 
3Array 
(
    [0] => Array 
    (
     [Id] => 3 
     [Name] => New Product 1          
     [Subject] => See this new product 
     [Body] => This is the body of a one new product 
     [Href] => 
    ) 

[1] => Array 
    (
     [Id] => 9 
     [Name] => Drip product 1          
     [Subject] => Drip new product 1 subject 
     [Body] => Drip new product 1 body 
     [Href] => 
    ) 

[2] => Array 
    (
     [Id] => 10 
     [Name] => Drip product 2          
     [Subject] => Drip new product 2 subject 
     [Body] => Drip new product 2 body 
     [Href] => 
    ) 

[3] => Array 
    (
     [Id] => 11 
     [Name] => Drip product 3          
     [Subject] => Drip new product 3 subject 
     [Body] => Drip new product 3 body 
     [Href] => 
    ) 

[templatename] => Products           
) 
5Array 
(
    [0] => Array 
    (
     [Id] => 4 
     [Name] => Happy Birthday          
     [Subject] => Happy Birthday 
     [Body] => This is the body of a birthday card 
     [Href] => 
    ) 

[1] => Array 
    (
     [Id] => 5 
     [Name] => Happy Anniversary         
     [Subject] => Happy Anniversary 
     [Body] => This is the body of a anniversary card 
     [Href] => 
    ) 

[2] => Array 
    (
     [Id] => 6 
     [Name] => After Days 0          
     [Subject] => We are glad you joined x0 days ago 
     [Body] => This is the body of a join x0 days ago card 
     [Href] => 
    ) 

[3] => Array 
    (
     [Id] => 7 
     [Name] => After Days 1          
     [Subject] => We are glad you joined x1 days ago 
     [Body] => This is the body of a join x1 days ago card 
     [Href] => 
    ) 

[4] => Array 
    (
     [Id] => 8 
     [Name] => After Days 2          
     [Subject] => We are glad you joined x2 days ago 
     [Body] => This is the body of a join x2 days ago card 
     [Href] => 
    ) 

[templatename] => Personal Events         
) 
     6Array 
     (
[0] => Array 
    (
     [Id] => 12 
     [Name] => Drip health tip 1         
     [Subject] => Drip health tip 1 subject 
     [Body] => Drip health tip 1 body 
     [Href] => 
    ) 

[1] => Array 
    ( 
     [templatename] => Health Tips 
     [Id] => 13 
     [Name] => Drip health tip 2         
     [Subject] => Drip health tip 2 subject 
     [Body] => Drip health tip 2 body 
     [Href] => 
    ) 

[2] => Array 
    (
     [templatename] => Health Tips 
     [Id] => 14 
     [Name] => Drip health tip 3         
     [Subject] => Drip health tip 3 subject 
     [Body] => Drip health tip 3 body 
     [Href] => 
    ) 


    ) 
     4Array 
     (
     [0] => Array 
    (
     [Id] => 15 
     [Name] => Drip financial tip 1        
     [Subject] => Drip financial tip 1 subject 
     [Body] => Drip financial tip 1 body 
     [Href] => 
    ) 

[1] => Array 
    (
     [Id] => 16 
     [Name] => Drip financial tip 2        
     [Subject] => Drip financial tip 2 subject 
     [Body] => Drip financial tip 2 body 
     [Href] => 
    ) 

[2] => Array 
    (
     [Id] => 17 
     [Name] => Drip financial tip 3        
     [Subject] => Drip financial tip 3 subject 
     [Body] => Drip financial tip 3 body 
     [Href] => 
    ) 

[templatename] => Financial Tips          
) 

所以,我想造成这样的下面。是有可能还是不

 Array 
(
     [0] => Array 
    ( 
     [templatename] => Calendar 
     [Id] => 1 
     [Name] => Merry Christmas         
     [Subject] => Merry Christmas 
     [Body] => This is the body 
     [Href] => 
    ) 

[1] => Array 
    (
     [templatename] => Calendar 
     [Id] => 2 
     [Name] => Happy 4th of July         
     [Subject] => Happy 4th! 
     [Body] => This is the body of a July message 
     [Href] => 
    ) 


) 
3Array 
(
    [0] => Array 
    (
     [templatename] => Products 
     [Id] => 3 
     [Name] => New Product 1          
     [Subject] => See this new product 
     [Body] => This is the body of a one new product 
     [Href] => 
    ) 

[1] => Array 
    (
     [Id] => 9 
     [templatename] => Products 
     [Name] => Drip product 1          
     [Subject] => Drip new product 1 subject 
     [Body] => Drip new product 1 body 
     [Href] => 
    ) 

[2] => Array 
    (
     [templatename] => Products 
     [Id] => 10 
     [Name] => Drip product 2          
     [Subject] => Drip new product 2 subject 
     [Body] => Drip new product 2 body 
     [Href] => 
    ) 

[3] => Array 
    (
     [templatename] => Products 
     [Id] => 11 
     [Name] => Drip product 3          
     [Subject] => Drip new product 3 subject 
     [Body] => Drip new product 3 body 
     [Href] => 
    ) 


) 
5Array 
(
    [0] => Array 
    (
     [templatename] => Personal Events 
     [Id] => 4 
     [Name] => Happy Birthday          
     [Subject] => Happy Birthday 
     [Body] => This is the body of a birthday card 
     [Href] => 
    ) 

[1] => Array 
    ( 
     [templatename] => Personal Events 
     [Id] => 5 
     [Name] => Happy Anniversary         
     [Subject] => Happy Anniversary 
     [Body] => This is the body of a anniversary card 
     [Href] => 
    ) 

[2] => Array 
    (
     [templatename] => Personal Events 
     [Id] => 6 
     [Name] => After Days 0          
     [Subject] => We are glad you joined x0 days ago 
     [Body] => This is the body of a join x0 days ago card 
     [Href] => 
    ) 

[3] => Array 
    (
     [templatename] => Personal Events 
     [Id] => 7 
     [Name] => After Days 1          
     [Subject] => We are glad you joined x1 days ago 
     [Body] => This is the body of a join x1 days ago card 
     [Href] => 
    ) 

[4] => Array 
    (
     [templatename] => Personal Events 
     [Id] => 8 
     [Name] => After Days 2          
     [Subject] => We are glad you joined x2 days ago 
     [Body] => This is the body of a join x2 days ago card 
     [Href] => 
    ) 


) 
     6Array 
     (
[0] => Array 
    (

     [templatename] => Health Tips 
     [Id] => 12 
     [Name] => Drip health tip 1         
     [Subject] => Drip health tip 1 subject 
     [Body] => Drip health tip 1 body 
     [Href] => 
    ) 

[1] => Array 
    (
     [templatename] => Health Tips 
     [Id] => 13 
     [Name] => Drip health tip 2         
     [Subject] => Drip health tip 2 subject 
     [Body] => Drip health tip 2 body 
     [Href] => 
    ) 

[2] => Array 
    (
     [templatename] => Health Tips 
     [Id] => 14 
     [Name] => Drip health tip 3         
     [Subject] => Drip health tip 3 subject 
     [Body] => Drip health tip 3 body 
     [Href] => 
    ) 


    ) 
    4Array 
     (
    [0] => Array 
    (
     [templatename] => Financial Tips 
     [Id] => 15 
     [Name] => Drip financial tip 1        
     [Subject] => Drip financial tip 1 subject 
     [Body] => Drip financial tip 1 body 
     [Href] => 
    ) 

[1] => Array 
    (
     [templatename] => Financial Tips 
     [Id] => 16 
     [Name] => Drip financial tip 2        
     [Subject] => Drip financial tip 2 subject 
     [Body] => Drip financial tip 2 body 
     [Href] => 
    ) 

[2] => Array 
    (
     [templatename] => Financial Tips 
     [Id] => 17 
     [Name] => Drip financial tip 3        
     [Subject] => Drip financial tip 3 subject 
     [Body] => Drip financial tip 3 body 
     [Href] => 
    ) 


) 

回答

0

从我所看到的,你会想改变行

$arr_tampgrop[] =array('Id'=>$value1['Id'],'Name'=>$value1['Name'],'Subject'=>$value1['Subject'],'Body'=>$value1['Body'],'Href'=>$value1['Href']); 

$arr_tampgrop[] =array('templatename'=>$tgvalues['Name'],'Id'=>$value1['Id'],'Name'=>$value1['Name'],'Subject'=>$value1['Subject'],'Body'=>$value1['Body'],'Href'=>$value1['Href']); 

,并删除,增加了TEMPLATENAME,你的任何线不希望它,如

$newarray =array('templatename' =>$tgvalues['Name']); 
print_r(array_merge($json1,$newarray)); 

&

$arraypush = array_push($arr_tampgrop,$arrtempname["templatename"] = $tgvalues['Name']); 

该代码在每个阵列的端部,而不是作为阵列的一部分被添加上的模板名称。它还会打印超过必要的数组,以便可以将其注释掉,以便输出更容易阅读。

+0

是的,你是对的所以我该怎么做。正在使用array_push我得到了o/p:35644,而不是数组的添加。 –

+0

只需删除您的array_push并使用print_r($ arr_tampgrop) – Zorgarath

0

您可以使用array_map来完成此操作。它允许您将函数应用于任何数组的每个元素。在你的情况下,你可以通过将array_map应用到父数组来为每个子数组添加一个元素。

简单的例子(可能需要调整您的需要)

$newArray = array_map(
    function($subArr) use ($tgvalues) { return $subArr['templatename'] = $tgvalues['Name']; }, 
    $json1 
); 

回调函数只能访问到它正在对数组的键和值。要使用回调定义的变量使用use ($var)

+0

它说undefind $ tgvalues ['Name']就地返回$ subArr ['templatename'] = $ templateName;作为值动态$ templateName = $ tgvalues ['名称'] –

+0

@smartdeveloper哦,是的,你必须使用'use'语法,我会更新。 – dan08

0

在取得TEMPLATENAME的价值在阵列状排列,我想

与元素即日历,健康提示,产品等

合并然后在foreach取所有值的子阵列并合并两个阵列

$arr_tampgrop[] = array_merge($arrgroup,$value1); 

in print_r(arr_tampgrop); result is obtained. 


$arrgroup = array("templatename" => $tgvalues['Name']); 
        $arr_tampgrop =array(); 
         foreach($json1 as $key1 => $value1) 
           { 
           $arr_tampgrop[] = array_merge($arrgroup,$value1); 
           } 
相关问题