[] 'In içinde PHP Değişken

4 Cevap

Aşağıdaki yardım için teşekkürler, ancak hala bazı sorunlar yaşıyorum, ben denemek ve daha anlatacağım:

Ben aşağıdaki gibi bir değişken var:

$value['questions'];

Şimdi, bir döngü içinde bir kontrol yapmak gerekiyor, bu yüzden döngü içinde bu kod parçası var:

if($results[$value['questions']]==4){blah blah blah};

ama ben yaşıyorum sorun değeri $value['questions'] q1 ama ben kod $ sonuç bölümünde bir dize (i.e. inside quotes '') olması q1 gerek, bu yüzden yani $ sonuçları eleman Bu gibi görünmelidir ...

if($results['q1']==4){blah blah blah};

Şu anda bu gibi görünüyor

if($results[q1]==4){blah blah blah};

Mantıklı?

Tekrar teşekkürler yardım için.

Hi all,

Ben basit bir çözüm olduğunu umuyoruz!

Ben bir değişken var:

$results['q1'];

Bir değişken olarak 'q1' eleman, böyle bir şey olması mümkün mü:

$results['$i[question]'];

Google'da bir çözüm bulmak mümkün olmuştur ve PHP kılavuzları araştıran yok ...

Herkes bir öneri / çözüm var mı?

Teşekkürler,

Homer.

4 Cevap

Evet, it is possible to use a variable as indice, bir dizi elemanı erişirken.

Bir örnek olarak, bu kod bölümünü göz önünde bulundurun:

$results = array(
    'a' => 'sentence a', 
    'b' => 'hello !', 
);

$indice = 'a';
echo $results[$indice];

Aşağıdaki çıktıyı verecektir:

sentence a


Here, $indice is a quite simple variable, but you could have used whatever you wanted between the [ ve ], like, for instance :

  • fonksiyonlar: $results[ my_function($parameter) ]
  • array element : $result[ $my_array['indice'] ]
    • Hangi yapmak istediğiniz ne gibi görünüyor?
  • nesne özellik: $result[ $obj->data ]
  • ...

Temelde, sen orada ne istersen hemen hemen kullanabilirsiniz - sürece bir sayıl değer değerlendirir gibi (i.e. a single integer, string)


In your specific case, you could have $results declared a bit like this :

$results = array(
    'q1' => 'first question', 
    'q2' => 'second question', 
);

Ve $i bu şekilde ilan olurdu:

$i = array(
    'question' => 'q1'
);

$i['question'] 'q1' olacak ve aşağıdaki kod bölümünün o demektir ki:

echo $results[ $i['question'] ];

Bu çıkışını alabilirsiniz:

first question



Edit : size soru başlığında kullanılan belirli sözcükleri cevaplamak için, ayrıca PHP variable variable denen kullanabilirsiniz:

$variable = 'a';
$indice = 'variable';
echo $results[ $$indice ];

İşte:

  • $indice olan 'variable'
  • ve $$indice is 'a'
  • hangi daha önce olduğu gibi aynı çıktıyı alırsınız demektir


And, of course, don't forget to read the Arrays section of the PHP manual.

Why is $foo[bar] wrong? paragraf firt yayınlanmıştır örneği göz önünde bulundurularak, özellikle ilgi olabilir.



Edit after the edit of the OP :

$value['questions'] ise 'q1', o zaman, kod aşağıdaki iki bölümleri:

if($results[$value['questions']]==4){blah blah blah}

ve

if($results['q1']==4){blah blah blah}

should do exactly the same thing : with $results[$value['questions']], the $value['questions'] part will be evaluated (to 'q1') before the rest of the expression, ve that one will be the same as $results['q1'].


As an example, the following portion of code :

$results = array(
    'q1' => 4, 
    'q2' => 6, 
);
$value = array('questions' => 'q1');

if($results[$value['questions']]==4) {
    echo "4";
}

Çıkışlar 4.

Sen yapabilirsin:

$results[$i['question']];

Bu, ilk olarak dizi $i value key 'question' tekabül için erişir. Bu değer diziye anahtar olarak kullanılacaktır $results.

Your way: $results['$i[question]']; is actually trying to get the value corresponding to the key '$i[question]' in the array $results. Since the key is in single quote, variable interpolation does not happen and the entire string is treated literally.

Alternatif olarak yapabilirsiniz:

   $results["$i[question]"]; // using double quotes also works.

Örnek:

$arr1 = array('php' => 'zend', 'java' => 'oracle');
$arr2 = array('p' => 'php', 'j' => 'java');

echo $arr1[$arr2['p']]; // prints zend.
echo $arr1["$arr2[p]"]; // prints zend.
<?php

$result['q1'];

// is equivalent to

$index = 'q1';
$result[ $index ];

Eğer bunu istiyorum gibi Şimdi $index gibi karmaşık olabilir. Örneğin, böyle bir şey de mümkün:

$result[ 'q' . round( $someArray[$i] / 2 ) ];

CODE

<?php

$results = array(
    'q1' => 'This is a result called q1.',
    'q2' => 'So, can you access this?',
    '$i[question]' => 'This is a result called $i[question]',
);

$i = array(
    'question' => 'q2',
    'answer'   => 'Yes.'
);

echo '<pre>';
echo $results['q1'] . "\n";              // Using Array's is easy -
echo $results['$i[question]'] . "\n";    // all you gotta do is make sure
echo $results[$i['question']] . "\n";    // that you understand how
echo $i['answer'];                       // the keys work.

?>

OUTPUT

This is a result called q1.
This is a result called $i[question]
So, can you access this?
Yes.