php recordarray,Array 数组 - [ php中文手册 ] - 在线原生手册 - php中文网
用戶評論:
[#1]
florenxe [2015-10-07 18:53:45]
//a?nice?little?way?to?print?leap?years?using?array
for?($leap?=?2032;?$leap?
{
$arr?=?[];
array_push($arr,?$leap);
foreach($arr?as?$a)?{
echo?($a).PHP_EOL;
}
}
[#2]
aditycse at gmail dot com [2015-08-10 09:23:11]
//Array?can?have?following?data?type?in?key?i.e?string,integer
//Behaviour?of?array?in?case?of?array?key?has?data?type?float?or?double
$exampleArray?=?array(0,
1,
"2.99999999"?=>?56,
2?=>?2,
3.9999?=>?3,
3?=>?3.1,
true?=>?4,
false?=>?6,
);
//array?structure
print_r($exampleArray);
//array?of?array?keys
print_r(array_keys($exampleArray));
[#3]
perske at uni-muenster dot de [2015-04-18 23:16:20]
Regarding?this?note:????Strings?containing?valid?integers?will?be?cast?to?the?integer?type.??
This?is?true?only?for?decimal?integers?without?leading???+??,?but?not?for?octal,?hexadecimal,?or?binary?integers.
Example:
$array=?array("0"=>"a","-1"=>"b","+1"=>"c","00"=>"d","01"=>"e","0x1"=>"f",
);var_dump($array);?>
This?example?will?output:
array(7)?{
[0]=>
string(1)?"a"
[-1]=>
string(1)?"b"
["+1"]=>
string(1)?"c"
["00"]=>
string(1)?"d"
["01"]=>
string(1)?"e"
["0x1"]=>
string(1)?"f"
}
Thus?different?strings?are?always?mapped?to?different?array?keys.
[#4]
mathiasgrimm at gmail dot com [2014-09-29 16:20:22]
$a['a']?=null;$a['b']?=?array();
echo$a['a']['non-existent'];//?DOES?NOT?throw?an?E_NOTICE?error?as?expected.echo$a['b']['non-existent'];//?throws?an?E_NOTICE?as?expected?>
I?added?this?bug?to?bugs.php.net?(https://bugs.php.net/bug.php?id=68110)
however?I?made?tests?with?php4,?5.4?and?5.5?versions?and?all?behave?the?same?way.
This,?in?my?point?of?view,?should?be?cast?to?an?array?type?and?throw?the?same?error.
This?is,?according?to?the?documentation?on?this?page,?wrong.
From?doc:
"Note:
Attempting?to?access?an?array?key?which?has?not?been?defined?is?the?same?as?accessing?any?other?undefined?variable:?an?E_NOTICE-level?error?message?will?be?issued,?and?the?result?will?be?NULL."
[#5]
brta dot akos at gmail dot com [2014-01-10 13:45:43]
Why?not?to?user?one-based?arrays:
$a=?array(1=>'a','b','d');print_r($a);array_splice($a,2,0,'c');print_r($a);?>
output:
Array?(?[1]?=>?a?[2]?=>?b?[3]?=>?d?)?Array?(?[0]?=>?a?[1]?=>?b?[2]?=>?c?[3]?=>?d?)
[#6]
note dot php dot lorriman at spamgourmet dot org [2014-01-09 15:01:55]
There?is?another?kind?of?array?(php>=??5.3.0)?produced?by
$array?=?new?SplFixedArray(5);
Standard?arrays,?as?documented?here,?are?marvellously?flexible?and,?due?to?the?underlying?hashtable,?extremely?fast?for?certain?kinds?of?lookup?operation.
Supposing?a?large?string-keyed?array
$arr=['string1'=>$data1,?'string2'=>$data2?etc....]
when?getting?the?keyed?data?with
$data=$arr['string1'];
php?does?*not*?have?to?search?through?the?array?comparing?each?key?string?to?the?given?key?('string1')?one?by?one,?which?could?take?a?long?time?with?a?large?array.?Instead?the?hashtable?means?that?php?takes?the?given?key?string?and?computes?from?it?the?memory?location?of?the?keyed?data,?and?then?instantly?retrieves?the?data.?Marvellous!?And?so?quick.?And?no?need?to?know?anything?about?hashtables?as?it's?all?hidden?away.
However,?there?is?a?lot?of?overhead?in?that.?It?uses?lots?of?memory,?as?hashtables?tend?to?(also?nearly?doubling?on?a?64bit?server),?and?should?be?significantly?slower?for?integer?keyed?arrays?than?old-fashioned?(non-hashtable)?integer-keyed?arrays.?For?that?see?more?on?SplFixedArray?:
http://uk3.php.net/SplFixedArray
Unlike?a?standard?php?(hashtabled)?array,?if?you?lookup?by?integer?then?the?integer?itself?denotes?the?memory?location?of?the?data,?no?hashtable?computation?on?the?integer?key?needed.?This?is?much?quicker.?It's?also?quicker?to?build?the?array?compared?to?the?complex?operations?needed?for?hashtables.?And?it?uses?a?lot?less?memory?as?there?is?no?hashtable?data?structure.?This?is?really?an?optimisation?decision,?but?in?some?cases?of?large?integer?keyed?arrays?it?may?significantly?reduce?server?memory?and?increase?performance?(including?the?avoiding?of?expensive?memory?deallocation?of?hashtable?arrays?at?the?exiting?of?the?script).
[#7]
ivegner at yandex dot ru [2013-08-28 08:43:28]
Note?that?objects?of?classes?extending?ArrayObject?SPL?class?are?treated?as?arrays,?and?not?as?objects?when?converting?to?array.
private$private='private';
public$hello='world';
}$object=?newArrayObjectExtended();$array=?(array)$object;//?This?will?not?expose?$private?and?$hello?properties?of?$object,
//?but?return?an?empty?array?instead.var_export($array);?>
[#8]
chris at ocportal dot com [2013-05-28 09:01:11]
Note?that?array?value?buckets?are?reference-safe,?even?through?serialization.
$x='initial';$test=array('A'=>&$x,'B'=>&$x);$test=unserialize(serialize($test));$test['A']='changed';
echo$test['B'];//?Outputs?"changed"?>
This?can?be?useful?in?some?cases,?for?example?saving?RAM?within?complex?structures.
[#9]
gtisza at gmail dot com [2013-02-12 14:17:26]
Be?very?careful?when?using?a?result?as?an?array.?<?phpecho $a['foo']['bar']['baz']?>?will?throw?an?error?if?$a?is?an?object,?and?throw?a?warning?if?$a?is?an?array?but?does?not?have?the?right?keys,?but?it?will?silently?return?true?if?$a?is?null?or?boolean?or?int,?and?if?$a?is?a?string,?it?will?return?its?first?character.?(This?is?true?even?with?E_STRICT?set.)?This?can?be?a?major?gotcha?with?functions?which?return?null?or?false?if?they?are?unsuccessful.
[#10]
martijntje at martijnotto dot nl [2012-03-29 07:48:35]
Please?note?that?adding?the?magic?__toString()?method?to?your?objects?will?not?allow?you?to?seek?an?array?with?it,?it?still?throws?an?Illegal?Offset?warning.
The?solution?is?to?cast?it?to?a?string?first,?like?this
$array[(string)?$stringableObject]
[#11]
mlvljr [2011-05-20 06:37:27]
please?note?that?when?arrays?are?copied,?the?"reference?status"?of?their?members?is?preserved?(http://www.php.net/manual/en/language.references.whatdo.php).
[#12]
Walter Tross [2010-08-12 11:04:39]
It?is?true?that?"array?assignment?always?involves?value?copying",?but?the?copy?is?a?"lazy?copy".?This?means?that?the?data?of?the?two?variables?occupy?the?same?memory?as?long?as?no?array?element?changes.
E.g.,?if?you?have?to?pass?an?array?to?a?function?that?only?needs?to?read?it,?there?is?no?advantage?at?all?in?passing?it?by?reference.
[#13]
ken underscore yap atsign email dot com [2008-01-09 08:00:16]
"If?you?convert?a?NULL?value?to?an?array,?you?get?an?empty?array."
This?turns?out?to?be?a?useful?property.?Say?you?have?a?search?function?that?returns?an?array?of?values?on?success?or?NULL?if?nothing?found.
Now?you?want?to?merge?the?array?with?another?array.?What?do?we?do?if?$values?is?NULL??No?problem:
Voila.
[#14]
carl at linkleaf dot com [2007-09-06 11:36:40]
Its?worth?noting?that?there?does?not?appear?to?be?any?functional?limitations?on?the?length?or?content?of?string?indexes.?The?string?indexes?for?your?arrays?can?contain?any?characters,?including?new?line?characters,?and?can?be?of?any?length:
$key="XXXXX";$test=?array($key=>"test5");
for?($x=0;$x<500;$x++)?{$key.="X";$value="test".strlen($key);$test[$key]?=$value;
}
echo"
";print_r($test);
echo"
";?>Keep?in?mind?that?using?extremely?long?array?indexes?is?not?a?good?practice?and?could?cost?you?lots?of?extra?CPU?time.?However,?if?you?have?to?use?a?long?string?as?an?array?index?you?won't?have?to?worry?about?the?length?or?content.
[#15]
Spudley [2007-03-16 01:44:27]
On?array?recursion...
Given?the?following?code:
$myarray=?array('test',123);$myarray[]?=?&$myarray;print_r($myarray);?>
The?print_r()?will?display?*RECURSION*?when?it?gets?to?the?third?element?of?the?array.
There?doesn't?appear?to?be?any?other?way?to?scan?an?array?for?recursive?references,?so?if?you?need?to?check?for?them,?you'll?have?to?use?print_r()?with?its?second?parameter?to?capture?the?output?and?look?for?the?word?*RECURSION*.
It's?not?an?elegant?solution,?but?it's?the?only?one?I've?found,?so?I?hope?it?helps?someone.
[#16]
[2006-10-25 09:18:13]
This?page?should?include?details?about?how?associative?arrays?are?implemened?inside?PHP;?e.g.?using?hash-maps?or?b-trees.
This?has?important?implictions?on?the?permance?characteristics?of?associative?arrays?and?how?they?should?be?used;?e.g.?b-tree?are?slow?to?insert?but?handle?collisions?better?than?hashmaps.??Hashmaps?are?faster?if?there?are?no?collisions,?but?are?slower?to?retrieve?when?there?are?collisions.??These?factors?have?implictions?on?how?associative?arrays?should?be?used.
[#17]
ia [AT] zoznam [DOT] sk [2005-09-30 02:55:37]
Regarding?the?previous?comment,?beware?of?the?fact?that?reference?to?the?last?value?of?the?array?remains?stored?in?$value?after?the?foreach:
<?phpforeach ?($arras$key=>?&$value)
{$value=1;
}//?without?next?line?you?can?get?bad?results...
//unset(?$value?);$value=159;?>
Now?the?last?element?of?$arr?has?the?value?of?'159'.?If?we?remove?the?comment?in?the?unset()?line,?everything?works?as?expected?($arr?has?all?values?of?'1').
Bad?results?can?also?appear?in?nested?foreach?loops?(the?same?reason?as?above).
So?either?unset?$value?after?each?foreach?or?better?use?the?longer?form:
<?phpforeach ?($arras$key=>$value)
{$arr[$key]?=1;
}?>
[#18]
caifara aaaat im dooaat be [2005-08-28 02:28:08]
[Editor's?note:?You?can?achieve?what?you're?looking?for?by?referencing?$single,?rather?than?copying?it?by?value?in?your?foreach?statement.?See?http://php.net/foreach?for?more?details.]
Don't?know?if?this?is?known?or?not,?but?it?did?eat?some?of?my?time?and?maybe?it?won't?eat?your?time?now...
I?tried?to?add?something?to?a?multidimensional?array,?but?that?didn't?work?at?first,?look?at?the?code?below?to?see?what?I?mean:
$a1=?array("a"=>0,"b"=>1);$a2=?array("aa"=>00,"bb"=>11);$together=?array($a1,$a2);
foreach($togetheras$single)?{$single["c"]?=3;
}print_r($together);foreach($togetheras$key=>$value)?{$together[$key]["c"]?=3;
}print_r($together);?>
[#19]
jeff splat codedread splot com [2005-04-21 09:16:32]
Beware?that?if?you're?using?strings?as?indices?in?the?$_POST?array,?that?periods?are?transformed?into?underscores:
printf("POST:?");print_r($_POST);printf("
");?>
Once?you?click?on?the?button,?the?page?displays?the?following:
POST:?Array?(?[Windows3_1]?=>?Sux?)
[#20]
lars-phpcomments at ukmix dot net [2005-03-28 08:40:25]
Used?to?creating?arrays?like?this?in?Perl?
@array?=?("All",?"A".."Z");
Looks?like?we?need?the?range()?function?in?PHP:
$array=array_merge(array('All'),range('A','Z'));?>
You?don't?need?to?array_merge?if?it's?just?one?range:
$array=range('A','Z');?>
總結
以上是生活随笔為你收集整理的php recordarray,Array 数组 - [ php中文手册 ] - 在线原生手册 - php中文网的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java入门 慕路径,Java入门基础知
- 下一篇: 2022-2028年中国体育赛事产业深度