2011-05-11 11 views
16

ऑटोविविफिकेशन के लिए PHP.net खोजना कोई परिणाम नहीं देता है। लिखने के समय, Wikipedia का दावा है कि केवल पर्ल ही है। "Php autovivification" के लिए Google खोजते समय कोई स्पष्ट निश्चित परिणाम नहीं हैं।क्या PHP में ऑटोविविफिकेशन है?

इस PHP कोड ठीक चलाता है:

$test['a'][4][6]['b'] = "hello world"; 
var_dump($test); 

array 
    'a' => 
    array 
     4 => 
     array 
      'b' => 
      array 
       ... 

किसी को भी एक विहित जवाब (अधिमानतः संदर्भ के साथ) प्रदान कर सकते हैं पीएचपी इस सुविधा है कि, और इस तरह संस्करण यह, विचित्रता, शॉर्टकट में पेश किया गया था के रूप में किसी भी जानकारी आदि?

+5

कि जब आप मान निर्दिष्ट कर रहे हैं काम करेंगे। PHP आपके लिए वह पथ बनाएगा। यह तभी काम करता है जब आप मान निर्दिष्ट कर रहे हों। – JohnP

+0

@ जोहानपी मोती के लिए विकिपीडिया लिंक पर उदाहरण में मामला लगता है। – Till

+0

@ अब तक कोई विचार नहीं कि वह मोती कोड क्या कर रहा था। यह मेरे लिए सभी वूडू है :) – JohnP

उत्तर

10

हाँ, पीएचपी autovivification है (और एक लंबे समय के लिए यह किया गया है), हालांकि यह है कि शब्दावली द्वारा संदर्भित नहीं है। PHP.net कहता है:

An existing array can be modified by explicitly setting values in it.

This is done by assigning values to the array, specifying the key in brackets. The key can also be omitted, resulting in an empty pair of brackets ([]).

$arr[key] = value; 
$arr[] = value; 
// key may be an integer or string 
// value may be any value of any type 

If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array.

हालांकि, प्रलेखन कहा गया है कि अगर आप एक को सेट किए बिना सरणी (या कुंजी) का मूल्य तक पहुँचने का प्रयास, यह एक त्रुटि वापस आ जाएगी:

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.

मैं नीचे नज़र रखी है मेरी वर्ष PHP3 पुस्तिका है, जो इस में कहा गया है:

You can also create an array by simply adding values to the array.

$a[] = "hello"; 
+1

तो सुविधा मौजूद है लेकिन PHP.net इसका नाम नहीं है, है ना? –

+2

हां, मैंने इसे वापस PHP3 पर भी ढूंढ लिया है। इसे आसानी से "सरणी बनाने का वैकल्पिक तरीका" के रूप में संदर्भित किया जाता है। Perl के साथ तुलना के लिए एक अच्छा उदाहरण दिखाने के लिए – Kelly

3

PHP 100% नहीं है यदि PHP ऑटोविविफिकेशन का समर्थन करता है लेकिन आपके द्वारा पोस्ट किए गए वाक्यविन्यास अधिकांश भाग के लिए काम करता है।

// Works as you have assigned a value of 'hello' 
$a['a'][4][6]['b'] = "hello"; 
var_dump($a); 
echo print_r($a,true); 

// works as you have assigned a value of 'world' 
$b[][][][] = "world"; 
var_dump($b); 
echo print_r($b,true); 

// ERROR: Cannot use [] for reading 
$c[]; 
var_dump($c); 
echo print_r($c,true); 

पढ़ने के लिए [] का उपयोग नहीं कर सकता: Related Link

3

पर्ल में, आइटम निरीक्षण पर autovivify होगा, असाइनमेंट आवश्यक नहीं है। अंतर्निहित अनुरोध कुंजी पर जाने के लिए आवश्यक वस्तुओं का मार्ग निरीक्षण पर बनाया जाएगा। ध्यान दें कि {d => undef} प्रविष्टि वास्तव में बनाई गई नहीं है लेकिन इसका निहित है।

use strict; 
    use warnings; 
    use Data::Dumper; 

    my %a;  # as is empty, equivalent to \%a is {}; 
    print Dumper %a; 
    $a{b}{c}{d}; # \%a is now { b => { c => {}}} 
       # returns an undef value. 
    print Dumper \%a; 

उत्पादन:

$VAR1 = {}; 
$VAR1 = { 
      'b' => { 
        'c' => {} 
       } 
     }; 

पर्ल सरणी उदाहरण:

use strict; 
use warnings; 
use Data::Dumper; 

my (@b,@c);   # @b=(), @c=() 
print Dumper \@b; 
$b[3];    # @b=() aka unchanged. 
print Dumper \@b; 
$b[3][2][1];   # @b=(undef,undef,undef,[undef,undef,[]]) 
print Dumper \@b; 
print Dumper \@c; 
$c[3]=1 ;   # @c=(undef,undef,undef,1) 
print Dumper \@c; 

आउटपुट:

Useless use of array element in void context at -e line 7. 
Useless use of array element in void context at -e line 9. 
$VAR1 = []; 
$VAR1 = []; 
$VAR1 = [ 
      undef, 
      undef, 
      undef, 
      [ 
      undef, 
      undef, 
      [] 
      ] 
     ]; 
$VAR1 = []; 
$VAR1 = [ 
      undef, 
      undef, 
      undef, 
      1 
     ]; 
+0

+1 – dmaestro12

संबंधित मुद्दे