2

I have an array of data like this . This array is the result of a database query , I want to get the index of rows and columns . I tried to get the index of each row but nonetheless failed. so, can anyone help me?

Query Result

array(
(int) 0 => array( 
            'B' => array(
                        'company' => 'ABC' 
            ), 
            'User' => array( 
                        'company' => 'abc' 
            ), 
            (int) 0 => array(
                            'date_part' => '3',
                            'jumlah' => null, 
                            'jumbuy' => '50990', 
                            'admin' => '50010' 
            ),
            (int) 1 => array(
                            'date_part' => '4',
                            'jumlah' => null, 
                            'jumbuy' => '98990', 
                            'admin' => '2010' 
            )
),
(int) 1 => array(
            'B' => array( 
                        'company' => 'BCD'
            ), 
            'User' => array( 
                            'company' => 'bcd' 
            ), 
            (int) 0 => array( 
                            'date_part' => '3',
                            'jumlah' => null, 
                            'jumbuy' => '65000', 
                            'admin' => '5000' 
            ),
            (int) 1 => array( 
                                'date_part' => '4',
                                'jumlah' => null, 
                                'jumbuy' => '9000', 
                                'admin' => '5000' 
            )
),
(int) 3 => array(
            'B' => array(
                        'company' => 'CDE'
            ), 
            'User' => array( 
                            'company' => 'cde' 
            ), 
            (int) 0 => array(
                            'date_part' => '4',
                            'jumlah' => null, 
                            'jumbuy' => '34566', 
                            'admin' => '2010' 
            )
)
);

Get Index

for ($row = 0; $row < count($array); $row++) {
    for($col = 0; $col < count(.....); $col++ ) {
       echo "Baris [row] kolom [colum]"; // output row and column
    }
}
2
  • 2
    You can use foreach loop. Commented May 7, 2015 at 6:57
  • @chandresh_cool's can you tell me? Commented May 7, 2015 at 6:59

3 Answers 3

3

The below code will give you all the indexes of this given array.

I checked the given array with the following code in my localhost.

And it gives us all the keys and the values in this array.

Try this

<?php
foreach($array as $arr=>$value ) 
{
  foreach($value as $ar=>$a)
  {
    echo $ar."<br>"; 
    foreach($a as $res =>$r)
    {
        echo $res.": ";
        echo $r;
        echo "<br>";
    }
  }
}
?>
Sign up to request clarification or add additional context in comments.

5 Comments

how about using the statement for
With for loop, It is hard to get the array keys that's why we use foreach loop instead of for.
As for as I know, Using for loop, you can get the key and values from single dimensional array. It is hard to get key and values of a multi-dimensional array using for loop. So we prefer foreach loop.
#gunaseelan I have seen the link , on the web for looping script for rows and columns have been known for rows and columns
#gunaseelan so if I use a script "for with count". the data will automatically show according to the amount of data
1

Use nested foreach for echoes keys:

foreach ($array as $row => $v) {
    foreach ($v as $col => $val) {
        echo 'row: ' . $row . ', col: ' . $col . '<br>';
    }
}

Comments

1

use the following

foreach ($values as $inde => $value) {
    foreach ($value as $key => $result) {
        echo '['. $inde.'] ---' .$key . '<br>'; 
    }
}

output will be

[0] ---B
[0] ---User
[0] ---0
[0] ---1
[1] ---B
[1] ---User
[1] ---0
[1] ---1
[3] ---B
[3] ---User
[3] ---0

1 Comment

#jagadeesh's how for value?

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.