i learn php and stuck in trouble with next code:
$people = new people();
$people->getByNumber($value[0]);
$peoples = array();
$count_people = 5;
for($i=1; $i<=$count_people; $i++)
{
$people->getByNumber($i);
$peoples[$i] = $people;
print_r($peoples[$i]);
}
print_r($peoples);
so after cycle "for" $peoples
dont show any information and print_r
tell, that array consist of empty values (but keys exists). While cycle work, inside it print_r
show me right pair key->value. How can i repair it to use right $peoples
after cycle?
upd: in for-loop by print_r($peoples[$i]);
i can see that:
people Object
(
[id] => 1
[age] => 19
[short_name] => Jhon
[full_name] => Jhon Persh
[image] =>
)
but after for-loop $peoples
contains only:
Array
(
[1] => people Object
(
[id] =>
[age] =>
[short_name] =>
[full_name] =>
[image] =>
)
[2] => people Object
(
[id] =>
[age] =>
[short_name] =>
[full_name] =>
[image] =>
)
[3] => people Object
(
[id] =>
[age] =>
[short_name] =>
[full_name] =>
[image] =>
)
[4] => people Object
(
[id] =>
[age] =>
[short_name] =>
[full_name] =>
[image] =>
)
[5] => people Object
(
[id] =>
[age] =>
[short_name] =>
[full_name] =>
[image] =>
)
)
upd2:
public function getByNumber($id)
{
$classProperty = get_object_vars($this);
foreach ($classProperty as $key=>$value)
$member[] = $key;
$data = //random data generation;
foreach ($member as $key =>$field)
{
$this->$field = $data[$field];
}
}
I found answer in construcnt new object of class $people1 = new People(), then just use $peoples[] = $people1 in loop after call method getByNumber($id); btw dont know why copy constructor dont call itself.
In the for-loop you are setting $peoples[$i]
to $people
, but there is no variable called $people
. You call $people->getByNumber($i)
but don't save it anywhere.
Your for-loop should look something like this:
$p = $people->getByNumber($id);
$peoples[$i] = $p;