1

Hello i have a problem with the logic to have a multidimentional array for each while loops that i had. I do not know why is it not working.

i wanted to have like this as concept

data >
      ser_id > 14
               org_name > "org a"
      ser_id > 15
               org_name > "org b"

but the output is like this

Array
(
    [data] => Array
        (
            [ser_id0] => 14
            [0] => Array
                (
                    [0] => Gannon University
                )

            [ser_id1] => 15
            [1] => Array
                (
                    [0] => Lions Club
                )

            [ser_id2] => 16
            [2] => Array
                (
                    [0] => Rotatory Club
                )

        )

)

Can you help me with the logic. Here is the code i worked on with rows loop fetched from db.

        $rs = $this->crud->fetchResultSet("services");

        $rows = array();
        $i=0;
        while($row = $rs->fetch_assoc()){

            //$rows = arra
            $ser_id = $row["ser_id"];
            $rows["data"][$i] = $ser_id;
            $orgrs = $this->crud->fetchSingleResultSet("organizations","ser_id",$row['ser_id']);

            $j=0;
            while($innrow = $orgrs->fetch_assoc()){
                $rows["data"][$i][$j] = $innrow["org_name"];
                $j++;
            }
            $i++;

        }
6
  • 1
    Array keys CANNOT be identical, Commented Feb 23, 2016 at 8:00
  • thats why i have ser_id0,serid1 Commented Feb 23, 2016 at 8:05
  • use multidimensional array like this =>> $a[$i]['ser_id']=$row["ser_id"]; $a[$i]['org_name']=$innrow["org_name"]; Commented Feb 23, 2016 at 8:07
  • Could you clarify your example then? Because I'm not sure what kind of structure you wan then, it's hard to see what is key and what is value. Commented Feb 23, 2016 at 8:13
  • i think my logic should have been like what @mohade said Commented Feb 23, 2016 at 8:15

1 Answer 1

2

use multidimensional array like this =>>

 $a[$i]['ser_id']=$row["ser_id"]; 
$a[$i]['org_name']=$innrow["org_name"]

and restore data like this

$max=count($a[$i]);
for($i=0;$i<$max;$i++){
echo 'ser_id =>'.$a[$i]['ser_id'];
echo 'org_name =>'.$a[$i]['org_name'];
}
Sign up to request clarification or add additional context in comments.

Comments

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.