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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying Multidimensional Array 1

Status
Not open for further replies.

Marine1969

IS-IT--Management
Mar 5, 2015
60
US
I have a recordset I need to create with data from the same structured table in 2 dbs. I have tried many different iterations of the below code. The page is finally not erroring out but also not showing the data to the screen. The page 500 errors when I

PHP:
echo $inv=>['db'];
And just prints "Array=" when
PHP:
echo $inv.'=>'['db'];

How do I access the values in this array?
PHP:
foreach($stpw as $row){
   $inv[] = array(['db']=>'w', ['id']=>$row['idinvoice'], ['rstop']=>$row['rstop']);
   
   echo $inv[0][0][0];
}
 
So much wrong there. Not sure where to start.

You should be getting a bunch or warnings with that code even if no errors show up.

Instead of going through what is wrong, I'll just show you the correct way of doing it.

Code:
foreach($stpw as $row){
   $inv = array('db'=>'w', 'id'=>$row['idinvoice'], 'rstop'=>$row['rstop']);
[indent][/indent]...   

}

That should correctly create your array. The "=>" is an assignment operator. You only use it when you assign a value. When outputting it should never be used.

You can verify your array by using the print_r() method. The echo method cannot directly print arrays, so only prints out "Array" as a way of telling you what you are trying to print is an array.

Code:
print_r($inv);

You can surround the output of print_r with <pre> html tags so it maintains the pre-formatted text from the array so its easier to read.
Code:
echo "<pre>";
print_r($inv);
echo "</pre>";


If you want to then select a particular item in the array, you can do so via the index.

For instance if I wanted to output the 'id' part of that array:

Code:
echo $inv['id'];

This should output whatever $row['idinvoice'] had as a value.

If I do
Code:
echo $inv['db'];

It would output the letter "w" as that what you assigned it to.

The main issue, you had, was your array was not being constructed correctly, so it could not be accessed.

You were also creating an array of arrays:
Code:
$inv[b][][/b] = array(...);
This makes $inv an array, and assigns another array to the the first available index in $inv.

To then access that array, you first need to go through the parent array.

$inv[0] would select the first index, and then you can select the inner item you want.

Code:
echo $inv[0]['id'];

//or

echo $inv['0']['rstop'];

//etc...




----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
That whole array of arrays I found in a few websites. It still does not make sense to me.

I put the code in for the array and tried "echo $inv['id']; " and nothing is displaying. I used count() and get 1 so the single record I have is in there.

Thank you for the assist!
 
Think of a box with things and other boxes inside.

Each box would be an array of things.

In your case $inv[id] would not exist if what you did was $inv[] = array(...); because you are creating an array inside another array. ie putting a box inside another box.

Doing: $inv[] is already making $inv an array. Then you get another box of items. i.e the array('db'=>'w'...) and place it inside the first box. $inv.

So to access the second box, you need to first open the first box, and select the inner box before you can see the items inside.

$inv is the outer nox. $inv[0] is the inner box. Then you can select an item from the inner box. $inv[0]['db']

If you had more inner boxes you could then do $inv[1] for the next box, and get the items inside that box. $inv[1]['db'], $inv[1]['rstop'] etc...








----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Got it, I understand the concept of the multidimensional arrays. So I am still just trying to view the data and used...
PHP:
$inv[] = array(['db']=>'w', ['id']=>$row['idinvoice'], ['rstop']=>$row['rstop']);
echo "Z" . $inv[0]['idinvoice'] . "Z<br>";

...and all I am getting is "ZZ". I checked the echo $row['idinvoice'] . "<br>"; and the data is there. Am I still displaying $inv[0]['idinvoice'] incorrectly?
 
Again, this is wrong:
Code:
$inv[] = array([b][COLOR=#CC0000][[/color][/b]'db'[b][COLOR=#CC0000]][/color][/b]=>'w', [b][COLOR=#CC0000][[/color][/b]'id'[b][COLOR=#CC0000]][/color][/b]=>$row['idinvoice'], [b][COLOR=#CC0000][[/color][/b]'rstop'[b][COLOR=#CC0000]][/color][/b]=>$row['rstop']);

This is what it should look like:
Code:
$inv[] = array('db'=>'w', 'id'=>$row['idinvoice'], 'rstop'=>$row['rstop']);

Note, that there are no square brackets around the index labels.

That is causing your array to be empty.

Again, you can use print_r() to output the entire array to verify its structure and contents.

Building the array correctly should then let you access it as:

Code:
echo "Z" . $inv[0]['idinvoice'] . "Z<br>";







----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
OMG...I do not know what happened there! The 12+ hour days are taking a toll. I just modified that and have it working. ALl the records are displaying now. I read that there are a number of ways to sort. What do you suggest?

Again, thank you for the help!
 
There's several ways to sort an array, but your sorting may best be done through your database at the query level, so that the results are already sorted when you print them out, instead of having them to sort in PHP which may be harder to do with multi-dimensional arrays.






----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
With a little trial and error I was able to get it to sort. There is not that many records. I wanted to say thanks again for the help!

-K
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top