Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

String vs Array

Posted on 2008-06-23
3
Medium Priority
?
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

660 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