Jump to content

[SOLVED] Arrays


mpharo

Recommended Posts

I have an array which is generated by the result of an SQL query...

 

What I am trying to do is take that array and insert it into a different table in a single field, so what  I am trying to do is make all the elements in a array into a single variable I can use to insert into a field with each array element seperated by a comma.  This would be easy if the array had the same amount of lines each time but it dosent, sometimes the array has 1 element sometimes it has 5....any ideas??

Link to comment
https://forums.phpfreaks.com/topic/46621-solved-arrays/
Share on other sites

I dont think that is really what im looking for....essentially here is the deal...

 

I have an array which contains random amounts...

 

I need to take that array and put it into a single variable with a comma seperating eash element in the array...

 

Here is a example of what I have now where I create the array $result, I need to take $result and put it into a different variable seperated by commas....

 

<?

$select2=mysql_query("SELECT category FROM blacklist WHERE domain='$matches[0]'");

 

$count_rows=mysql_num_rows($select2);

 

If($count_rows>1){

 

$result=Array();

 

while($sql2=mysql_fetch_array($select2)){

 

array_push($result, $sql2[0]);

 

}

 

}

 

print_r($result);

 

?>

Link to comment
https://forums.phpfreaks.com/topic/46621-solved-arrays/#findComment-227013
Share on other sites

EDIT: what pikemsu28 wrote is a better way, this is an alternative.

 

You never stated the comma separated portion. You just wanted it in the db as a single string, which the serialize would have done.

 

Try this:

<?
$select2=mysql_query("SELECT category FROM blacklist WHERE domain='$matches[0]'");
$count_rows=mysql_num_rows($select2);
If($count_rows>1){
//$result=Array(); ??
$i=0;
while($sql2=mysql_fetch_array($select2)){
     foreach ($sql2 as $value) 
            $newString[$i] .= $value . ",";
     $newString[$i] = substr($newString[$i], 0, -1); // remove the last , since there is no value for it.
  $i++;
}

}

print_r($newString);

?>

 

That should help you out.

Link to comment
https://forums.phpfreaks.com/topic/46621-solved-arrays/#findComment-227016
Share on other sites

Serialize does not make it comma delimited, it just allows you to keep the structure and type of a variable. As we know it is hard to just put an array or object into a DB without this.

 

So if you serialize an array, when you retrieve it and unserialize that array you will have the same structure and values that it had when you put it into the DB.

 

What you "want" is to make the array one long string comma delimited, in that case the implode works. However if you want it to stay an array serialize it.

Link to comment
https://forums.phpfreaks.com/topic/46621-solved-arrays/#findComment-227019
Share on other sites

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.