0

This is a fun one.

I have a function that produces an array from mySQL...or better yet, it produces an array of arrays. Follow?

I've figured out how to drill down in to the array to display them into a working table. As so:

<table id="list_table" cellpadding="1" cellspacing="1">

<?php
    $array = $this->disparray;

    foreach($array as $key => $value)
        {
            echo '<tr>';
            foreach($value as $key => $value)
                {
                    echo '<td>' .  $value . '</td>';
                }
            echo '</tr>';
        }

?> 
</table>

HOWEVER, I only want to call specific <td>'s, which means, I have to call references to the specific column indexes. I've tried $value['1'], but it just does some crazy stuff. so, where I'm stuck is I do not know where to call the specific column indexes that I want.

4
  • 1
    Define "crazy stuff" and can you post what your array looks like. Commented Nov 29, 2012 at 21:06
  • what does crazy stuff mean?> Commented Nov 29, 2012 at 21:06
  • if I put $value['13'], it would display all of the columns in the array, but only values in col2 and 13, and the value wouldn't be right. It was weird. Commented Nov 29, 2012 at 21:12
  • This question lacks clarity and a minimal reproducible example. Commented Dec 25, 2024 at 21:01

2 Answers 2

1

You're nesting/overwriting your $key and $value variables. That is likely completely messing things up.

Try:

<?php
$array = $this->disparray;

foreach($array as $key => $value)
    {
        echo '<tr>';
        foreach($value as $k => $v)
            {
                echo '<td>' .  $v . '</td>';
            }
        echo '</tr>';
    }

?>

That might help you solve your issues.

Sign up to request clarification or add additional context in comments.

1 Comment

No, nothing is messed up, it displays the table as seen here: [link]mgoode.com/…, I just need to know how to specify which columns to use. (or which indexes/keys to display) But I did take you up on that advice.
0

You stated " I have to call references to the specific column indexes" and "I just need to know how to specify which columns to use. (or which indexes/keys to display)".

I suspect your issue is actually more involved than this, but the answer to that question is to use the standard syntax for multidimensional arrays. E.g., $array['index1']['index2'].

More information about the PHP array syntax is here. Please clarify your question if you need more information.

4 Comments

"I suspect your issue is actually more involved than this..." I'm a web-designer who somehow got mixed up in a web-development project. That's the real issue. :/
I have to do a foreach() for the first dimension and then pick specific columns out of the second dimension. And then, after that, I need to apply specific classes to the values in the second dimension. I'm just pulling my hair out trying to get the syntax right.
I was drilling down one dimension too deep. It was producing the specific character from whatever variable I put in. Fixed code is foreach($array as $key => $value) { echo '<td>' . $value['1'] . '</td>'; }
Glad you got it figured out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.