I have array element like this,
$array = array(rha3pf, rha3pf, adfsasdf, asdfsad);
I want to make a unique, for that i used,
$uniqe = array_unique($array);
print_r($uniqe);
I want result as,
$array = array(rha3pf, adfsasdf, asdfsad);
I am getting result as,
E_NOTICE : type 8 -- Use of undefined constant rha3pf - assumed 'rha3pf' -- at line 3
E_NOTICE : type 8 -- Use of undefined constant rha3pf - assumed 'rha3pf' -- at line 3
E_NOTICE : type 8 -- Use of undefined constant adfsasdf - assumed 'adfsasdf' -- at line 3
E_NOTICE : type 8 -- Use of undefined constant asdfsad - assumed 'asdfsad' -- at line 3
Array ( [0] => rha3pf [2] => adfsasdf [3] => asdfsad )
I am not getting correct answer in live. How to solve this
Thanks in advances
The following code
$array = array('rha3pf', 'rha3pf', 'adfsasdf', 'asdfsad');
$a = implode(" ",$array);
$a = array_unique($a);
print_r($a);
will print
rha3pf adfsasdf asdfsad
What is $a in print??
<?php
$array = array(rha3pf, rha3pf, adfsasdf, asdfsad);
$uniqe = array_unique($array);
print_r($uniqe);
//^^^ this part
?>
Working demo here
Related
getting the notice error for array how to resolve this unintialized string and undefined offset.
for($i=0;$i<count($exp_company);$i++)
{
$exp_fd2 = explode("/",$exp_fd1 [$i]);
$exp_td2 = explode("/",$exp_td1 [$i]);
$exp_fd = $exp_fd2[2]."-".$exp_fd2[1]."-".$exp_fd2[0];
$exp_td = $exp_td2[2]."-".$exp_td2[1]."-".$exp_td2[0];
mysql_query("insert into empexp(employee_id,company_name,designation,from_date,to_date,exp_description)values('$insert_id','$exp_company[$i]','$exp_designation[$i]','$exp_fd','$exp_td','$exp_description[$i]')");
}
Here You are Trying to Access the index which does not exist.
undefined offset means that an array has run out of bounds as simple as that.
let's assume your array is:
[0]==> 'a',
[1]==> 'b',
From above I cannot access array[2] as it does not exist.
the array size is smaller than the index that you are trying to fetch.
I'm trying to use create_function in order to find out how many times a certain value occurs in a particular array.
while (!empty($rollcounts)){
// Take the first element of $rollcounts
$freq = count(array_filter($rollcounts,create_function("$a","return $a == $rollcounts[0]")));// Count how many times the first element of $rollcounts occurs in the list.
$freqs[$rollcounts[0]] = $freq; // Add the count to the $frequencies list with associated number of rolls
for($i=0;$i<count($rollcounts);$i++){ // Remove all the instances of that element in $rollcounts
if(rollcounts[$i] == $rollcounts[0]){
unset($rollcounts[$i]);
}
}
} // redo until $rollcounts is empty
I get a "Notice" message complaining about the $a in create_function(). I'm surprised, because I thought $a was simply a parameter. Is create_function() not supported in my version of php? phpversion() returns 5.6.30 and I'm using XAMPP. The error message:
Notice: Undefined variable: a in /Applications/XAMPP/xamppfiles/htdocs/learningphp/myfirstfile.php on line 34
So, if I'm reading your question correctly, I think you want to count the occurrences of each element in the array? If so just use array_count_values e.g. [1, 1, 2, 2, 3] -> [1 => 2, 2 => 2, 3 => 1]
$freqs = array_count_values($rollcounts);
This way you can skip your while loop.
You should use something like ...
$freq = count(array_filter($rollcounts,function($a) {return $a == $rollcounts[0];}));
Have a read of http://php.net/manual/en/functions.anonymous.php which explains a bit more about them.
I am receiving the following error in PHP
Notice undefined offset 1: in C:\wamp\www\includes\imdbgrabber.php line 36
Here is the PHP code that causes it:
<?php
# ...
function get_match($regex, $content)
{
preg_match($regex,$content,$matches);
return $matches[1]; // ERROR HAPPENS HERE
}
What does the error mean?
If preg_match did not find a match, $matches is an empty array. So you should check if preg_match found an match before accessing $matches[0], for example:
function get_match($regex,$content)
{
if (preg_match($regex,$content,$matches)) {
return $matches[0];
} else {
return null;
}
}
How to reproduce this error in PHP:
Create an empty array and ask for the value given a key like this:
php> $foobar = array();
php> echo gettype($foobar);
array
php> echo $foobar[0];
PHP Notice: Undefined offset: 0 in
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(578) :
eval()'d code on line 1
What happened?
You asked an array to give you the value given a key that it does not contain. It will give you the value NULL then put the above error in the errorlog.
It looked for your key in the array, and found undefined.
How to make the error not happen?
Ask if the key exists first before you go asking for its value.
php> echo array_key_exists(0, $foobar) == false;
1
If the key exists, then get the value, if it doesn't exist, no need to query for its value.
Undefined offset error in PHP is Like 'ArrayIndexOutOfBoundException' in Java.
example:
<?php
$arr=array('Hello','world');//(0=>Hello,1=>world)
echo $arr[2];
?>
error: Undefined offset 2
It means you're referring to an array key that doesn't exist. "Offset"
refers to the integer key of a numeric array, and "index" refers to the
string key of an associative array.
Undefined offset means there's an empty array key for example:
$a = array('Felix','Jon','Java');
// This will result in an "Undefined offset" because the size of the array
// is three (3), thus, 0,1,2 without 3
echo $a[3];
You can solve the problem using a loop (while):
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
// Increase count by 1, thus, $i=1
$i++;
$groupname[$i] = base64_decode(base64_decode($row['groupname']));
// Set the first position of the array to null or empty
$groupname[0] = "";
}
I am getting this PHP error:
PHP Notice: Undefined offset: 1
Here is the PHP code that throws it:
$file_handle = fopen($path."/Summary/data.txt","r"); //open text file
$data = array(); // create new array map
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle); // read in each line
$parts = array_map('trim', explode(':', $line_of_text, 2));
// separates line_of_text by ':' trim strings for extra space
$data[$parts[0]] = $parts[1];
// map the resulting parts into array
//$results('NAME_BEFORE_:') = VALUE_AFTER_:
}
What does this error mean? What causes this error?
Change
$data[$parts[0]] = $parts[1];
to
if ( ! isset($parts[1])) {
$parts[1] = null;
}
$data[$parts[0]] = $parts[1];
or simply:
$data[$parts[0]] = isset($parts[1]) ? $parts[1] : null;
Not every line of your file has a colon in it and therefore explode on it returns an array of size 1.
According to php.net possible return values from explode:
Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter.
If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.
How to reproduce the above error in PHP:
php> $yarr = array(3 => 'c', 4 => 'd');
php> echo $yarr[4];
d
php> echo $yarr[1];
PHP Notice: Undefined offset: 1 in
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(578) :
eval()'d code on line 1
What does that error message mean?
It means the php compiler looked for the key 1 and ran the hash against it and didn't find any value associated with it then said Undefined offset: 1
How do I make that error go away?
Ask the array if the key exists before returning its value like this:
php> echo array_key_exists(1, $yarr);
php> echo array_key_exists(4, $yarr);
1
If the array does not contain your key, don't ask for its value. Although this solution makes double-work for your program to "check if it's there" and then "go get it".
Alternative solution that's faster:
If getting a missing key is an exceptional circumstance caused by an error, it's faster to just get the value (as in echo $yarr[1];), and catch that offset error and handle it like this: https://stackoverflow.com/a/5373824/445131
Update in 2020 in Php7:
there is a better way to do this using the Null coalescing operator by just doing the following:
$data[$parts[0]] = $parts[1] ?? null;
This is a "PHP Notice", so you could in theory ignore it. Change php.ini:
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
To
error_reporting = E_ALL & ~E_NOTICE
This show all errors, except for notices.
my quickest solution was to minus 1 to the length of the array as
$len = count($data);
for($i=1; $i<=$len-1;$i++){
echo $data[$i];
}
my offset was always the last value if the count was 140 then it will say offset 140 but after using the minus 1 everything was fine
The ideal solution would be as below. You won't miss the values from 0 to n.
$len=count($data);
for($i=0;$i<$len;$i++)
echo $data[$i]. "<br>";
In your code:
$parts = array_map('trim', explode(':', $line_of_text, 2));
You have ":" as separator. If you use another separator in file, then you will get an "Undefined offset: 1" but not "Undefined offset: 0" All information will be in $parts[0] but no information in $parts[1] or [2] etc. Try to echo $part[0]; echo $part[1]; you will see the information.
I just recently had this issue and I didn't even believe it was my mistype:
Array("Semester has been set as active!", true)
Array("Failed to set semester as active!". false)
And actually it was! I just accidentally typed "." rather than ","...
The output of the error, is because you call an index of the Array that does not exist, for example
$arr = Array(1,2,3);
echo $arr[3];
// Error PHP Notice: Undefined offset: 1 pointer 3 does not exist, the array only has 3 elements but starts at 0 to 2, not 3!
I am receiving the following error in PHP
Notice undefined offset 1: in C:\wamp\www\includes\imdbgrabber.php line 36
Here is the PHP code that causes it:
<?php
# ...
function get_match($regex, $content)
{
preg_match($regex,$content,$matches);
return $matches[1]; // ERROR HAPPENS HERE
}
What does the error mean?
If preg_match did not find a match, $matches is an empty array. So you should check if preg_match found an match before accessing $matches[0], for example:
function get_match($regex,$content)
{
if (preg_match($regex,$content,$matches)) {
return $matches[0];
} else {
return null;
}
}
How to reproduce this error in PHP:
Create an empty array and ask for the value given a key like this:
php> $foobar = array();
php> echo gettype($foobar);
array
php> echo $foobar[0];
PHP Notice: Undefined offset: 0 in
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(578) :
eval()'d code on line 1
What happened?
You asked an array to give you the value given a key that it does not contain. It will give you the value NULL then put the above error in the errorlog.
It looked for your key in the array, and found undefined.
How to make the error not happen?
Ask if the key exists first before you go asking for its value.
php> echo array_key_exists(0, $foobar) == false;
1
If the key exists, then get the value, if it doesn't exist, no need to query for its value.
Undefined offset error in PHP is Like 'ArrayIndexOutOfBoundException' in Java.
example:
<?php
$arr=array('Hello','world');//(0=>Hello,1=>world)
echo $arr[2];
?>
error: Undefined offset 2
It means you're referring to an array key that doesn't exist. "Offset"
refers to the integer key of a numeric array, and "index" refers to the
string key of an associative array.
Undefined offset means there's an empty array key for example:
$a = array('Felix','Jon','Java');
// This will result in an "Undefined offset" because the size of the array
// is three (3), thus, 0,1,2 without 3
echo $a[3];
You can solve the problem using a loop (while):
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
// Increase count by 1, thus, $i=1
$i++;
$groupname[$i] = base64_decode(base64_decode($row['groupname']));
// Set the first position of the array to null or empty
$groupname[0] = "";
}