1,105,320 Community Members

Array output

Member Avatar
mlhazan
Newbie Poster
10 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello Experts,
I am getting unwanted result.Please help!
Here is the function:

function ArrayResults($result){
		
		while ($row = mysql_fetch_assoc($result)){
		//echo $row["first_name"];	//tested :works!!
                   $this->arr2[] = $row;
                    
		}
		return $this->arr2;
	}

I called the function and used the print_r to see how is the array:

$mysql->ArrayResults($mysql->ExecuteSQL("SELECT * FROM Employee"));
print_r($mysql ->arr2);

Array look weird:

Array ( [0] => Array ( [id] => 1 [first_name] => Jack [last_name] => Schrvaski [start_date] => 1999-07-20 [end_date] => 2011-07-21 [salary] => 1224.56 [city] => Toronto [description] => Programmer ) [1] => Array ( [id] => 2 [first_name] => David [last_name] => Moron [start_date] => 1999-07-20 [end_date] => 2011-07-21 [salary] => 1224.56 [city] => Toronto [description] => Designer ) [2] => Array ( [id] => 3 [first_name] => Eric [last_name] => Ron [start_date] => 1998-08-29 [end_date] => 2011-03-21 [salary] => 1114.56 [city] => Toronto [description] => Tester ) [3] => Array ( [id] => 4 [first_name] => Frost [last_name] => Robert [start_date] => 1999-01-10 [end_date] => 2011-07-10 [salary] => 2114.56 [city] => Toronto [description] => Manager ) [4] => Array ( [id] => 5 [first_name] => Robin [last_name] => Wood [start_date] => 2009-12-22 [end_date] => 2012-02-13 [salary] => 1000.00 [city] => Thronhill [description] => System Analyst ) )

I tried to get a single data using:

print_r($mysql ->arr2[1][2]);

shows no output.But if i type :

print_r($mysql ->arr2[1]['first_name']);

I get David.My question is how to set the array so that I can use loop to get output.
like:

print_r($mysql ->arr2[$i][$j]);

Please help.

Member Avatar
diafol
Where are my eyes?
12,945 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

Use array_values() to change associative array to indexed array.

$ind = array_values($mysql->arr2[$i]);

loop over $ind

Member Avatar
urtrivedi
Posting Virtuoso
1,897 posts since Dec 2008
Reputation Points: 249 [?]
Q&As Helped to Solve: 408 [?]
Skill Endorsements: 27 [?]
 
0
 

Print_r can only show dump values.
You can try following code to show values on screen in better manner.

<?php 
echo "<table>";
echo "<tr>";

echo "<td>ID</td>";
echo "<td>First Name</td>";
echo "<td>Last Name</td>";
   
echo "</tr>";
for ($i=0;$i<count($mysql->arr2);$i++)
{
   echo "<tr>";

   echo "<td>{$mysql->arr2[$i]['id']}</td>";
   echo "<td>{$mysql->arr2[$i]['first_name']}</td>";
   echo "<td>{$mysql->arr2[$i]['last_name']}</td>";
   
   echo "</tr>";
}
echo "</table>";
?>

or still you want to use print_r, then use following code

<?php 

for ($i=0;$i<count($mysql->arr2);$i++)
{

   echo "<pre>";   
   print_r($mysql->arr2[$i]);
   echo "</pre>";
}

?>
Question Answered as of 2 Years Ago by diafol and urtrivedi
Member Avatar
mlhazan
Newbie Poster
10 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks guys.It was really helpful.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: