当前位置: 动力学知识库 > 问答 > 编程问答 >

php - how to insert key to an array

问题描述:

my idea below

<?php

$arr=array("dd");

for ($i=0; $i <=4 ; $i++) {

$arr[$i];

}

?>

it showing an error can any body help me. Any help is appreciable

网友答案:

You are only refering to the key in the array, you have to set it or read from it. Because the key does not exist, the only option that remains is to set a value to it.

$arr[$i] = 'something';

Be careful with setting 'null' to the value. isset will return false when a value is null but the key exists.

网友答案:

Try this code :-

<?php
$arr=array();
for ($i=0; $i <=4 ; $i++) { 
    //$arr[rand(10,100)]=rand(10,1000);
    $arr[$i.'abc']=$i.'abcd';
}
print "<pre>";
print_r($arr);
print "</pre>";
?>
网友答案:

To create an array with key and value you should do the following :

$array = array();
$array['key'] = 'value';

You can also take a look at array_push() that push value at the end of an array

http://php.net/manual/fr/function.array-push.php

网友答案:

To insert key to an array:

$arr["key"] = "value";
分享给朋友:
您可能感兴趣的文章:
随机阅读: