Multi dimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Multi dimensional array

Post 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);
}

?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Post Reply