Nasıl dizisi ile ben döngü miyim?

3 Cevap

Ne zaman print_r veya var_dump olarak

echo "<pre>";
echo var_dump($groupname);
echo "</pre>";

Ben sonuç var

array(2) {
  [0]=>
  object(stdClass)#330 (1) {
    ["name"]=>
    string(3) "AAA"
  }
  [1]=>
  object(stdClass)#332 (1) {
    ["name"]=>
    string(3) "BBB"
  }
}

Şimdi ben diziden sonuç var istiyorum.

AAA | BBB

3 Cevap

Sen how arrays work bakarak işe başlamak isteyebilirsiniz. Eğer bilmiyorsanız da bir dizidir "ne", bakmak this Wikipedia entry.

Examples from PHP.NET

Bir dizin ve değerleri beyan

<?php
$arr = array("foo" => "bar", 12 => true);

echo $arr["foo"]; // bar
echo $arr[12];    // 1
?>

Burada sonuçlardan bir örneğidir

<?php
// Create a simple array.
$array = array(1, 2, 3, 4, 5);
print_r($array);

// Now delete every item, but leave the array itself intact:
foreach ($array as $i => $value) {
    unset($array[$i]);
}
print_r($array);

// Append an item (note that the new key is 5, instead of 0).
$array[] = 6;
print_r($array);

// Re-index:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>

Yukarıdaki sonucu:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
Array
(
)
Array
(
    [5] => 6
)
Array
(
    [0] => 6
    [1] => 7
)

Your special problem

Şimdi size dizi / listedeki her kayıt / satırın her isim seçmek istiyorum bana görünüyor.

Bunu yapmak için, dizi boyunca döngü / iterate ve geçerli satırlar isim-değer yazmak gerekir.

Example on looping with arrays

<?php
$people = Array(
        Array('name' => 'Kalle', 'salt' => 856412),
        Array('name' => 'Pierre', 'salt' => 215863)
        );

for($i = 0; $i < sizeof($people); ++$i)
{
    echo $people[$i]['name'];
}
?>

Doing this without loops

Yani sizin dizisi yalnızca iki değere sahip olduğunu biliyorum diyelim, iyi o zaman bu gibi bakarak bir dizi olsaydı:

$arr = array(10, 20);

Onları yazmak için böyle bir şey yazabilirsiniz:

echo $arr[0];
echo $arr[1];

Hangi "satır" almak için dizi söylemek, her zaman ilk değer istiyorsanız, bu nedenle endeksi ..., 0, 0 adımlar, yani "gitmek için kaç adım" demek olduğunu unutmayın!

Object orientation and arrays

Eğer dizideki bir nesne kullanarak beri PHP and Object Oriented Programming hakkında bazı bilgileri kontrol etmek isteyebilirsiniz.

Side note and a cool example

Size ne yapabilirim bir fikir verebilir ki bu serin örneğe bakın

<?php

$obj = (object) array('foo' => 'bar', 'property' => 'value');

echo $obj->foo; // prints 'bar'
echo $obj->property; // prints 'value'

?>

Bu foreach ile kolayca yapılabilir.

This $groupname var looks like an an array of objects. Lets have a mock class that will create objects for our purpose.

class MyClass
{
 public $name = '';

 public function __construct($n)
 {
  $this->name = $n;
 }
}

This class has the name attribute/member in it. The $groupname is an array as of such objects (from the var_dump)... We could create that array like this:

$groupnames = Array(
        new MyClass ('AAA'), new MyClass ('BBB')
   );

To print out the information in that array we can use the foreach constrcut of php.

foreach($groupname as $group)
{
    print "$group->name\n";
}

This prints the names each on a line. To have this loop output "AAA | BBB" is left as an exercise:) Hint: You can use the "end()" construct like this "end($group)"...

Regards, Riza Dindir

Eğer bilmiyorsanız çalıştığını kadar, sadece şeyler deneyin.

echo $groupname[0]["name"]; //probably doesnt work, next
echo $groupname[0][0]; // hmm nope
echo $groupname[0]->name; // hey this works.