This is a discussion on Determine which are user defined keys? within the PHP General forums, part of the PHP Programming Forums category; Given the following array: <?php $myArr = array( 'joe' => 'bob', "0" => 'briggs', 'whatever', 'whereever'); echo '<...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Given the following array:
<?php $myArr = array( 'joe' => 'bob', "0" => 'briggs', 'whatever', 'whereever'); echo '<pre>' . print_r( $myArr, TRUE ) . '</pre>'; ?> Array ( [joe] => bob [0] => briggs [1] => whatever [2] => whereever ) "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? thnx, Christoph |
|
|||
|
2008. 01. 11, pĂ©ntek keltezĂ©ssel 08.12-kor Christoph Boget ezt Ă*rta:
> Given the following array: > > <?php > $myArr = array( 'joe' => 'bob', "0" => 'briggs', 'whatever', 'whereever'); > echo '<pre>' . print_r( $myArr, TRUE ) . '</pre>'; > ?> > > Array > ( > [joe] => bob > [0] => briggs > [1] => whatever > [2] => whereever > ) > > "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. I don't think you can do that. Why not apply some modification to the 'user created' keys, like prepend them with a _ sign or something. in that case you can know for sure if its a php generated key or not. greets Zoltán Németh > > Any pointers? > > thnx, > Christoph > |
|
|||
|
Christoph Boget schreef:
> Given the following array: > > <?php > $myArr = array( 'joe' => 'bob', "0" => 'briggs', 'whatever', 'whereever'); > echo '<pre>' . print_r( $myArr, TRUE ) . '</pre>'; > ?> > > Array > ( > [joe] => bob > [0] => briggs > [1] => whatever > [2] => whereever > ) > > "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. > > thnx, > Christoph > |
|
|||
|
On Jan 11, 2008 1:17 PM, Jochem Maas <jochem@iamjochem.com> wrote:
> 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. I'd wait and listen to what Jochem has to say first, but you might be able to keep a copy of your original array and do some sort of array_diff to find out what what changed. |
|
|||
|
> > "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 |
|
|||
|
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 > |
|
|||
|
On Fri, January 11, 2008 7:12 am, Christoph Boget wrote:
> Given the following array: > > <?php > $myArr = array( 'joe' => 'bob', "0" => 'briggs', 'whatever', > 'whereever'); > echo '<pre>' . print_r( $myArr, TRUE ) . '</pre>'; > ?> > > Array > ( > [joe] => bob > [0] => briggs > [1] => whatever > [2] => whereever > ) > > "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. I doubt that even down in the guts of PHP there's any record of which kind of key it was, so I doubt that you can do that without hacking PHP scource in a big way... WHY would you want to do this anyway? Give us the Big Picture, and maybe you'll get some ideas for another approach. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/from/lynch Yeah, I get a buck. So? |
![]() |
| Thread Tools | |
| Display Modes | |
|
|