Undefined offset error and undefined variable error even if defined - php

I have a function that I am writing to take in values datetime values from an array and convert it into hours and minutes format like 0010. I want to use this to later extract other values from the array as this is an associative array
function getData($lengthArray, $csvdat) { # passing down the values of the array and the length of the array
$newData = array(
'nightTime1',
'nightTime2',
'dayTime1',
'peakTime',
'dayTime2',
'winterNightTime1',
'winterNightTime2',
'winterDayTime1',
'winterDayTime2',
'winterPeakTime');
for ($count = 0; $count <= $lengthArray; $count++ ) { #looping to the length of the array
$arrayTimestamp = $csvdat[$count]['timestamp'];
$comparator = date('Hs', strtotime($arrayTimestamp)); #extracting the time format
$nightTime1 = 0759;
$dayTime1 = 1659;
$peakTime = 1859;
$dayTime2 = 2259;
$nightTime2 = 2359;
if ( $comparator == $nightTime1 ) {
if (empty($newData['nightTime1'])) {
$newData['nightTime1'] = $nightTime;
$newData['nightTime1']['phase1'] = $csvdat[$count++]['day_chan1'];
$newData['nightTime1']['phase1'] = $csvdat[$count++]['day_chan2'];
$newData['nightTime1']['phase1'] = $csvdat[$count++]['day_chan3'];
}
else if (!empty($newData)) {
}
}
}
return $newData;
}
getData($lengthData , $csvdata);
var_dump($newData);
The problem I am facing now is that I get an Undefined offset error at the line
$arrayTimestamp = $csvdat[$count]['timestamp'];
and an undefined variable for $newData. I am not very good at php so please advise.
Here is the var_dump of the csvdat array
array (size=118061)
0 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5972' (length=4)
'curr_property_cost' => string '62' (length=2)
'day_property' => string '19' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2898' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '9' (length=1)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2189' (length=4)
'curr_chan2' => string '2898' (length=4)
'curr_chan3' => string '885' (length=3)
'day_chan1' => string '7' (length=1)
'day_chan2' => string '9' (length=1)
'day_chan3' => string '2' (length=1)
1 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5215' (length=4)
'curr_property_cost' => string '54' (length=2)
'day_property' => string '37' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2141' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '16' (length=2)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2173' (length=4)
'curr_chan2' => string '2141' (length=4)
'curr_chan3' => string '901' (length=3)
'day_chan1' => string '14' (length=2)
'day_chan2' => string '16' (length=2)
'day_chan3' => string '5' (length=1)
2 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5215' (length=4)
'curr_property_cost' => string '54' (length=2)
'day_property' => string '54' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2157' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '23' (length=2)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2157' (length=4)
'curr_chan2' => string '2157' (length=4)
'curr_chan3' => string '901' (length=3)
'day_chan1' => string '21' (length=2)
'day_chan2' => string '23' (length=2)
'day_chan3' => string '8' (length=1)
3 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5183' (length=4)
'curr_property_cost' => string '54' (length=2)
'day_property' => string '71' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2125' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '31' (length=2)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2173' (length=4)
'curr_chan2' => string '2125' (length=4)
'curr_chan3' => string '885' (length=3)
'day_chan1' => string '28' (length=2)
'day_chan2' => string '31' (length=2)
'day_chan3' => string '11' (length=2)

Try
for ($count = 0; $count < $lengthArray; $count++ )
instead of
for ($count = 0; $count <= $lengthArray; $count++ )

If and only if you are 100% sure that $lengthArray is the size of the $csvdat array, then your error is caused by this line:
for ($count = 0; $count <= $lengthArray; $count++ ) {
it should be
for ($count = 0; $count < $lengthArray; $count++ ) {
Right now you're breaking the loop if the $count is lower OR the same as $lengthArray. Notice that last key in your array is 3 and its length is 4.
EDIT
Now about Undefined variable: $newData.
You're trying to var_dump($newData) which is undefined. It exists inside the getData function but not outside it. Maybe you wanted to do it like this:
$newData = getData($lengthData , $csvdata);
var_dump($newData);

It should be $count < $lengthArray in for loop
for ($count = 0; $count < $lengthArray; $count++ ) {
}

Related

Find and get array key from multilevel array from other array

i have this array structure, and i want to find and compare with other array that i have.
This is the array that i want search:
array (size=7)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
1 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
2 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
3 =>
array (size=9)
0 => string 'Verde' (length=5)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
An this is the array with search values:
Array
(
[0] => Blanco
[1] => 32GB
[2] => Plastico
)
I have the key and value, but i need find, in this example the main key is 1 in the long and main array, how can i get that?
PD: the number of search values are the numbers inside main arrays
Let's say that elements is the name of the array that you want to iterate over and find the index whose first three values match the search values. You can simply iterate over the elements, checking if the values match and if they do, then you can store the index in a variable called $wantedKey:
$target = array(
'0' => Blanco
'1' => 32GB
'2' => Plastico
);
$wantedKey = null;
foreach($elements as $key => $elem){
if($elem[0] == $target[0] && $elem[1] == $target[1] && $elem[2] == $target[2]){
$wantedKey = $key;
break;
}
}
echo $wantedKey;
In case you have an arbitrary amount of values, you can use a foreach to iterate over them and check if they match:
foreach($elements as $key => $elem){
$sameValues = true;
foreach($target as $t_key => $t_value){
if($elem[$t_key] != $t_value){
$sameValues = false;
break;
}
}
if($sameValues){
$wantedKey = $key;
break;
}
}
echo $wantedKey;

Getting array value by string key

I have array and i nees access to key:
var_dump($load);
array (size=18)
'4' =>
array (size=3)
0 => string '12.3%' (length=5)
1 => string '17.8%' (length=5)
2 => string '11.8%' (length=5)
'9' =>
array (size=3)
0 => string '5.0%' (length=4)
1 => string '8.2%' (length=4)
2 => string '8.9%' (length=4)
'24' =>
array (size=3)
0 => string '53.1%' (length=5)
1 => string '86.1%' (length=5)
2 => string '53.6%' (length=5)
'7' =>
array (size=3)
0 => string '95.1%' (length=5)
1 => string '90.1%' (length=5)
2 => string '80.7%' (length=5)
Hove a can get data on key?
var_dump($load["7"]);
var_dump($load["'7'"]);
var_dump($load['"7"']);
var_dump($load[7]);
All var_dump return null;
Var dump array keys, Keys match.
$keys=array_keys($load);
var_dump($keys);
array (size=18)
0 => string '14' (length=2)
1 => string '60' (length=2)
2 => string '19' (length=2)
3 => string '7' (length=1)
4 => string '15' (length=2)
5 => string '24' (length=2)
6 => string '1' (length=1)
7 => string '16' (length=2)
8 => string '5' (length=1)
9 => string '11' (length=2)
10 => string '17' (length=2)
11 => string '18' (length=2)
12 => string '3' (length=1)
13 => string '6' (length=1)
14 => string '2' (length=1)
15 => string '10' (length=2)
16 => string '9' (length=1)
17 => string '4' (length=1)

Disallowed Key Characters in Codeigniter 2

I'm getting this error while submitting my form. Have read similar questions but couldn't find my problem.
I just disabled '_clean_input_keys' function temporarily and printed the $_POST to see what's wrong. but could not see anything weird.
Where should I search for this disallowed character?
This is my $_POST:
array (size=67)
'Naam' => string '1' (length=1)
'NaamKhanevadegi' => string '1' (length=1)
'NaamPedar' => string '1' (length=1)
'Shenasname' => string '1' (length=1)
'CodeMelli' => string '1111111222' (length=10)
'TarikhTavalodDay' => string '10' (length=2)
'TarikhTavalodMonth' => string '10' (length=2)
'TarikhTavalodYear' => string '1361' (length=4)
'Jensiat' => string '1' (length=1)
'MahalTavalod' => string '1' (length=1)
'Khedmat' => string '1' (length=1)
'SerialShenasname' => string '222222' (length=6)
'Taahol' => string '1' (length=1)
'SarparastHastam' => string '1' (length=1)
'TedadeTakafol' => string '2' (length=1)
'Ghad' => string '2' (length=1)
'Jesmani' => string '1' (length=1)
'Vazn' => string '2' (length=1)
'SabegheBimari' => string '1' (length=1)
'SabegheBime' => string '1' (length=1)
'Bimegar' => string '1' (length=1)
'BimeShode' => string '1' (length=1)
'ShomareBime' => string '2' (length=1)
'BimeDay' => string '28' (length=2)
'BimeMonth' => string '8' (length=1)
'BimeYear' => string '8' (length=1)
'ShoruMostamariDay' => string '8' (length=1)
'ShoruMostamariMonth' => string '8' (length=1)
'ShoruMostamariYear' => string '1360' (length=4)
'BimeBikari' => string '1' (length=1)
'PayanMostamariDay' => string '8' (length=1)
'PayanMostamariMonth' => string '8' (length=1)
'PayanMostamariYear' => string '1360' (length=4)
'Isargari' => string '1' (length=1)
'NameHemayati' => string '1' (length=1)
'PostalCode' => string '1212121212' (length=10)
'Tel' => string '' (length=0)
'Mobile' => string '09090909090' (length=11)
'Email' => string '' (length=0)
'Address' => string '' (length=0)
'MarjaNaam' => string '' (length=0)
'MarjaTel' => string '0909090909' (length=10)
'MarjaMobile' => string '09090909090' (length=11)
'Savad' => string '5' (length=1)
'Reshte' =>
array (size=5)
0 => string '0' (length=1)
1 => string '0' (length=1)
2 => string '0' (length=1)
3 => string '0' (length=1)
4 => string '0' (length=1)
'Moadel' =>
array (size=5)
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '' (length=0)
4 => string '' (length=0)
'NoeDaneshgah' =>
array (size=4)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string '1' (length=1)
3 => string '1' (length=1)
'Mokaleme' =>
array (size=1)
''.$i.'' => string '1' (length=1)
'Neveshtan' =>
array (size=1)
''.$i.'' => string '1' (length=1)
'ZabanTasalot' =>
array (size=1)
''.$i.'' => string '0' (length=1)
'NoeMaharat' =>
array (size=1)
0 => string '' (length=0)
'DarajeMaharat' =>
array (size=1)
0 => string '' (length=0)
'Gavahiname' =>
array (size=3)
0 => string '1' (length=1)
1 => string '10' (length=2)
2 => string '11' (length=2)
'NaameMoasese' =>
array (size=1)
0 => string '' (length=0)
'SabegheJobTitle' =>
array (size=1)
0 => string '0' (length=1)
'SabegheKar' =>
array (size=1)
0 => string '' (length=0)
'DarkhastiJobTitle' =>
array (size=1)
0 => string '0' (length=1)
'NoeEstekhdam' => string '1' (length=1)
'ShahreDarkhasti1' => string '' (length=0)
'ShahreDarkhasti2' => string '' (length=0)
'ShahreDarkhasti3' => string '' (length=0)
'KhodEshteghali' => string '2' (length=1)
'Kharej' => string '2' (length=1)
'Keshvar1' => string '' (length=0)
'Keshvar2' => string '' (length=0)
'Comment' => string '' (length=0)
'Tamas' => string '1' (length=1)
I found it!
That little $ in $i made the problem.
I forgot some php tag there.
In most of the cases when you have a existing software and you are trying to deploy in a new enviroment this kind of error should be caused by the PHP property
short_open_tag
Check if you have enabled in your new enviroment. In other words PHP couldn't read the tags in your code.

php display multilevel treenode menu recursive

I have a post regarding this which was answered though I now have a different problem related to the same topic. You may want to reference here for the original question. php display multilevel treenode menu
Now that the array is displaying by tree-node, it seems that it is not fully recursive. I notice that when the first element is a child node, it is not displaying as a child.
I tried doing a krsort then ksort.
function getChildren(&$rows, $p = 0) {
$r = array();
krsort($rows);
foreach($rows as $row_id => $row) {
if ($row['parent_node']==$p) {
$r[$row['product_category_code']] = getChildren($rows, $row['product_category_code']);
unset($rows[$row_id]);
}
}
ksort($rows);
return $r;
}
Here's the structure of the array:
array
0 =>
array
'product_category_code' => string 'akamia' (length=6)
'product_category_desc' => string 'Akamia' (length=6)
'parent_node' => string 'summer-dress' (length=12)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-04-01 10:03:42' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-04-01 10:03:47' (length=19)
1 =>
array
'product_category_code' => string 'bracelets' (length=9)
'product_category_desc' => string 'Bracelets' (length=9)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:04:08' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-14 22:09:05' (length=19)
2 =>
array
'product_category_code' => string 'floral-dress' (length=12)
'product_category_desc' => string 'Floral Dress' (length=12)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:49' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-04-01 10:03:30' (length=19)
3 =>
array
'product_category_code' => string 'flowery-bracelets' (length=17)
'product_category_desc' => string 'Flowery Bracelets' (length=17)
'parent_node' => string 'bracelets' (length=9)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:16' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-30 19:08:44' (length=19)
4 =>
array
'product_category_code' => string 'small-flowery-bracelets' (length=23)
'product_category_desc' => string 'Small Flowery Bracelets' (length=23)
'parent_node' => string 'flowery-bracelets' (length=17)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:08:35' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-30 19:09:44' (length=19)
5 =>
array
'product_category_code' => string 'summer-dress' (length=12)
'product_category_desc' => string 'Summer Dress' (length=12)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:29' (length=19)
'modified_by' => string '0' (length=1)
'modified_date' => null
The output (without the krsort / ksort) is showing this, which is wrong:
Akamai
Bracelets
Flowery Bracelets
Small Flowery Bracelets
Floral Dress
Summer Dress
This should be the output:
Bracelets
Flowery Bracelets
Small Flowery Bracelets
Floral Dress
Summer Dress
Akamai
Never mind, I got the answer. The $p on function getChildren(&$rows, $p = 0) should be $p = '' instead of a zero (0) since the parent node on the array is empty. Silly me. XD

php display multilevel treenode menu

How do I display this structure into a multi-level menu?
Here's the structure:
array
0 =>
array
'product_category_code' => string 'bracelets' (length=9)
'product_category_desc' => string 'Bracelets' (length=9)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:04:08' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-14 22:09:05' (length=19)
1 =>
array
'product_category_code' => string 'floral-dress' (length=12)
'product_category_desc' => string 'Floral Dress' (length=12)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:49' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-30 19:06:58' (length=19)
2 =>
array
'product_category_code' => string 'flowery-bracelets' (length=17)
'product_category_desc' => string 'Flowery Bracelets' (length=17)
'parent_node' => string 'bracelets' (length=9)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:16' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-30 19:08:44' (length=19)
3 =>
array
'product_category_code' => string 'small-flowery-bracelets' (length=23)
'product_category_desc' => string 'Small Flowery Bracelets' (length=23)
'parent_node' => string 'flowery-bracelets' (length=17)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:08:35' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-30 19:09:44' (length=19)
4 =>
array
'product_category_code' => string 'summer-dress' (length=12)
'product_category_desc' => string 'Summer Dress' (length=12)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:29' (length=19)
'modified_by' => string '0' (length=1)
'modified_date' => null
And output should be like this:
Bracelets
Flowery Bracelets
Small Flowery Bracelets
Floral Dress
Summer Dress
Here is what I did but it still display the child nodes a
function getChildren($rows, $p = 0) {
$r = array();
foreach($rows as $row) {
if ($row['parent_node']==$p) {
var_dump($p);
$r[$row['product_category_code']] = getChildren($rows, $row['product_category_code']);
}
}
return $r;
Thanks!
It's because you still have the categories in the array when you already assigned them. What you can do is to do the function where you pass the argument as a reference, and the in the foreach loop to have the ability to clear the array from that already assigned category. Simple implementation below.
function getChildren(&$rows, $p = 0) {
$r = array();
foreach($rows as $row_id => $row) {
if ($row['parent_node']==$p) {
$r[$row['product_category_code']] = getChildren($rows, $row['product_category_code']);
unset($rows[$row_id]);
}
}
return $r;
}

Categories