How Can I loop with array? [closed]
When In print_r or var_dump
echo "<pre>";
echo var_dump($groupname);
echo "</pre>";
I got the result
array(2) {
[0]=>
object(stdClass)#330 (1) {
["name"]=>
string(3) "AAA"
}
[1]=>
object(stdClass)#332 (1) {
["name"]=>
string(3) "BBB"
}
}
Now I want to got the result from the array.
AAA | BBB
You might want to start out by looking on how arrays work. Also if you don't know "what" an array is, look over this Wikipedia entry.
Examples from PHP.NET
Declare an indexes and values
<?php
$arr = array("foo" => "bar", 12 => true);
echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>
Here is another example with the results
<?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);
?>
Result of the above:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
)
Array
(
[5] => 6
)
Array
(
[0] => 6
[1] => 7
)
Your special problem
Now it looks to me that you want to select each name from each record / row in your array / list.
To do this you need to loop / iterate throughout the array and write the current rows name-value.
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
So let's say that you know that your array only has two values, well then if you had an array looking like this:
$arr = array(10, 20);
You could write something like this to write them out:
echo $arr[0];
echo $arr[1];
Remember that when you tell your array which "row" to get, you always say "how many steps to go", meaning that if you want the first value, you take 0 steps, therefore the index is... 0!
Object orientation and arrays
Since you are using an object in your array you might want to check out some information about PHP and Object Oriented Programming.
Side note and a cool example
See this cool example which might give you an idea on what you can do
<?php
$obj = (object) array('foo' => 'bar', 'property' => 'value');
echo $obj->foo; // prints 'bar'
echo $obj->property; // prints 'value'
?>
This could be done easily with foreach.
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
If you don't know, just try stuff until it works.
echo $groupname[0]["name"]; //probably doesnt work, next
echo $groupname[0][0]; // hmm nope
echo $groupname[0]->name; // hey this works.
精彩评论