Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中的数组元素中添加更多键值_Php_Arrays - Fatal编程技术网

在php中的数组元素中添加更多键值

在php中的数组元素中添加更多键值,php,arrays,Php,Arrays,假设我有一个数组,它的第一个元素是 Array ( [name] => gaurav pandey [education] => MCA ) 现在我想插入更多属性,因此最终结果应该如下所示: Array ( [name] => gaurav pandey [education] => MCA [occupation] => developer [passion] => programming) 如何在php中实现这一点?我已经看到了实例及其属性的动态创建,

假设我有一个数组,它的第一个元素是

Array ( [name] => gaurav pandey [education] => MCA )
现在我想插入更多属性,因此最终结果应该如下所示:

Array ( [name] => gaurav pandey [education] => MCA [occupation] => developer [passion] => programming)

如何在php中实现这一点?我已经看到了实例及其属性的动态创建,但仍然不知道如何在php数组中实现它。

我很确定您只是在问如何在数组中插入新的键/值,这是一个非常基本的php语法问题

具体见:

要更改某个值,请使用该元素的键为该元素指定一个新值。要删除键/值对,请对其调用unset()函数

 <?php
 $arr = array(5 => 1, 12 => 2);

 $arr[] = 56;    // This is the same as $arr[13] = 56;
                // at this point of the script

 $arr["x"] = 42; // This adds a new element to
                // the array with key "x"

 unset($arr[5]); // This removes the element from the array

 unset($arr);    // This deletes the whole array
 ?>


用于向数组添加属性的语法:

$a = array (
    "name" => "gaurav pandey",
    "education" => "MCA"
);
$a["occupation"] = "developer";
$a["passion"] = "programming"

你应该从阅读第一部分开始。并检查此示例:

// create the associative array:
$array = array(
    'name' => 'gaurav pandey'
);

// add elements to it
$array ['education'] = 'MCA';
$array ['occupation'] = 'Developer';

除了@meagar的帖子外,我还建议您看看php手册中的array_函数页面:

例如,组合数组、遍历数组、排序数组等

还可以合并数组

<?php
$array1 = array("name" => "gaurav pandey","education" => "MCA");
$array2 = array("color" => "green", "shape" => "trapezoid", "occupation" => "developer", "passion" => "programming");
$result = array_merge($array1, $array2);
print_r($result);
?>


您也可以使用
array\u push()
。如果您一次向阵列中添加多个项目,但会增加一些开销,则此方法非常方便。

请学习自己解决这些基本问题。谷歌搜索并阅读几乎所有的搜索结果,而不是问一些如此基本的问题。@meagar请放心,一些在958左右拥有代表的人知道stackoverflow的基本规则。。只是我来自一个.net的后台,所以很多时候搜索这些东西变得很困难。。我已经搜索过了,然后在这里问这个问题。像我这样的开发人员的问题是,我把关键词放在谷歌上,可以立即给我.net特定的结果。。但是在php中,很多时候我都不能得到相同的结果。你知道我刚刚开始喜欢php。真的吗?您知道如何获取.Net结果,但不知道如何获取PHP结果吗?你试过在搜索词中加入“PHP”吗?如果你在谷歌上搜索“PHP数组”,第一个结果就是答案。我很抱歉,但这些简单且容易回答的问题正是我们将“显示出没有研究努力”作为否决票理由的原因。不,不要采取其他明智的做法。。我只是说我试过了,但没办法解决这个问题,就是这样。我的意思是,在.net中,我通常可以从谷歌获得90%的东西,但在php中,我必须从同行那里获得帮助,等等。