Reputation: 6365
I get the error Fatal error: Cannot use [] for reading on line
at return $array[];
inside the function.
I'm using PHP Version 7.0.1 on Windows 10.
I'm trying to get the output as follows:
<i>Error Line One...</i><br>
<i>Error Line Two...</i><br>
, in json_encode
{"s":"Error Line One...<\/i>Error Line Two...<\/i>","success":false}
I previously used a different approach, but switched to this yesterday.
How can I get this work?
$errorCount = 1;
$errors[] = '*Error Line One...';
$errors[] = '*Error Line Two...';
$errors['success'] = ($errorCount == 0 ? True : False);
$errors[] = ajax($errors);
function ajax($array) {
$array = preg_replace('#\*(.+?)(?![^*])#','<i>$1</i><br>',$array);
return $array[];
}
json_encode($errors);
Below is how it was done previously, then changed to the above to avoid the .=
parts. This approach works. See Fiddle.
$errorCount = 1;
$errors['s'] = '*Error Line One...';
$errors['s'] .= '*Error Line Two...';
$errors['s'] = ajax($errors);
function ajax($array) {
$array = preg_replace('#\*(.+?)(?![^*])#','<i>$1</i>',$array);
return $array['s'];
}
$errors['success'] = ($errorCount == 0 ? True : False);
echo json_encode($errors);
Upvotes: 0
Views: 750
Reputation: 347
You are returning $array[] instead of $array,
Change this:
function ajax($array) {
$array = preg_replace('#\*(.+?)(?![^*])#','<i>$1</i><br>',$array);
return $array[];
}
into this:
function ajax($array) {
return preg_replace('#\*(.+?)(?![^*])#','<i>$1</i><br>',$array);
}
edit:
This fixes it I guess:
<?php
$errorCount = 1;
$errors[] = '*Error Line One...';
$errors[] = '*Error Line Two...';
$errors = ajax($errors);
$errors['success'] = ($errorCount == 0 ? True : False);
function ajax($array) {
foreach ($array as $key => $value)
$array[$key] = preg_replace('#\*(.+?)(?![^*])#','<i>$1</i><br>', $value);
return $array;
}
echo json_encode($errors);
?>
Upvotes: 1