Answer the question
In order to leave comments, you need to log in
Why does PHP throw a strange error under the same logic conditions?
Good day.
The essence of the matter is very simple. There is code that does this:
<?php
$arr = array();
$arr[] = array('Коля', 'Дима', 'Гоша');
$arr[] = array('Катя', 'Света', 'Яна');
for($i = 0; $i < 10; $i++){
//unset($result);
foreach ($arr as $key => $value) {
$result[$i][$key]['names'] = $value;
}
$result = json_encode($result);
print_r($result);
}
<?php
$arr = array();
$arr[] = array('Коля', 'Дима', 'Гоша');
$arr[] = array('Катя', 'Света', 'Яна');
for($i = 0; $i < 10; $i++){
unset($result);
foreach ($arr as $key => $value) {
$result[$i][$key]['names'] = $value;
}
$result = json_encode($result);
print_r($result);
}
Answer the question
In order to leave comments, you need to log in
It's all about this line:
When the for loop goes to the second round - $result is no longer an array, and therefore it can not be accessed like this: $result[$i][$key]['names']
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question