Viewed   225 times

I have a form that users fill out, and on the form there are multiple identical fields, like "project name", "project date", "catagory", etc. Based on how many forms a user is submitting, my goal is to:

  1. loop over the number of forms
  2. create individual SQL insert statements

However, PHP throws me a NOTICE that I don't seem to understand:


Notice: Uninitialized string offset: 1 ...dataPasser.php on line 90


$myQuery = array();

if ($varsCount != 0)
  for ($i=0; $i <= $varsCount; $i++)
    $var = "insert into projectData values ('" . $catagory[$i] . "', '" .  $task[$i] . "', '" . $fullText[$i] . "', '" . $dueDate[$i] . "', null, '" . $empId[$i] ."')";
    array_push($myQuery, $var);     

There are references to this issue I am having, but they are not exact and I am having trouble deducing where the actual problem stems from. I would greatly appreciate any help in understanding what is causing the array to not initialize properly.



This error would occur if any of the following variables were actually strings or null instead of arrays, in which case accessing them with an array syntax $var[$i] would be like trying to access a specific character in a string:


In short, everything in your insert query.

Perhaps the $catagory variable is misspelled?

Tuesday, October 18, 2022

Actually, this can be done. Through a php extension.

File: config.m4

PHP_ARG_ENABLE(test, whether to enable test Extension support, [ --enable-test   Enable test ext support])

if test "$PHP_TEST" = "yes"; then
  AC_DEFINE(HAVE_TEST, 1, [Enable TEST Extension])
  PHP_NEW_EXTENSION(test, test.c, $ext_shared)

File: php_test.h

#ifndef PHP_TEST_H
#define PHP_TEST_H 1

#define PHP_TEST_EXT_VERSION "1.0"
#define PHP_TEST_EXT_EXTNAME "test"


extern zend_module_entry test_module_entry;
#define phpext_test_ptr &test_module_entry


File: test.c

#include "config.h"

#include "php.h"
#include "php_test.h"

ZEND_BEGIN_ARG_INFO_EX(func_args, 1, 0, 0)

static function_entry test_functions[] = {
    PHP_FE(getaddress4, func_args)
    PHP_FE(getaddress, func_args)

zend_module_entry test_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
#if ZEND_MODULE_API_NO >= 20010901


    zval *var1;
    zval *var2;
    zval *var3;
    zval *var4;
    char r[500];
    if( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "aaaa", &var1, &var2, &var3, &var4) == FAILURE ) {
    sprintf(r, "n%p - %p - %p - %pn%p - %p - %p - %p", var1, var2, var3, var4, Z_ARRVAL_P(var1), Z_ARRVAL_P(var2), Z_ARRVAL_P(var3), Z_ARRVAL_P(var4) );
    RETURN_STRING(r, 1);

    zval *var;
    char r[100];
    if( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &var) == FAILURE ) {
    sprintf(r, "%p", Z_ARRVAL_P(var));
    RETURN_STRING(r, 1);

Then all you have to do is phpize it, config it, and make it. Add a "extension=/path/to/so/file/modules/" to your php.ini file. And finally, restart the web server, just in case.

  $x = array("123"=>"123");
  $w = $x;
  $y = $x;
  $z = &$x;

Returns(at least for me, your memory addresses will probably be different)

string '
0x9efeb0 - 0x9effe0 - 0x9ef8c0 - 0x9efeb0
0x9efee0 - 0x9f0010 - 0x9ed790 - 0x9efee0' (length=84)

string '0x9efee0' (length=8)

string '0x9f0010' (length=8)

string '0x9ed790' (length=8)

string '0x9efee0' (length=8)

Thanks to Artefacto for pointing this out, but my original code was passing the arrays by value, so thereby was recreating arrays including the referenced-one, and giving you bad memory values. I have since changed the code to force all params to be passed by reference. This will allow references, arrays, and object, to be passed in unmolested by the php engine. $w/$z are the same thing, but $w/$x/$y are not. The old code, actually showed the reference breakage and the fact that the memory addresses would change or match when all variables were passed in vs multiple calls to the same function. This was because PHP would reuse the same memory when doing multiple calls. Comparing the results of the original function would be useless. The new code should fix this problem.

FYI - I'm using php 5.3.2.

Sunday, September 4, 2022

You can try below code to merge array. Code generates desired output required to you. I have used sample array as given by you:

        "384"=>array("name"=>"SomeMovieName1","age"=>"12.2 hrs","IMDBLink"=>"","IMDBRating"=>"", "coverArt"=>""),
        "452"=>array("name"=>"SomeMovieName2","age"=>"15.2 hrs","IMDBLink"=>"","IMDBRating"=>"", "coverArt"=>""),
        "954"=>array("name"=>"SomeMovieName3","age"=>"4.2 hrs","IMDBLink"=>"","IMDBRating"=>"", "coverArt"=>"")
       "384" => array("IMDBLink" => "7.2", "IMDBRating" => "", "coverArt" => ""),
       "452" => array("IMDBLink" => "5","IMDBRating" => "", "coverArt" => ""),
       "954"=>array("IMDBLink" => "8","IMDBRating" => "", "coverArt" => "")
    $arr3 = array();
    foreach($arr1 as $key=>$val)
         $arr3[] = array_merge($val, $arr2[$key]);
    echo "<pre>";
Tuesday, September 13, 2022

$options is a string in this context, not an array. You are trying to treat it like an array, which doesn't work. The logic in your code will never work; it's not clear what you're trying to do with $options[$id].

The reason you get

Uninitialized string offset: 0

is that $id is not initialized anywhere, so PHP treats it as 0. You are saying, "look at this string ($options) and get the array value at key 0," and PHP throws its hands up because that doesn't make sense.

Your various solutions won't work for several other reasons. stripslashes( isset( $options[$id] ); fails because (1) $options[$id] doesn't exist and (2) stripslashes() takes a string parameter, but you are handing it a boolean value. Same problem with esc_attr( isset( $options[$id] ) );

Monday, December 26, 2022

You are occasionally overrunning $characters because of strlen($characters), which should be strlen($characters) - 1. Your random range should begin with zero, and you need to end with the length minus one. If the length of $characters is 10, then the last zero-based array index is nine, and that becomes your upper bound for random selection.

$characters = "0123456789abcdef";
for ($p = 0; $p < $length ; $p++) {
    $string .= $characters[mt_rand(0, strlen($characters) - 1)]; 

Because you are choosing randomly, you wouldn't always receive the notice -- it would only occur if mt_rand() returned its maximum value at some point in your loop.

Wednesday, August 24, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :