Solved

String vs Array

Posted on 2008-06-23
3
248 Views
Last Modified: 2008-07-05
PHP
Question:
I have bunch code that im using array to store the data.
I was think to change array to string.
how can i do that
as show below is one small part of my code.
how can i change it to string that i can get the same output result

  $query = mysql_query(select * from student);
   
    while($row = mysql_fetch_array($query))
       {
       $this->GetCompNumberReturn[$i] = $row['COMPNUMBER'];
        $i++;
                       }
0
Comment
Question by:firekiller15
3 Comments
 
LVL 1

Accepted Solution

by:
binarydesignnz earned 250 total points
ID: 21852528
Is this something like what you want?
<?php
 
 

if($query = mysql_query(select * from student)){

    

    $output = "";

    $seperator = "\n"
 

    while($row = mysql_fetch_array($query)){

        $output .= $row['COMPNUMBER'] .$seperator; 

    }

}  
 

?>

Open in new window

0
 

Author Comment

by:firekiller15
ID: 21852815
the $row['COMPNUMBER'] is assign to $output  as a string?

for example i have 3 record can i out display all the 3 record
say i put code above in function a()
and i make $ouput as object variable
then i create a function called function b()
in function b i do this
echo $this->output can the output display all the 3 record in function b?


 
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 21852839
If you want to save the array as a string so that you can save it, you can serialize it:

 $myarray = array('item'=>'value', 'item2'=>'value');
 $mystring = serialize($myarray);
 echo "Your array looks like this serialized: $mystring<br />";
 $myarray_again = unserialize($mystring);
 echo "If we bring it back to an array, we can print_r: <br />";
 print_r($myarray_again);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now