Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with multidimensional array 1

Status
Not open for further replies.

admoore

IS-IT--Management
May 17, 2002
224
US
I need to access specific data in one array while looping through another...

I can print_r the array I need to reference; but, somehow I cannot retrieve the data I am looking for...

Example:
the array $data[]
Code:
Array
(
    [0] => Array
        (
            [salesman] => 818
            [0] => DAVID QUINTERO
            [salesman_name] => DAVID QUINTERO
            [1] => 1
            [quan] => 1
        )

    [1] => Array
        (
            [salesman] => 909
            [0] => RAY WHISMAN
            [salesman_name] => RAY WHISMAN
            [1] => 1
            [quan] => 1
        )

    [2] => Array
        (
            [salesman] => 954
            [0] => LEO DIAZ
            [salesman_name] => LEO DIAZ
            [1] => 1
            [quan] => 1
        ).... etc

Can't be referenced
Code:
$data[0][quan]
to return a value of 1... What am I doing wrong here??
 
What happens when you try? Do you get an error?

At first glance, the lack of quotes is likely generating a notice of undefined constant or something similar.

Try: $data[0]['quan']

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
No value is returned...

i.e.
Code:
echo '$data[0][quan]: '.$data[0]['quan']; exit;
comes back empty whether quoted or not...
 
Hmmm, without seeing the code in context and as such what else may be intervening its hard to say what is happening.

Recreating the array as you present it, yields the expected result:

Code:
$data = array(0 => array('salesman' => 818,0 => 'DAVID QUINTERO','salesman_name' => 'DAVID QUINTERO',1=> 1,'quan' => 1),1 => array('salesman' => 909,0 => 'RAY WHISMAN','salesman_name' => 'RAY WHISMAN',1 => 1,'quan' => 1),2 => array('salesman' => 954,0 => 'LEO DIAZ','salesman_name' => 'LEO DIAZ',1 => 1,'quan' => 1));


[code]
echo "data[0][quan] ==============> " . $data[0]['quan'];

output said:
data[0][quan] ==============> 1

Sounds like you are retrieving this from a DB query, Is this correct?


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Also what happens if you try to get a different key. Say $data[0][0]? or $data[0]['salesman']?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Other keys fail to return valid result as well...
Array IS created from MySQL query as follows:

Code:
WHILE( $row = mysql_fetch_array($result)) { 
                                            // make array
                                            $id = array_shift($row); // Shifts first element
                                            $data[$id] = $row; }

Seems like it should be fine; but..?
 
O.k, so you remove the ID from the array returned by mysql, and then assign the remainder of the array to $data[$id]. Is this ID the salesman number in the array above?

Is there an ID of 0 (zero) in the returned results?

What happens if you print $data after one iteration of the loop? What does the array look like?

Code:
WHILE( $row = mysql_fetch_array($result)) { 
                                            // make array
                                            $id = array_shift($row); // Shifts first element
                                            $data[$id] = $row; 
                                            [COLOR=#A40000]print_r($data); die();[/color]
}




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
What happens if you print $data after one iteration of the loop? What does the array look like?
That seems to be the clue I needed!

Should be able to resolve from here... Thank you.

Code:
Array
(
    [818] => Array
        (
            [salesman] => 818
            [0] => DAVID QUINTERO
            [salesman_name] => DAVID QUINTERO
            [1] => 
            [quan] => 
        )

)
 
There you go. Glad I could help.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top