[PHP] convert associative array to ordinary array
Hi, i have an associative array and i want to use it as an ordinary array, is that possible? what i mean is instead of $arr['fruit'] i want to call it by its position in the array $arr[3] thanks Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] convert associative array to ordinary array
On Fri, Mar 28, 2008 at 2:27 PM, It Maq [EMAIL PROTECTED] wrote: Hi, i have an associative array and i want to use it as an ordinary array, is that possible? what i mean is instead of $arr['fruit'] i want to call it by its position in the array $arr[3] Did you try? -- /Daniel P. Brown Forensic Services, Senior Unix Engineer 1+ (570-) 362-0283 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] convert associative array to ordinary array
Daniel Brown wrote: On Fri, Mar 28, 2008 at 2:27 PM, It Maq [EMAIL PROTECTED] wrote: Hi, i have an associative array and i want to use it as an ordinary array, is that possible? what i mean is instead of $arr['fruit'] i want to call it by its position in the array $arr[3] Did you try? array_values() will throw out the keys and replace them with numeric indices. Anyway, in PHP there is no difference between an associative array or an ordinary array, they're both the same thing. Only one has integers as keys while the other has strings... - Tul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] convert associative array to ordinary array
On Fri, Mar 28, 2008 at 10:33 AM, Daniel Brown [EMAIL PROTECTED] wrote: On Fri, Mar 28, 2008 at 2:27 PM, It Maq [EMAIL PROTECTED] wrote: Hi, i have an associative array and i want to use it as an ordinary array, is that possible? what i mean is instead of $arr['fruit'] i want to call it by its position in the array $arr[3] Did you try? -- /Daniel P. Brown Forensic Services, Senior Unix Engineer 1+ (570-) 362-0283 $numbered_array = array_values($associative_array); -- -Casey -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] convert associative array to ordinary array
thank you, it works! --- Casey [EMAIL PROTECTED] wrote: On Fri, Mar 28, 2008 at 10:33 AM, Daniel Brown [EMAIL PROTECTED] wrote: On Fri, Mar 28, 2008 at 2:27 PM, It Maq [EMAIL PROTECTED] wrote: Hi, i have an associative array and i want to use it as an ordinary array, is that possible? what i mean is instead of $arr['fruit'] i want to call it by its position in the array $arr[3] Did you try? -- /Daniel P. Brown Forensic Services, Senior Unix Engineer 1+ (570-) 362-0283 $numbered_array = array_values($associative_array); -- -Casey Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] [SOLUTION] Re: [PHP] Convert an array in an object instance
Ok, I've write a function that emulates the Perl's bless one. It works pretty well. The idea comes to me when Rob suggest to parse the serialized data. I think this is better -or at least faster- than parsing all serialized data to modify object values: Just convert the object to an array, modify whatever values you want -no matter about member visibility-, and then convert the array again in an object. Note that you should NEVER modify private and protected members outside its visibility environment... well, never UNLESS you're writting your own serialization routines indeed (that's exactly because I need to do it ;-) ) ? function bless ( $Instance, $Class ) { $serdata = serialize ( $Instance ); /* For an array serialized data seems to meant: array_tag:array_count:{array_elems} array_tag is always 'a' array_count is the number of elements in the array array_elems are the elemens in the array For an object seems to meant: object_tag:object_class_name_len:object_class_name:object_count:{object_members} object_tag is always 'O' object_class_name_len is the length in chars of object_class_name string object_class_name is a string with the name of the class object_count is the number of object members object_members is the object_members itself (exactly equal to array_elems) */ list ($array_params, $array_elems) = explode ('{', $serdata, 2); list ($array_tag, $array_count) = explode (':', $array_params, 3 ); $serdata = O:.strlen ($Class).:\$Class\:$array_count:{.$array_elems; $Instance = unserialize ( $serdata ); return $Instance; } class TestClass { private $One=1; protected $Two=2; public $Three=3; public function sum() { return $this-One+$this-Two+$this-Three; } } $Obj = new TestClass (); $Clone = (array) $Obj; echo As an array:br; print_r ($Clone); bless ( $Clone, TestClass ); echo brbrAfter blessing as a TestClass instance:br; print_r ($Clone); echo brbrCalling sum method: ; echo $Clone-sum(); echo brThe array was blessed! miracle!!! ;-)br; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] [SOLUTION] Re: [PHP] Convert an array in an object instance
Erm... I've seen there're some aspects to perform... it fails because the name of the members is changed during conversion to the array. It puts the class name using '\0' (0 is a zero, not a caps 'o') character as separator before member name in private and an '*' in protected. It's not an unaffordable issue, because you still can modify array values having this in account. For example, if you want to modify the $One private member in the array, you cannot do: $Clone['One']=5; you should do $Clone[\0TestClass\0One]=5; instead. Anyway I think it should be easily fixed (when I've time to do it, now I must work in another thing :-| ) Francisco M. Marzoa Alonso wrote: Ok, I've write a function that emulates the Perl's bless one. It works pretty well. The idea comes to me when Rob suggest to parse the serialized data. I think this is better -or at least faster- than parsing all serialized data to modify object values: Just convert the object to an array, modify whatever values you want -no matter about member visibility-, and then convert the array again in an object. Note that you should NEVER modify private and protected members outside its visibility environment... well, never UNLESS you're writting your own serialization routines indeed (that's exactly because I need to do it ;-) ) ? function bless ( $Instance, $Class ) { $serdata = serialize ( $Instance ); /* For an array serialized data seems to meant: array_tag:array_count:{array_elems} array_tag is always 'a' array_count is the number of elements in the array array_elems are the elemens in the array For an object seems to meant: object_tag:object_class_name_len:object_class_name:object_count:{object_members} object_tag is always 'O' object_class_name_len is the length in chars of object_class_name string object_class_name is a string with the name of the class object_count is the number of object members object_members is the object_members itself (exactly equal to array_elems) */ list ($array_params, $array_elems) = explode ('{', $serdata, 2); list ($array_tag, $array_count) = explode (':', $array_params, 3 ); $serdata = O:.strlen ($Class).:\$Class\:$array_count:{.$array_elems; $Instance = unserialize ( $serdata ); return $Instance; } class TestClass { private $One=1; protected $Two=2; public $Three=3; public function sum() { return $this-One+$this-Two+$this-Three; } } $Obj = new TestClass (); $Clone = (array) $Obj; echo As an array:br; print_r ($Clone); bless ( $Clone, TestClass ); echo brbrAfter blessing as a TestClass instance:br; print_r ($Clone); echo brbrCalling sum method: ; echo $Clone-sum(); echo brThe array was blessed! miracle!!! ;-)br; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Convert an array in an object instance
Hi, Is it possible to convert an array in an object instance? I can convert an object in an array as follows: $Test = new MyClass (); $TestArray = (array) $Test; Then I've an array with all members of the object $Test, but it seems that I cannot simply do: $TestObject = (MyClass) $TestArray; To recover the object in the inverse way, so... is there any manner to do this? Thanks a lot in advance, -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Convert An Array
http://ccl.flsh.usherb.ca/db/authors_under_study.php or $names = array (john, mary, bill, mary, bill, mary, bill, john, bill, john); 1) How do I count the elements of $names to produce: bill 4 john 3 mary 3 2) How do I store the counted elements into a new array? How do I create this: $new_array = array (bill=4, john=3, mary=3) I'm looking at http://www.php.net/manual/en/function.array.php but not sure.
Re: [PHP] Convert An Array
http://www.php.net/manual/en/function.array-count-values.php - Original Message - From: jtjohnston [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Sunday, February 24, 2002 10:49 PM Subject: [PHP] Convert An Array http://ccl.flsh.usherb.ca/db/authors_under_study.php or $names = array (john, mary, bill, mary, bill, mary, bill, john, bill, john); 1) How do I count the elements of $names to produce: bill 4 john 3 mary 3 2) How do I store the counted elements into a new array? How do I create this: $new_array = array (bill=4, john=3, mary=3) I'm looking at http://www.php.net/manual/en/function.array.php but not sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php