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
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
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
Share on other sites

Is the serialize option faster than implode?

 

the reason why I had the

 

$result=Array();

 

was cause I kept getting an error and it wouldnt allow me to do an array_push()

 

I assumed that the variable needed to be initialized as an array before you could push to it...

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.