Welcome Guest, Not a member yet? Register   Sign In
array walk in an easy way for displaying view? [Solved]
#1

[eluser]notebook[/eluser]
In my view i want to display multidimesional array data decently , i want total control over it so that i can show data in a table the way i want.
i have these 39 arrays in $mydata in the view. I'll give you an idea with var_dump($mydata)

Code:
array(39)
{ [0]=> array(3) { [0]=> string(18) "Unit Name 1" [1]=> string(9) "Category Name 1" [2]=> string(20) "Course Name 1" }
[1]=> array(3) { [0]=> string(54) "Unit Name 2" [1]=> string(15) "Category Name 2" [2]=> string(20) "Course Name 2" }
  [2]=> array(3) { [0]=> string(29) "Unit Name 3" [1]=> string(15) "Category Name 3" [2]=> string(20) "Course Name 3" }
.....

i tried
Code:
foreach($mydata as $row)
            {
            $row->unit_name;
            $row->category_name;
            $row->course_name;
            }
i got this error

Quote:Message: Trying to get property of non-object

then i tried this which worked
Code:
foreach($mydata as $row)
            {
                echo $row[0];
                echo "</br>";
                echo $row[1];
                echo "</br>";
                echo $row[2];
                echo "</br>";
            }
Is there any way to access my inner array items like this , because that makes more sense to me , or is there any easy way to display array elements ?
Quote:$row->unit_name;
$row->category_name;
$row->course_name;
#2

[eluser]Jaketoolson[/eluser]
You have an array, not an object. Also, they keys are numerical (0,1,2,etc) which is why $row[0] works and $row['unit_name'] wouldn't. One option would be to dynamically assign the row key's as variables like this.

Code:
foreach($mydata as $row => $value)
{
    $$row = $value;
}


"Variable Variables"
#3

[eluser]notebook[/eluser]
i am sorry i could'nt understand this concept.
does that mean this ?

Code:
foreach($mydata as $row => $value)
{
$$row = $unit_name;
$$row = $category_name;
$$row = $course_name;

echo $unit_name;
echo $category_name;
echo $course_name;

}
#4

[eluser]dudeami0[/eluser]
Something like this could work:

Code:
foreach($mydata as $row => $value) {
   $unit_name = $value[0];
   $category_name = $value[2];
   $course_name = $value[3];
  
   echo $unit_name;
   echo $category_name;
   echo $course_name;
}

Also, the variable being defined is always on the left, and the value to set is always on the right.
#5

[eluser]notebook[/eluser]
thanks Jaketoolson and dudeami0

that's working !
#6

[eluser]LinkFox[/eluser]
Or make the original array associative.

$temp = array(
[0] => array('name' => 'value'))

Then you can access is with $temp[0]['name'] etc.

I prefer this way of doing arrays as its easier for me / other developers to identify the information being accessed in the array.




Theme © iAndrew 2016 - Forum software by © MyBB