×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Displaying Multidimensional Array

Displaying Multidimensional Array

Displaying Multidimensional Array

(OP)
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

CODE --> php

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

CODE --> php

echo $inv.'=>'['db']; 

How do I access the values in this array?

CODE --> php

foreach($stpw as $row){
   $inv[] = array(['db']=>'w', ['id']=>$row['idinvoice'], ['rstop']=>$row['rstop']);
   
   echo $inv[0][0][0];
} 

RE: Displaying Multidimensional Array

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']);
... }

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[] = 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... 

https://www.w3schools.com/php/php_arrays.asp



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

RE: Displaying Multidimensional Array

(OP)
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!

RE: Displaying Multidimensional Array

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

RE: Displaying Multidimensional Array

(OP)
Got it, I understand the concept of the multidimensional arrays. So I am still just trying to view the data and used...

CODE --> 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?

RE: Displaying Multidimensional Array

Again, this is wrong:

CODE

$inv[] = array(['db']=>'w', ['id']=>$row['idinvoice'], ['rstop']=>$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

RE: Displaying Multidimensional Array

(OP)
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!

RE: Displaying Multidimensional Array

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

RE: Displaying Multidimensional Array

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close