Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

String vs Array

Posted on 2008-06-23
3
Medium Priority
?
256 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…
Suggested Courses

782 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