multidimensional associatve array problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pauld

    #1

    multidimensional associatve array problem

    from a MySQL DB i want to get a multidimensiona l array that i can loop
    through

    either key =field name value = array of ENUM options
    or array[x][0] =field name, array[x][1]= ENUM options and increment x
    inside loop


    $q=mysql_query( "SHOW FIELDS FROM table" ) or die ("Query failed");

    while ($row = mysql_fetch_arr ay($q))
    {
    //from http://uk.php.net/mysql_fetch_field

    echo 'field is '.$row['Field'] . ' type is ' . $row['Type'];
    if (ereg('enum.(.* ).', $row['Field'], $match))
    {
    $opts = explode(',', $match[1]);
    foreach ($opts as $item)
    {$finalResult[] = substr($item, 1, strlen($item)-2);}
    }

    $array=array ($row['Field'] =>$finalResult[]);

    }
    gives Fatal error :Cannot use [] for reading in .......

    a similar error message when i try a multidimentiona l array
    array[$x][0]=$row['Field']
    array[$x][1]=$finalResult[]


    anyone know the answer ( or a better way of doing it )

  • Dave

    #2
    Re: multidimensiona l associatve array problem

    pauld (pdc124@yahoo.c o.uk) decided we needed to hear...[color=blue]
    > from a MySQL DB i want to get a multidimensiona l array that i can loop
    > through
    >
    > either key =field name value = array of ENUM options
    > or array[x][0] =field name, array[x][1]= ENUM options and increment x
    > inside loop
    >[/color]
    <snip failing code>[color=blue]
    >[/color]
    Try the following instead (altered from your code)...

    $q = mysql_query("sh ow fields from thetable");
    $arr = array();
    while ($row = mysql_fetch_arr ay($q)) {
    echo 'field is ' . $row[0] . ' type is ' . $row[1] . '<br>';
    $finalResult = array();
    if (preg_match('/enum\((.*?)\)/', $row[1], $match)) {
    $opts = explode(',', $match[1]);
    foreach ($opts as $item) {
    array_push($fin alResult, substr($item, 1, strlen($item)-2));
    }
    }
    $arr[$row[0]] = $finalResult;
    }
    print_r($arr);

    --
    Dave <dave@REMOVEbun dook.com>
    (Remove REMOVE for email address)

    Comment

    Working...