Jump to content

WANT HELP WITH ASSOCIATIVE ARRAY


salman233

Recommended Posts

I WANT HELP WITH ASSOCIATIVE ARRAY THERE IS ARRAY LIKE THIS

 

$ARRAY= array(

 

'dd'=>1,

'dde'=>233,

'qww'=2231

)

i want to store this array in database which data type i should i use want to use mixed data mixed string-numeric type which data type is suitable and i want to print  indexes only but values only what should i do

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/237101-want-help-with-associative-array/
Share on other sites

Not quite understanding your question by seems youre wanting to do store the actual array within your database? If you do then you need to serialize it first. Then when you get it out of the database you'll need to unserialize it to convert it back to an array. The data type you'll want to use would be TEXT.

okay i understand about the serialize concept i want to know which mysql datatype should i use to store these mixed values like ddew,1.6773 for example 

VARCHAR,INT WHICH datatype and how to print the indexs values for associative array

for example  array ('ddd'=>23333  how to print ddd

THIS IS THE ARRAY

$real= array (

'USD'=>1,

'PAK'=>1.6323,

'GBP'=>1.02544

);

$abc= serialize($real);

NO I STORE IT BUT IT WON,T BE STORED

$dql = "INSERT INTO countrytype (`ID`,`fromcountry`,`tocountry`) VALUES ('','".$abc."','".$abc."')";

mysql_query($dql) or die(mysql_error());

NOW TO RETRIVE IT

for($i=0;$i<$row=mysql_fetch_assoc($aws);$i++)

{

$des=unserialize($row['fromcountry']);

$des=explode(",",$row['fromcountry']);

 

echo $row['ID'];

echo $des;

AND PRINTING INDEXES

$k=1;

foreach($des as $fef=>$dwe)

{

echo "<option name='ad[]' value='$dwe'>$dwe</option>";

$k++;

 

}

}

BUT IT DON,T PRINT INDEXES AND DON,T STORE IT IN DATABASE

HELP ME OUT BRO

When using the serialized array in your query you should first pass it to mysql_real_escape_string.

$abc= mysql_real_escape_string( serialize($real) );
$dql = "INSERT INTO countrytype (`ID`,`fromcountry`,`tocountry`) VALUES ('','".$abc."','".$abc."')";

 

When you are retrieving the serialized array this line is not needed, remove it.

$des=explode(",",$row['fromcountry']);

 

This line

$des=unserialize($row['fromcountry']);

Will restore the array.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.