Undefined offset with an array - php

Why can't I use variable which contains a number to specify an array value:
$info(array)
$mySQLHeadings(array)
$infoString(empty String)
$mySQLHeadingString(empty string)
for ($i=0; $i<=count($info) ; $i++){
if($info[$i] != ""){
$mySQLHeadingString .= $mySQLHeadings[$i] . ",";
$infoString .= "'". $info[$i] ."',";
}
}
PHP says it's an undefined offset $i in the arrays. How can I correct it or do something similar. Thank you so much.

You should write for ($i = 0; $i < count($info); $i++). Array indexes start from 0 while count() starts from 1.
Also don't use count() inside for loop - move it before:
$count_info = count($info);
for ($i = 0; $i < $count_info; $i++)

If $info is numerically indexed, you can access elements with $i, but not further than max index !
count() gives you the array length, but max numeric index is (length - 1)
so :
for ($i=0; $i < count($info); $i++) {
//....
}

Related

For Loop Display Fact

I'm trying to figure out why I am getting a undefined offset of 1 for this for loop I'm writting. I have an array ($facts) that has specific key values pairs and I'm trying to see if on each iteration the $i matches one of the keys in the array. If the key isset and in the array I need to display the value of that key.
for ($i = 1; $i <= 100; $i++) {
if (isset($i) && in_array($i, $facts[$i])) {
echo $facts[$i];
}
echo $i;
}
UPDATE: Use the function isset to test if the incremented value equals one of the keys in the $facts array. If there is a key that matches, display the value after the number.
I think this is the correct way of checking (removing the in_array()).
for ($i = 1; $i <= 100; $i++) {
echo $i; // Now the number is first.
if (isset($facts[$i])) {
// This is only echoed if $i exists as a key.
echo $facts[$i];
}
}
If you only want to show the number if the fact exists, move echo $i inside the if-statement (or better yet, use foreach($facts as $key => $value) in that case).
You could check $facts[$i]
for ($i = 1; $i <= 100; $i++) {
if (isset($facts[$i]) && in_array($i, $facts[$i])) {
echo $facts[$i];
}
echo $i;
}

Object iteration using for loop

Object iteration with foreach is easy:
foreach ($item->attributes as $attribute) {
// echo $attribute->name;
}
.. but I wonder if its possible to do the same with for instead:
for ($j=0; $j < count($item->attributes); $j++) {
// echo $item->attributes->$j->$name ?
}
Although I can create a counter outside foreach and increment it, but just wanted to know if for works for objects.
For reference, the object(s) I'm working with looks like this.
for loops works for arrays which have incremented or decremented numeric index, and for associative arrays you have to use foreach unless you have separate arrays of keys, for example:
$count = count($keys);
for($i=0; $i < $count; $i++) {
echo $arr[$keys[$i]];
}
or you can reindex your assocative arrays, using array_values
$arr = array_values($assoc_array);
$count = count($arr);
for($i=0; $i < $count; $i++) {
echo $arr[$i];
}
for objects, they are properties, which can't be start from numbers, therefore you have to convert your object to array and reindex keys.
$arr = array_values(json_decode(json_encode($object), true));
$count = count($arr);
for($i=0; $i < $count; $i++) {
echo $arr[$i];
}
try to avoid above and use foreach instead.
for ($j=0; $j < count((array)$item->attributes); $j++) {
echo $item->attributes[$j];
}
Is this what you mean? You directly access this object that way, same as above, and you cast it beforehand to count.

Why does accessing an array element throw an array-as-string error in PHP?

I've done some searching and I didn't find any posts that quite answered my question.
I have a PHP array generated, for the sake of argument, with this code:
$i = 5;
for($i = 0; $i < $j; $i++) {
$multiArray[0][$i] = $i;
$multiArray[1][$i] = $i;
}
When I try to access it with:
for($i = 0; $i < $j; $i++) {
echo "$multiArray[0][$i]";
echo "$multiArray[1][$i]";
}
I get:
Notice: Array to string conversion on line 3
Notice: Array to string conversion on line 4
...x4
When I replace echo with printf("%d", $multiArray[0][$i]) then it prints fine. Why do I have to explicitly tell PHP that I'm asking for an int when the element I'm accessing is clearly an int (and PHP knows it, via var_dump())? I'm not accessing the array, but an element within the array.
Thanks
Simple double quoted variable interpolation supports up to one nested element. In other words, "foo[0][1]" is interpreted as "{$foo[0]}[1]". That means it tries to interpret the array $foo[0] as a string at that point to interpolate it into the string.
But using quotes here at all is entirely nonsensical. You don't want string interpolation, you just want to output a variable value:
echo $multiArray[0][$i];
just try this:
for($i = 0; $i < $j; $i++) {
echo $multiArray[0][$i];
echo $multiArray[1][$i];
}
Your code is parsing the string not the array, try to remove the quotes.The [] brackets after the array are considered as a string not the as a paremeter.Use the code below
<?php
$j = 5;
for($i = 0; $i < $j; $i++) {
$multiArray[0][$i] = $i;
$multiArray[1][$i] = $i;
}
for($i = 0; $i < $j; $i++) {
echo $multiArray[1][$i];
}
Hope this helps you

Making associative array

My goal is to make ant assoc array from the values of for loop.
//$from_time value is 6 and $to_time value is 23
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = $i;
}
echo json_encode($working_time_array);
The output I get on AJAX success, and when I console.log it, I get result as such :
["6",7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
Preferred result is
["6","7","8","9","10"]... etc
The only difference between the two results is one result set contains integers and the other contains strings. If you want those values to be strings just cast them when assigning them to the array:
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = (string) $i;
}
This really shouldn't be necessary unless your client side is expecting strings only.
You would need to cast $i to a string before pushing it to the array.
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = (string)$i;
}
why would you convert int to string?
for your goal this should work
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = "$i";
}
echo json_encode($working_time_array);

cannot put msSQL column names into array

I'm trying to put the column names of a msSQL table into an array. Using
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
echo mssql_field_name($result) . "<br><br>";
}
the column names print to the screen just fine. Also get_type() shows that they are strings. However, when I try to put them into an array like this:
$column_names = array();
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
$current_column = mssql_field_name($result);
array_push($column_names, $current_column);
}
var_dump($column_names); gives me an array (albeit the expected length) of boolean values. All false. I would expect to see an array containing the names of all my columns. What am I doing wrong here? Thank you
Looks like you are missing the $i argument on the mssql_field_name call. Try this maybe:
$column_names = array();
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
$column_names[] = mssql_field_name($result, $i);
}
http://php.net/manual/en/function.mssql-field-name.php

Categories