Jump to content

Echoing the Elements of a Parsed Array


iwato
 Share

Recommended Posts

PROBLEM: Please consider the following two blocks of code (they differ only in their last line) and explain why the first echoes the number of the discovered group name, and the second produces nothing at all.COMMENT: I am teaching myself how to work with PHP arrays and am baffled by the result.

			<?php				$owner = fileowner("../copy/nonPHP_file.txt");				echo $owner . "<br />";				$ownerArray = Array();				$ownerArray = posix_getpwuid($owner);				$count = count($ownerArray);				echo $count . "<br /><br />";				foreach ($ownerArray as $ownerInfo) {					echo $newArray[] = $ownerInfo . "<br />";				}				echo "<br />" . $newArray[3];				?>

			<?php				$owner = fileowner("../copy/nonPHP_file.txt");				echo $owner . "<br />";				$ownerArray = Array();				$ownerArray = posix_getpwuid($owner);				$count = count($ownerArray);				echo $count . "<br /><br />";				foreach ($ownerArray as $ownerInfo) {					echo $newArray[] = $ownerInfo . "<br />";				}				echo "<br />" . $ownerArray[3];				?>

Roddy

Link to comment
Share on other sites

That's strange... PHP is always strange on the MAC, but if this is a bug, it would be ridiculous.I suggest you add

$newArray = array();

before the foreach, in order to initialize the array. The same goes for the $ownerArray - it's "array", in all lowercase.Last, but not least, try to var_dump() both arrays before and after the foreach. Maybe that would reveal something.

Link to comment
Share on other sites

MYSTERY SOLVED

I suggest you add
$newArray = array();

before the foreach, in order to initialize the array. The same goes for the $ownerArray - it's "array", in all lowercase.

These changes had no effect.
Last, but not least, try to var_dump() both arrays before and after the foreach. Maybe that would reveal something.
Using the var_dump() function proved very beneficial. What I discovered is the following:1) $ownerArray is an associative array with keys and values.2) $newArray is an index array with numbers and values.After entering the key obtained from the var_dump() function between the brackets of $ownerArray[] the first and second sets of code produced identical results.So as to avoid this sort of problem in the future, do you know of a function by which one can discover the array-type without having to dump the array's entire contents to find out? I checked the entire list of array functions in the online PHP manual and could find nothing that answers directly the question: "Is this array an associate, indexed, or mixed array?"By the way, thanks for teaching about the var_dump() function. A very useful piece of code!Roddy
Link to comment
Share on other sites

In PHP, arrays can contain both string and numeric keys, so it's technically not possible to find out that about the array without going over the array's keys in some way.You do have the is_string() and is_numeric() functions to check if something is a string or a numer. So, even though such a function doesn't exists, you can write a function that does this for you, like for example:

function is_associative_array($array) {	foreach($array as $key => $value) {		if (is_string($key)) {			return true;		}	}	return false;}

Alternatively, the commments in the is_array() function suggest this function for the same purpose:

function is_assoc($array) {	return (is_array($array) && (0 !== count(array_diff_key($array, array_keys(array_keys($array)))) || count($array)==0));}

Link to comment
Share on other sites

[The] commments in the is_array() function suggest this function for the same purpose:
function is_assoc($array) {	return (is_array($array) && (0 !== count(array_diff_key($array, array_keys(array_keys($array)))) || count($array)==0));}

This is a nifty bit of code, but I am not sure why it returns an empty initialized array as an associative array.A more important personal dilemma: What is the best way to globalize the is_assoc( ) function so that I may draw on it anytime and anyplace?In Action Script I would likely create a class for it. How is it best done in PHP?Roddy
Link to comment
Share on other sites

This is a nifty bit of code, but I am not sure why it returns an empty initialized array as an associative array.
return (is_array($array) && (... count($array)==0));It will return true if $array is an array and the count is 0 (it has 0 elements). The other condition is a way of checking that all of the keys are numeric or not.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...