8

I've searched how to push both key and value and I found this : How to push both value and key into array

But my question is how to add more than one key and value into an array?

$somearray :

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

I want to add this into $somearray :

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

So the output will be

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

I know this code will not work

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

Any help would be greatly appreciated. Thank you

Community
  • 1
  • 1
King Goeks
  • 492
  • 4
  • 11
  • 28

7 Answers7

7

You have to enclose the array key in quotes and also the value if its a string.If the value is an integer then there is no need of enclosing the value in quotes.But you must enclose the value in quotes if its a string.So you need to change he code like this

$arrayname['first_name'] = 'Michael';
$arrayname['last_name'] = 'George';
$arrayname['work'] = 'Google';
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
5

This is how I add all the elements from one array to another:

<?php
$oneArray = ['d', 'e', 'f'];
$anotherArray = ['a', 'b', 'c'];

array_push($anotherArray, ...$oneArray);
//['a', 'b', 'c', 'd', 'e', 'f'];
lordisp
  • 634
  • 9
  • 21
1

This will give you the idea:

<?

$array = array(
         [id] => 1);

$array["hello"] = "world";

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


?>
Sal00m
  • 2,938
  • 3
  • 22
  • 33
1

Syntax for adding value into array,

$ArrayName['IndexName'] = $elementValue;
Krish R
  • 22,583
  • 7
  • 50
  • 59
0

Try this:

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.
  • Thanks
Anand Solanki
  • 3,419
  • 4
  • 16
  • 27
0

Don't forget to put the quote when you assigning the value.

$arrayname[first_name] = 'Michael';
$arrayname[last_name] = 'George';
$arrayname[work] = 'Google';
King Goeks
  • 492
  • 4
  • 11
  • 28
0
$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);

Output like this

Array (
  [date] => Array (
    [0] => 10-08-2017
    [1] => 15-07-2016
  )
  [amt] => Array (
    [0] => 5000
    [1] => 2000
  )
)
Tony
  • 9,672
  • 3
  • 47
  • 75