Page 1 of 1

Multi dimensional array

Posted: Thu Feb 27, 2003 7:21 am
by Johnm
Hi all,
I am trying to add elements to a multi dimensional array but get a syntax error on the line with array_push()... can anyone help?

John M

Here is the problem code:

Code: Select all

<?php
$x=0;
$gauge_array = array();
foreach($material_array as $target)
{
	    
    $sql="select gauge,mat_name
              from gauge_info 
              where mat_name ='".$target."'
              and loc_code='00'";
     $qry=ifx_query($sql,$dbid);
      while( $row=ifx_fetch_row($qry,"NEXT"))
      {
          array_push($gauge_array,$row['mat_name']=>$row['gauge']);
          echo $gauge_array[$x]."<br>\n";
          $x++;
       }
       ifx_free_result($qry);
}

?>

Posted: Thu Feb 27, 2003 10:54 am
by volka
int array_push ( array array, mixed var [, mixed ...]) appends an element to an existing array using the next free numerical index.
Please explain what you want to store in which way and why.

Posted: Thu Feb 27, 2003 1:30 pm
by Johnm
I need to store the elements retirived in the query in one array.
So

$gauge_array[$row['mat_name']=$row['gauge']);

For each returned row from the querry.


John m

Posted: Thu Feb 27, 2003 2:01 pm
by volka
almost

Code: Select all

$gauge_array[$row['mat_name']]=$row['gauge'];
is the value of each $row['mat_name'] unique for the whole select?
If not and you want to store all the values (that would be a multidim-array) you can do something like

Code: Select all

if (isset($gauge_array[$row['mat_name']])
    $gauge_array[$row['mat_name']][]=$row['gauge'];
else
    $gauge_array[$row['mat_name']]= array($row['gauge']);

Code: Select all

$gauge_array[$row['mat_name']][]=$row['gauge'];
is equal to

Code: Select all

array_push($gauge_array[$row['mat_name']], $row['gauge']);
so I guess you want the latter