Re: Determine which are user defined keys?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Christoph Boget schreef:
"joe" and "0" are keys that I created whereas the key "1" and "2" are

...

php doesn't differentiate between numeric [integer] strings and actual
integers with regard to array keys.

herein lies the 'problem'

)

but my function above would create an array that looks like this:

Array
(
 [joe] => array( bob, that )
 [0] => array( briggs, other )
 [1] => array( whatever, this )
 [2] => array( whereever, there )
)

perhaps I am approaching this a$$backwards.  I just wanted to keep the
merged array in more or less the same form as the original arrays in
the sense that those values that didn't originally have a user defined
key are not grouped together like those that did originally have a
user defined key.

where do these arrays come from? why is it important to differentiate
between 'user keys' and 'php keys'? if the difference is important maybe
you need to think about creating a different data structure to start with

e.g.

$a = array(
	array("key" => "joe", "val" => "bob"),
	array("key" => "0",   "val" => "briggs"),
	array("key" => null, "val" => "whatever"),
	array("key" => null, "val" => "whatever"),
);

$b = array(
	array("key" => "joe", "val" => "that"),
	array("key" => "0",   "val" => "other"),
	array("key" => null, "val" => "this"),
	array("key" => null, "val" => "there"),
);

$r = array();
foreach(array($a, $b) as $arr)
	foreach($arr as $tmp)
		if (!is_null($tmp["key"]))
			$r[$tmp["key"]][] = $tmp["val"];	
		else
			$r[] = $tmp["val"];

foreach($r as $k => $v)
	if (is_array($v) && count($v) == 1)
		$r[$k] = $v[0];

var_dump($r);

still it would help if you could explain why you need this,
and why you have 'inappropriate' data structures to start with ...
as I mentioned already, you can't differentiate between an automatically
created numeric index and a user defined associative index that happens
to be equivalent to an integer.


thnx,
Christoph


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux