php_begins Posted August 31, 2011 Share Posted August 31, 2011 Hello, i retrieve an array from the database that looks something like this: $var=a:1:{s:2:"cc";a:1:{i:22;s:11:"TESTING";}}; I need to retrieve the number corresponding to the character i: and print it(11 in the first case). For example , If, $var=a:1:{s:2:"cc";a:1:{i:15;s:11:"TESTING";}}; I would need to print 15. $var=a:1:{s:2:"cc";a:1:{i:3;s:11:"TESTING";}}; I would need to print 3. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/246128-printing-a-specific-number-from-an-array/ Share on other sites More sharing options...
php_begins Posted August 31, 2011 Author Share Posted August 31, 2011 Hello, i retrieve an array from the database that looks something like this: $var=a:1:{s:2:"cc";a:1:{i:22;s:11:"TESTING";}}; I need to retrieve the number corresponding to the character i: and print it(11 in the first case). For example , If, $var=a:1:{s:2:"cc";a:1:{i:15;s:11:"TESTING";}}; I would need to print 15. $var=a:1:{s:2:"cc";a:1:{i:3;s:11:"TESTING";}}; I would need to print 3. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/246128-printing-a-specific-number-from-an-array/#findComment-1264005 Share on other sites More sharing options...
PaulRyan Posted August 31, 2011 Share Posted August 31, 2011 Is this the actual resulting text from the database, or is the actual text: a:1:{s:2:"cc";a:1:{i:22;s:11:"TESTING";}} Regards, PaulRyan. Link to comment https://forums.phpfreaks.com/topic/246128-printing-a-specific-number-from-an-array/#findComment-1264011 Share on other sites More sharing options...
php_begins Posted August 31, 2011 Author Share Posted August 31, 2011 The actual resulting text from the database. The format is the same for every retrieval. i need to retrieve the number corresponding to i: Link to comment https://forums.phpfreaks.com/topic/246128-printing-a-specific-number-from-an-array/#findComment-1264015 Share on other sites More sharing options...
premiso Posted August 31, 2011 Share Posted August 31, 2011 That is just a serizlied array. Use unserialize and then you can access it like a normal array. Link to comment https://forums.phpfreaks.com/topic/246128-printing-a-specific-number-from-an-array/#findComment-1264016 Share on other sites More sharing options...
PaulRyan Posted August 31, 2011 Share Posted August 31, 2011 Heres a simple way, kind of hacky, but it works nonetheless <?PHP $dbText = '$var=a:1:{s:2:"cc";a:1:{i:15;s:11:"TESTING";}};'; $string = explode(';',end(explode('{i:',$dbText,2)),2); $number = $string[0]; echo $number; ?> Regards, PaulRyan. Link to comment https://forums.phpfreaks.com/topic/246128-printing-a-specific-number-from-an-array/#findComment-1264020 Share on other sites More sharing options...
php_begins Posted August 31, 2011 Author Share Posted August 31, 2011 Pardon me, I am a still a novice. When I do unserialize & print, i get the following data: Array ( [bcc] => Array ( [898] => tester ) ) how do i just print just the value 898 and tester from the variable? Link to comment https://forums.phpfreaks.com/topic/246128-printing-a-specific-number-from-an-array/#findComment-1264023 Share on other sites More sharing options...
premiso Posted August 31, 2011 Share Posted August 31, 2011 By using list if the array always has 1 value pair. If not you would need to use a foreach loop: list($key, $value) = $var['bcc']; Foreach: foreach ($var['bcc'] as $key => $value) { echo $key . " " . $value . " " ; } $key would hold 898 and $value would hold tester. And just for the record, this has nothing to do with regex Link to comment https://forums.phpfreaks.com/topic/246128-printing-a-specific-number-from-an-array/#findComment-1264024 Share on other sites More sharing options...
php_begins Posted August 31, 2011 Author Share Posted August 31, 2011 Thanks a lot ..i didnt realize the use of unserialize function... Link to comment https://forums.phpfreaks.com/topic/246128-printing-a-specific-number-from-an-array/#findComment-1264028 Share on other sites More sharing options...
PFMaBiSmAd Posted August 31, 2011 Share Posted August 31, 2011 Too bad this was a duplicate/cross-posted thread. This was solved in the other thread (the data is serialized and simply needs to be unserialized to get the original array back.) Link to comment https://forums.phpfreaks.com/topic/246128-printing-a-specific-number-from-an-array/#findComment-1264033 Share on other sites More sharing options...
PaulRyan Posted August 31, 2011 Share Posted August 31, 2011 Ahh yeah, I'm not to familiar with serialize and unserialize. Apologies, PaulRyan. Link to comment https://forums.phpfreaks.com/topic/246128-printing-a-specific-number-from-an-array/#findComment-1264035 Share on other sites More sharing options...
Pikachu2000 Posted August 31, 2011 Share Posted August 31, 2011 Duplicate threads have been merged to this one. Don't double post. Link to comment https://forums.phpfreaks.com/topic/246128-printing-a-specific-number-from-an-array/#findComment-1264037 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.