Arushi
Arushi

Reputation: 137

get the value from the associtive array in table

Array
(
    [0] => Array
        (
            [datas] => Array
                (
                    [res] => 1
                    [rows] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [a] => 11
                                    [b] => 901
                                    [c] => 2
                                    [d] => 2
                                    [e] => A
                                    [f] => BT 
                                    [g] => arushi
                                    [h] => [email protected]
                                    [i] => 123445
                                    [j] => 12355.jpg
                                )

                        )

                )

        )

    [1] => Array
        (
            [datas] => Array
                (
                    [res] => 1
                    [rows] => stdClass Object
                        (
                            [person_name] => arushi
                        )

                )

        )

)

if i get the value in that kind off array how can i get the value of both partially with different variable m not able to understand the structure of the array.. need to get the value in table seperatly in different different table with same page how i can get the values

Upvotes: 1

Views: 92

Answers (2)

NASEEM FASAL
NASEEM FASAL

Reputation: 429

You can use foreach to loop through the arrays which have keys starting from 0 to n. So that you can foreach through the main array to get the datas array for all the keys. For getting rows childs you can use another foreach inside it to get each items. But for rows it is object , so you have to use -> to select the key. see below example code. But on your array the second array consist different formate in rows array. so make it like a common formate to loop through it easily. For eg :-

foeach($parent_array as $array){

   $data=$array['datas'];
   $res = $data['res'];
   $rows=$data['rows'];

   foreach($rows as $row){
      echo $row->a;  // to get the value of key a = 11
   }
}

Upvotes: 2

CAllen
CAllen

Reputation: 836

You would need to do a foreach loop. The only problem is that if you want to make it dynamic and grab the data by itself without you telling it what to get. If not it means you would have to know exactly what you need and the structure of your results. But seeing that there is a pattern you can do some checks until you reach rows. For example ($array would be the variable that has the data you provided):

foreach ($array AS $arr) {
    // To make you function/check work faster,
    // before even entering to fetch for data
    // you can check if it has any data, else you
    // probably will end up with an error
    if (isset ($arr ['datas']) && $arr ['datas']) {
        foreach ($arr ['datas'] AS $data) {
            if (isset ($arr ['res']) && 0 < $arr ['res']) {
                // here is where it gets tricky because
                // from you example its either an array or an object
                // but the kool part about it is that you can convert an object into an array
                // or just leave it as is because the foreach will take care of it either way :)

                // first check it it is an object and convert if yes
                $row    = $arr ['rows'];

                if (!is_array ($row))
                    $row = (array)$row; // converting it to array is super easy :)

                foreach ($row AS $key=>$dat) {
                    // from here your result can either be another array or just data
                    // so you run the check once more
                    if (is_array ($dat)) {
                        foreach ($dat AS $k=>$d) {
                            echo "data for $k is $d";
                        }
                    }
                    else
                        echo "data for $key is $dat";
                }
            }
        }
    }
}

Upvotes: 3

Related Questions