【问题标题】:How to push multiple key and value into php array?如何将多个键和值推送到 php 数组中?
【发布时间】:2013-11-09 17:19:27
【问题描述】:

我搜索了如何同时推送键和值,我发现了这个:How to push both value and key into array

但我的问题是如何将多个键和值添加到数组中?

$somearray

Array ( 
[id] => 1645819602 
[name] => Michael George) 

我想将此添加到$somearray 中:

[first_name] => Michael 
[last_name] => George
[work] => Google

所以输出将是

Array ( 
    [id] => 1645819602 
    [name] => Michael George
    [first_name] => Michael 
    [last_name] => George
    [work] => Google) 

我知道这段代码行不通

$arrayname[first_name] = Michael;
$arrayname[last_name] = George;
$arrayname[work] = Google;

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 您的 PHP 代码中有语法错误。错误消息应该已经告诉你了。

标签: php arrays


【解决方案1】:

您必须将数组键括在引号中,如果它是字符串,则还必须将值括起来。如果值是整数,则不需要将值括在引号中。但如果它是a,则必须将值括在引号中字符串。所以你需要像这样更改他的代码

$arrayname['first_name'] = 'Michael';
$arrayname['last_name'] = 'George';
$arrayname['work'] = 'Google';

【讨论】:

    【解决方案2】:

    这会给你的想法:

    <?
    
    $array = array(
             [id] => 1);
    
    $array["hello"] = "world";
    
    print_r($array); //prints Array (
                                 [id] => 1,
                                 [hello] => "world")
    
    
    ?>
    

    【讨论】:

      【解决方案3】:

      向数组中添加值的语法,

      $ArrayName['IndexName'] = $elementValue;
      

      【讨论】:

        【解决方案4】:

        试试这个:

        Here you need to add quotes to wrap index.
        
        <?php
        $arrayname['first_name'] = 'Michael';
        $arrayname['last_name'] = 'George';
        $arrayname['work'] = 'Google';
        ?>
        
        Always use this when assigning any value in the array.
        
        • 谢谢

        【讨论】:

          【解决方案5】:

          分配值时不要忘记输入quote

          $arrayname[first_name] = 'Michael';
          $arrayname[last_name] = 'George';
          $arrayname[work] = 'Google';
          

          【讨论】:

            【解决方案6】:
            $ac_re_arr['date']      = array();
            $ac_re_arr['amt']       = array();
            
            $sql5   = mysql_query(" SELECT `id`,`bank_dues_amt`,`bank_dues` FROM `tbl_act` where `bank_dues_amt` !='' and `case_id`='$case_id' ")or die(mysql_error());
            while($data5    = mysql_fetch_array($sql5))
            {
                $amt3       = explode('$',$data5['bank_dues_amt']);
                $date3      = explode('$',$data5['bank_dues']);
                $k          = 0;
                foreach($amt3 as $key3)
                {
                    array_push($ac_re_arr['date'],$date3[$k]);
                    array_push($ac_re_arr['amt'],$amt3[$k]);
                    $k++;
                }
            }
            print_r($ac_re_arr);
            

            这样的输出

            数组 ( [日期] => 数组 ( [0] => 10-08-2017 [1] => 15-07-2016 ) [amt] => 数组 ( [0] => 5000 [1] = > 2000 ) )

            【讨论】:

              【解决方案7】:

              这就是我将一个数组中的所有元素添加到另一个数组的方法:

              <?php
              $oneArray = ['d', 'e', 'f'];
              $anotherArray = ['a', 'b', 'c'];
              
              array_push($anotherArray, ...$oneArray);
              //['a', 'b', 'c', 'd', 'e', 'f'];
              

              【讨论】:

                猜你喜欢
                • 2011-01-08
                • 2017-09-25
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-06-17
                相关资源
                最近更新 更多