Re: Determine which are user defined keys?

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

 



> > "joe" and "0" are keys that I created whereas the key "1" and "2" are
> > keys assigned by PHP when the array was created.  When iterating
> > through an array, is there a way to determine which were generated by
> > PHP?  I can't rely on whether or not the key is an integer because
> > it's quite possible that such a key was user generated.  I've gone
> > through the docs and based on what I've read, I don't think something
> > like this is possible but I'm hoping that's not the case.
> > Any pointers?
> explain what your trying to achieve and why. because it seems like your
> 'requirement' is a result of tackling the problem from the wrong end.

array_merge() clobbers the value of a particular key if that key
exists in any of the arrays that appear later in the argument list for
that function.  If I want it so that I can merge arrays but make it so
that all the values are retained, I need to write a function myself.
So basically I end up with something that approximates:

function array_merge_retain( $arr1, $arr2 ) {
  $retval = array();

  foreach( $arr1 as $key => $val ) {
    $retval[$key][] = $val;
  }

  foreach( $arr2 as $key => $val ) {
    $retval[$key][] = $val;
  }
  return $retval;

}

(there would be a lot of other stuff going on, error checking and
such, but the above is just bare bones).

What that gives me is an array where the values for keys that appear
in both arrays are retained.  The above function is all well and fine,
I suppose, but when the keys were created by PHP, I don't really care
about retaining the key.  If the key was created by PHP, i'd just do
an array_push() instead of explicitly defining the key, as I am in the
above function.  That way, in the end, the merged array would contain
arrays for values for only those keys that were user defined.

Consider the following example:

$myArr1 = array( 'joe' => 'bob', "0" => 'briggs', 'whatever', 'whereever');
$myArr2 = array( 'joe' => 'that', "0" => 'other', 'this', 'there');

the values 'whatever' and 'this' both have a key of 1 while 'wherever'
and 'there' both have a key of 2.  When merged, I envision the new
array looking something like this:

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

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.

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