PHP - Unserialize Array And Display
my user pm table has a touserarray field which is a serialized array that looks like this:
a:1:{s:2:"cc";a:1:{i:15773;s:14:"testusername";}} i know I can unserialize it with the unserialize() function. But I need to extract the value corresponding to 'i' field and the 's' field that has the username. So from the above array, i need to retrieve the value '15773' and 'testusername'. Can someone tell me how? Similar Tutorialsthe code is simple but the output is wrong. Code: [Select] $str = <<<EOF a:2:{s:7:"visible";a:7:{i:0;s:5:"email";i:1;s:5:"print";i:2;s:8:"facebook";i:3;s:4:"digg";i:4;s:11:"stumbleupon";i:5;s:7:"twitter";i:6;s:6:"reddit";}s:6:"hidden";a:0:{}} EOF; echo "<textarea rows='10' cols='90'>";print_r(unserialize($str));echo "</textarea>"; the ouput is Code: [Select] Array ( [visible] => Array ( [0] => email [1] => print [2] => facebook [3] => digg [4] => stumbleupon [5] => twitter [6] => reddit ) [hidden] => Array ( ) ) This is wrong I need something like this: Code: [Select] array ( 'visible' => array ( 0 => 'email', 1 => 'print', 2 => 'facebook', 3 => 'digg', 4 => 'stumbleupon', 5 => 'twitter', 6 => 'reddit', ), 'hidden' => array ( ), ) What am I doing wrong? How do I fix it? guys this is my code <?php include("header.php"); include("conn.php"); ?> <html> <head> </head> <body> <table border="1" cellpadding="0" cellspacing="0" align="center"> <tr> <td>Please Enter the Amount</td> <td valign="top" align="left" width="200"> <form action="next.php" method="post"> <input name="amount" value="1" size="7"> </td> <tr> <td>From:</td> <td valign="top" align="left" width="200"> <?php $real= array ( 'USD'=>1, 'PAK'=>1.6323, 'GBP'=>1.02544 ); $abc= mysql_real_escape_string( serialize($real) ); //$ddsr=unserialize($abc); //$abc= implode(",",$real); //echo $abc; $from= array('Dollars','Rupees','Pounds'); $length = count($from); //$dbs=implode(",",$from); $dql = "INSERT INTO countrytype (`ID`,`fromcountry`,`tocountry`) VALUES ('','".$abc."','".$abc."')"; mysql_query($dql) or die(mysql_error()); $to= array('Dollars','Rupees','Pounds'); $to=str_replace("","",$to); ?> <input type="hidden" name="h1" value="<?php echo $length;?>"> <select> <?PHP $dgd= "SELECT * FROM countrytype GROUP BY ID"; $aws= mysql_query($dgd) or die(mysql_error()); while($row=mysql_fetch_array($aws)) { $des=stripslashes($row['fromcountry']); $dsw=unserialize($des); //$des=explode("",$row['fromcountry']); //var_dump($dsw); //echo "<td>".$row['ID']."<td>"; //echo "<td>".$dsw."<td>"; $k=1; foreach($dsw as $key=>$value) { echo "<option name='ad[]' value='$value'>$value</option>"; //print_r(array_keys($des)); $k++; } } //$sss=explode("",$row['fromcountry']); //echo $sss; ?> </select> <select> <?php $i=0; $k=1; foreach($from as $abd => $value) { echo "<option name='ar[]' value='$value' >$value</option>"; $k++; } ?></td></tr></select> <td>To: </td><td><select> <?php foreach($to as $abd => $value) { $i++; echo "<option id='i' >$value</option>"; } ?> </select> <input type="submit" name="sub" value="Convert!"/> </form> </td> </tr> </tr> </table> </body> </html> serilization works but when i fetch it from database and unserialize it nothing works Hello all, I need to make a rotating banner system for a client. They want about 5 banners to rotate with Javascript. However they want the visitor to sort of step through each one on each refresh. So if I have 5 banners and I go to the site, I see banner 1 and then the javascript rotates through each one. When I go back to the site, they want it to start with banner 2 and then the javascript will rotate through each one. Then on and on. Go back and see banner 3, then step through again. I have a nice Javascript image slider I'd like to use. It basically just requires the images in order. Like: <img src="1.jpg"/> <img src="2.jpg"/> <img src="3.jpg"/> <img src="4.jpg"/> <img src="5.jpg"/> I've thought about it and I think the best way to do this would just be using PHP and a cookie. So when the viewer first goes to the site, PHP checks for the cookie. If it exists, it pulls the starting image #. If it doesn't, it starts at 0 and sets the cookie. As for the images part, I figured an associate array would work well. The key would be what the cookie logic is based on, and then the image HTML would be in the array. So you visit the site without a cookie it would be... 0 => <img src="1.jpg"/> 1 => <img src="2.jpg"/> 2 => <img src="3.jpg"/> 3 => <img src="4.jpg"/> 4 => <img src="5.jpg"/> Then when you go back to the site, I need the array to be re-ordered like this: 0 => <img src="2.jpg"/> 1 => <img src="3.jpg"/> 2 => <img src="4.jpg"/> 3 => <img src="5.jpg"/> 4 => <img src="1.jpg"/> What's the best way to go about this array logic? My experience with manipulating data inside arrays is rather limited. Basically I need it to say "Start with key # and then reorder array based on that" Hey I got this script which is trying to unserialize but i get this error and have no idea what it means so i equally have no idea how to fix the problem. This is what it says: Quote Notice: unserialize() Error at offset 0 of 4 bytes This is the line which causes the error: Code: [Select] <?php $contents = unserialize(file_get_contents($this->msgsFile)); ?> Does any one know what the error means? And what i need to look for to fix it. Need a bit of help with some php and a few kind members helped me in the past. My code is: $query = "SELECT names FROM table WHERE type='$type'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { foreach(unserialize($row['names']) as $key => $value){ echo $value . '<br />'; } } Basically, a field on my table is called names. The information in this field is serialized (so many names are together in a single field, for a single row). I want to be able to search the whole table (where type='$type' - so many rows) for all names and then show unique names only. My code above unserializes the names from each field and then lists them, but doesn't deal with any duplicates (which I need removing). I have looked at functions like "unique_array()", and have tried using it in different places but it isn't doing the job. Any help appreciated. I took an object, and saved it into a database serialized. Code: [Select] $object_here = serialize($theobject); Then later on, I go and I get that data from a database, and try to unserialize it.. Code: [Select] $objecthere = unserialize($row->theobject) And it throws the following error...any advice? Quote Warning: unserialize() [function.unserialize]: Node no longer exists in /path/to/file on line 20 i tried to serialize an array in php but im not sure if i had the right syntax $field[0] = "Student Name"; $field[1] = "fail"; $field_ser = serialize($field); $field_unser = unserialize($field_ser); i think i didnt do it right Hi, I'm new to PHP and wondered if anyone could help... I am consuming an asmx webservice using PHP code. (Later to make a widget for wordpress). I can receive all the data, but it is in one very big xml file. I want to be able to convert this data, so I can make it more readable. Could really do with help as I don't have a clue how to convert it. ANy ideas? Hi everyone, Im trying to serialize the values of checked checkboxes, save them to a database, then on another page retrieve the value from the database, unserialize it, and then write each checkbox value to the database individually. Using the following test code I'm able to serialize the checkbox values, and then unserialize them and echo them out fine, however im unsure how to separate them and save them individually into the database. $string = implode($_POST['checkbox']); $test = serialize($string); $test2 = unserialize($test); echo $test2; <?php $i = 0; ?> <?php while ($row = mysql_fetch_assoc($query)) { ?> <tr> <td><input type="checkbox" name="<?php echo "checkbox[$i]"; ?>" value="<?php echo $row['test']; ?>" /></td> </tr> <?php $i++; } ?> Any ideas? Thanks I am trying to unserialize this string: a:3:{s:3:\"zip\";s:5:\"55068\";s:4:\"city\";s:9:\"Rosemount\";s:5:\"state\";s:2:\"MN\";} but I unserialize is returning false. Why is it doing that? $info = unserialize($_COOKIE['zipcode']); var_dump($info); I have an array from the database like so Array (2) ( | ['0'] => Array (4) | ( | | ['f_fid'] = String(1) "1" | | ['0'] = String(1) "1" | | ['f_names'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} " | | ['1'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} " | ) | ['1'] => Array (4) | ( | | ['f_fid'] = String(1) "2" | | ['0'] = String(1) "2" | | ['f_names'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} " | | ['1'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} " | ) ) So i use a foreach loop to turn the serialized value into an array foreach($fields as $key => $val) { $fields[$key]['f_names'] = unserialize($val['f_names']); } But for some reason the last f_names is false Array (2) ( | ['0'] => Array (4) | ( | | ['f_fid'] = String(1) "1" | | ['0'] = String(1) "1" | | ['f_names'] => Array (4) | | ( | | | ['0'] = String(18) "register_user_name" | | | ['1'] = String(5) "r_u_n" | | | ['2'] = String(10) "user_alias" | | | ['3'] = String(12) "nickusername" | | ) | | ['1'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} " | ) | ['1'] => Array (4) | ( | | ['f_fid'] = String(1) "2" | | ['0'] = String(1) "2" | | ['f_names'] = Boolean(0) FALSE | | ['1'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} " | ) ) Anyone have any idea why? Thanks Is this a mistake in the documentation? I am running PHP7.4 and get warning: unserialize() expects parameter 2 to be array, bool given. QuoteEither an array of class names which should be accepted, false to accept no classes, or true to accept all classes. If this option is defined and unserialize() encounters an object of a class that isn't to be accepted, then the object will be instantiated as __PHP_Incomplete_Class instead. Omitting this option is the same as defining it as true: PHP will attempt to instantiate objects of any class.
Debian 5 PHP 5.2.6-1 This script works: <? $output = shell_exec('ls -lart'); echo "<pre>$output</pre>"; ?> This script doesn't work: <? $vars=unserialize(file_get_contents("file.txt")); $output = shell_exec('ls -lart'); echo "<pre>$output</pre>"; ?> I get error: Quote Warning: shell_exec(): Unable to execute 'ls -lart' or Warning: system(): Unable to fork [ls -lart] If file.txt has size 500k, the script works without errors. But my file file.txt has size 30M. Help. Hi, I'm hoping someone can help me with a simply array problem. I have a table with two columns: "ID" and "Name" and 100 Names. I also have an array called $myIDList containing 10 IDs. I want to display 10 Names from the table that correspond with the IDs from my array. Originally, I tried to implode the array, adding commas and inserted into my MySQL query using: $myQuery = "SELECT ID, Name FROM namesTable WHERE ID IN ($myImplodedIDList)"; The problem was that duplicated names (i.e. the same Name, but assiged to different IDs) would only be displayed once. So now I'm trying to use the original array to loop through a second array and display Names where the ID matches an ID from my first array. I have succesffuly listed the table like this: $myQuery = "SELECT ID, Name FROM myTable ORDER BY ID"; $myResult = mysql_query($myQuery) or die(mysql_error()); while($row=mysql_fetch_array($myResult)) { echo $row['ID']; echo $row['Name']; } But I want to do something like: while($row=mysql_fetch_array($myResult)) { while $row['ID'] = $myIDList { echo $row['Name'] } } Can someone shed some light onto this for me please? Thank you! Bryan Hello, I have a call tu function unserialize() in a script that receives a string and has to return an array. In my localhost it works properly but in the server it returns a 1 dimension array with empty value. I'm testing with the string a:1:{s:1:"0";s:8:"value_eq";} The php.ini configuration for magic_quotes is (in both, server and localhost): magic_quotes_gpc Off magic_quotes_runtime Off Other configurations is difficult to compare, as php info is long and very different in local and server. Any ideas where the problem might be? Thank you Hi, Is it possible to display the whole array of voteID in this url? https://falling-skies-tv.loopiasecure.com/vote_result.php?voteID=292&comp=falling-skies-danmark Is there a script/website/code I can use? Thank you! $color=array("01 orange","02 blue","03 red"); If my array was structured this way where the first two characters represented a code, how would I be able to display the code and color distinctly? Hi, i'm getting an error when I load my php code in a browser. Here's my code snippet: <?php mysql_connect("localhost","root"); mysql_select_db("something"); $bellProductsArray=array(1=>"Apple_iPhone3GS.jpg",2=>"Apple_iPhone4.jpg"); $result=mysql_query("SELECT Name, Manufacturer FROM bellProducts WHERE ID=1"); $row=mysql_fetch_assoc($result); print "Name: {$row['Name']} Manufacturer: {$row['Manufacturer'] }"; ?> <img src=<?php array_values($bellProductsArray); ?> alt="Apple iPhone 3GS" title="Apple iPhone 3GS" /> **I can't get it to display the first image (eg: Apple_iPhone3GS.jpg), I made sure that I have the image in same directory. please help!1! Example: i have an array of names array{ => john [1] = ted [2] = jeff } and i display them like this: john, ted, jeff but jeff is currently logged in so i want his name to display first(i have my reasons lol) how would i go about this? i got as far as: if(in_array($user->user_name, $likes)) { $user_key = array_search($user->user_name, $likes); } then hit a brain freeze. Thanks |