0

My query

$sql ="select tTrack from tblcourse where tCourseCode = 'AZURE'";
$rs = mysql_query($sql);
$row_rs_course_category = mysql_fetch_assoc($rs);

gives me value Array ( [tTrack] => MS,CLOUD )

but i need to conver that array to this format from PHP

Array ( [0] => MS [1] => CLOUD )

how do i do it in php

4
  • 2
    Try explode(',', $row_rs_course_category['tTrack']); Commented May 15, 2013 at 6:36
  • why dont you just explode the array value and generate a new array.. Commented May 15, 2013 at 6:36
  • 1
    Just to make you aware these functions have been deprecated php.net/manual/en/function.mysql-fetch-assoc.php Commented May 15, 2013 at 6:39
  • 3
    MySQL (mysql_* functions) extension is deprecated. I suggest to use MySQLi (mysqli_* functions) or PDO instead. Commented May 15, 2013 at 6:41

7 Answers 7

5

You can use explode function to get this.

$newValue = explode(",", $row_rs_course_category['tTrack']);
Sign up to request clarification or add additional context in comments.

Comments

2

Use explode().

$value = explode(',',$row_rs_course_category['tTrack'] );
echo "<pre>";
print_r($value);
//Array ( [0] => MS [1] => CLOUD )

Comments

0

I think you can use explode function to achieve this

$array = array ( 'tTrack' => 'MS,CLOUD' );
$array1 = explode(',', $array['tTrack']);
var_dump($array1);

this will output

array(2) { [0]=> string(2) "MS" [1]=> string(5) "CLOUD" } 

Comments

0

explode() can help you

try this

$array = explode("," , $row_rs_course_category['tTrack']);
print_r($array);

Comments

0

You can use PHP explode function: http://php.net/manual/en/function.explode.php

$newArr = explode(',', $row_rs_course_category['tTrack']);
print_r($newArr);

Comments

0

User Php explode() function:

Look on Below link :

http://php.net/manual/en/function.explode.php

$sql ="select tTrack from tblcourse where tCourseCode = 'AZURE'";
$rs = mysql_query($sql);
$row_rs_course_category = mysql_fetch_assoc($rs);
if(isset($row_rs_course_category['tTrack']) && $row_rs_course_category['tTrack'] != '')
{
  $result = explode(',',$row_rs_course_category['tTrack'] );
  echo "<pre>";
  print_r($value);
 }

array explode ( string $delimiter , string $string [, int $limit ] )

delimiter

The boundary string.

string

The input string.

limit

If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.

If the limit parameter is negative, all components except the last -limit are returned.

If the limit parameter is zero, then this is treated as 1.

Comments

0

Switch to sqli, and do what they said explode the array.

$your_array=Array ( 'tTrack' => "MS,CLOUD" );
$new_array=explode(",",$your_array['tTrack']);
print_r($new_array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.