Parse error: syntax error, unexpected 'unset' (T_UNSET) - php

I am using simple php unset() function to remove and index of array but it's showing the following error:
Parse error: syntax error, unexpected 'unset' (T_UNSET)
Here is my erroneous code:
echo $totalArray = unset($linkExtHelp[0]);
Thanks in advance.

Try this, Reason for unset($linkExtHelp[0]) assigning to the variable echo $totalArray =
You can't assign the unset() value to the variable, You can use to check before unset and after unset as like below. In other words, unset does not have any return value, since unset is a void. Void - does not provide a result value to its caller.
Syntax: void unset ( mixed $var [, mixed $... ] )
echo "Before unset: ".$linkExtHelp[0];
unset($linkExtHelp[0]);
$linkExtHelp = array_values($linkExtHelp);
echo "After unset: ".$linkExtHelp[0];
instead of
echo $totalArray = unset($linkExtHelp[0]);

unset does not return a value - it cannot be used meaningfully as an expression, even if such a production was accepted.
However, the parse error is caused because unset is a keyword and a "special production": even though unset looks like a function, it is not a function1. As such, unset is only valid as a statement2 per the language grammar.
The production can be found in zend_language_parser.y:
309 unticked_statement:
| ..
338 | T_UNSET '(' unset_variables ')' ';'
1 The syntax is due to historical design choices, and arguably a mistake from a consistency viewpoint:
Note: Because [unset] is a language construct and not a function, it cannot be called using variable functions.
2 There is also "(unset) casting", but I'm ignoring that here.

Unset does not return anything, it is a keyword.
I assume you want to re-index the array, you can use array_values to do this
Try this:
unset($linkExtHelp[0]);
$totalArray = array_values($linkExtHelp);

You can't assign "unset". this is how I do.
you need to make a temp array.
$totalArray = $linkExtHelp; // assign it to a new array (so you can keep the original one )
foreach ($totalArray as $key => $value) {
unset($totalArray [$key]['save_day']); // if you need to remove all of 'save_day' value from multi array
}
var_dump($totalArray); // after unset
var_dump($linkExtHelp); // the original array
//echo $totalArray = unset($linkExtHelp[0]); // This is wrong. no echo
I hope it helps.

Related

A character 1 after every value I get from the database using CodeIgniter [duplicate]

My print_r($view) function yields:
View Object
(
[viewArray:View:private] => Array
(
[title] => Projet JDelage
)
)
1 <--------------
What does the "1" at the end mean? The PHP manual isn't very clear on how to parse the output of print_r.
You probably have echo print_r($view). Remove the echo construct. And... what need do you have to parse its output? There are certainly much better ways to solve your problem.
print_r called with one argument (or with its second argument set to false), will echo the representation of its parameter to stdout. If it does this, it returns TRUE. Thus if you echo print_r($foo) you will print the contents of foo, followed by a string representation of the return value (which is 1).
When using print_r to return, than than output/print the value, pass the 2nd parameter which defines return as true.
echo print_r($view, true);
This is useful if you want to save the results to a variable or concatenate with another string.
$var = 'The array is: ' . print_r($view, true);

PHP - Array fatal error

I've got an odd error in my PHP code regarding dynamic arrays.
The error outputted is:
Fatal error: Cannot use string offset as an array ... on line 89
This is a portion of my code, it is within a foreach loop, which is looping through settings in a database:
foreach($query->fetchAll() as $row)
{
if($site!=CURRENT_SITE_TEMPLATE)
{
$property = 'foreignSettings';
$propertyType = 'foreignSettingsTypes';
} else {
$property = 'settings';
$propertyType = 'settingTypes';
}
$this->$property[$row['variable_section']][$row['variable_name']] = $row['variable_value'];
settype($this->$property[$row['variable_section']][$row['variable_name']],$row['variable_type']);
$this->$propertyType[$row['variable_section']][$row['variable_name']] = $row['variable_type'];
}
For the sake of the example code, $site is 'admin' and CURRENT_SITE_TEMPLATE is 'admin'.
In addition, $foreignSettings, $foreignSettingsTypes, $settings, and $settingTypes are all defined as arrays in the class scope
The error is on line 89, which is:
$this->$property[$row['variable_section']][$row['variable_name']] = $row['variable_value'];
I originally thought it was because of the $property variable accesing the array, however, this looks like valid legal code in the PHP documentation ( http://php.net/manual/en/language.variables.variable.php in example #1)
Any help on this error would be appreciated.
Thanks
In your given example $property is a string. You are then trying to use that as an array. Strings only has numeric indexes (if you need to use as an array).
The problem is as follows: $this->$property[0] means you access the 0th place of $property which in your case would be the first letter of the string $property. Thus you end up with $this->f or $this->s.
with $this->$property[0][0] you would be trying to access the 0th place of the 0th place of the $property string what results in an error because you try to access the 0th place of the char s what is not possible since the char s can not be referenced as an array.
what you want is $this->{$propperty}[0][0] what means that you try to access the 0th place of the 0th place of the variable that has the name $propperty.

Undefined index, isset and references in PHP

If accessing an undefined index of a null reference, PHP does not throw any errors.
<?php
$array = &$foo['bar'];
if ($array['stuff']) echo 'Cool'; // No PHP notice
$array['thing'] = 1; // Array created; $foo['bar']['thing'] == 1
$array['stuff']; // PHP notice
If $array wasn't a reference PHP would have complained on the first line.
Why doesn't it for references? Do I need bother with isset for null references, or is PHP complaining internally and not letting me know?
In your code $array is null. The following code will not give you a notice either:
$b = null;
if ($b['stuff']) echo 'cool';
This is strange, this comment in the documentation points to that fact.
You must raise your error reporting level. Your example $array['stuff'] will throw warnings about index not found. I often combine a test for key in with the evaluation so as to prevent those warnings:
if( array_key_exists("blah",$arr) && strlen($arr['blah']) > 0 ) {
; // do stuff here
}
I often combine variables in with array names because anytime I have to cut-n-paste copy code to the next section to do the same-ish thing, I'd rather make an array of variable names and then iterate through the variable names. The most absurd condition is when I have billing and shipping data to manipulate, where I'll have an array variable name $BorS or just $BS and then at the top, set $BorS="shipping"; and end up with really interesting statements like:
${$BorS."data"}[${$BorS."_addr1"}]=$input_array[$BorS."_address_line_1"];
Why not just do:
$array = array();

php is_array() test on $value[] - not sure why it won't register as an array

I am just doing some experimenting. How do I print the value of this $chvalues[] or test (as below) it's an array. Is it possible to print this out as-is? $chvalues[] (yes my input field is properly defined with name=seminar etc...)
$chvalues = array();
if (isset($_POST['seminar'])) {
foreach ($_POST['seminar'] as $ch => $value) {
$chvalues[] = $value;
if(is_array('$chvalues[]')) {
echo "Yes. It's an Array.";
}
}
}
I am trying to test this:
if(is_array('$chvalues[]')) { ...
I get this:
Fatal error: Cannot use [] for reading in...
You are reading the array as a string by using single quotes around it (which should just return false).
You should use if(is_array($chvalues)) without the brackets.
$chvalues will always be an array, because you just assigned it to one the line before, as well as at the start of your code, before the loop.
Maybe you meant to check $value, or run the is_array() after the loop and not initialize $chvalues as an array before running the loop.
Following a variable by [] is only for appending new elements to the end of the array within the variable. If you want to look at the array then just use the variable itself.
if(is_array($chvalues)) {
...
You only use $value[] for accessing the "top" element of the $value array, the unset next position, if you will.
Change it to $chvalues in the is_array() function.
To print an array, it depends on output. Check print_r for debugging.
Also don't use quotes as they make the array act like a string, in this case (because they are single quotes) the string '$chvalues[]' is being tested for being an array.
Two problems here. First, everytime you do $chvalues[] with empty brackets, you are appending a new empty element onto your array. So when you do:
if(is_array($chvalues[]))
You always get an empty element, never an array.
Instead you probably want:
if(is_array($chvalues))
Second problem is you cannot single-quote '$chvalues[]' as you have done. It should have no quotes as in my first code block above.
don't use $chvalues[], just $chvalues (without single quotes inside the is_array function).
$chvalues = array();
if (isset($_POST['seminar'])) {
foreach ($_POST['seminar'] as $ch => $value) {
$chvalues = $value;
if(is_array($chvalues)) {
echo "Yes. It's an Array.";
}
}
}
You really want, is_array($chvalues) (which will always be true in your code) or, if you're trying to find out if the last item added to $chvalues is an array, you should call is_array($value). If you don't have that option (not sure why you wouldn't), then you can use is_array(end($chvalues)) to test the last thing added to the array. I will warn, though, that you will need to call reset to iterate through $chvalues with foreach.
Of course, you could also call $arr = array_keys( $chvalues ); is_array($chvalues[end($arr)]);, but that gets very silly (especially since I left out the last paren originally).

What does "1" mean at the end of a php print_r statement?

My print_r($view) function yields:
View Object
(
[viewArray:View:private] => Array
(
[title] => Projet JDelage
)
)
1 <--------------
What does the "1" at the end mean? The PHP manual isn't very clear on how to parse the output of print_r.
You probably have echo print_r($view). Remove the echo construct. And... what need do you have to parse its output? There are certainly much better ways to solve your problem.
print_r called with one argument (or with its second argument set to false), will echo the representation of its parameter to stdout. If it does this, it returns TRUE. Thus if you echo print_r($foo) you will print the contents of foo, followed by a string representation of the return value (which is 1).
When using print_r to return, than than output/print the value, pass the 2nd parameter which defines return as true.
echo print_r($view, true);
This is useful if you want to save the results to a variable or concatenate with another string.
$var = 'The array is: ' . print_r($view, true);

Categories