The implode function is not worked!

I want to show the string as follows;

id-101-102-103-104-105

$handle = fopen($_FILES['file']['tmp_name'], "r");
while(!feof($handle)) {
	$data = fgets($handle);
	$listing = explode(",", $data);	
	$id = $listing[0];
	$name=$listing[1];  // this is second column data.
	$telephone = $listing[2];
	$email = $listing[3];

	$id1 = implode("-,$id);
	
	echo "id = " . $id . "<br />";

Recommended Answers

All 17 Replies

You forgot a " on line 10

$id1 = implode("-", $id);

and $id isn't an array but a single value so you can't implode that (implode takes an array).

please check this line of code $id1 = implode("-,$id);


try this one

$id1 = implode("-",$id);

It cannot work? Could anyone help!


Since the data is from a csv file, i just get the data from one column. "$id" is id101102103104105, i want to chsnge it to id-101-102-103-104-105

Anyone can help!

implode works on array not on string
so you need to replace spaces with hyphen.

I am assuming only one space between all ids

$id1 = str_replace(" ","-", $id);

But i have no space in this? How can i do ?

Member Avatar for diafol

Are all numbers 3 digit? Trivial solution:

$str = "id101102103104105";
$out = str_split(substr($str,2), 3);
echo "id-" . implode("-",$out);

Works for me if 'id' always the prefix and all numbers are 3 digit.

Why the result is

id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105

Actually, i import the csv file and then i want to get the particular cell value? (coluumn one row three). I know how to get all the column value (first column value) as previously mentioned, but it is data set. But i don't know how to get the value of row 3 in column value data set? So i need to seperate the column value to get what i want?

I upload the file for you to check! Thanks all


The csv file as below:

id name telephone email
1000001 A 2323232
1000002 B 2323232
1000003 C 2323232
1000004 D 2323232
1000005 E 2323232
1000006 F 2323232
1000007 G 2323232
1000008 H 2323232
1000009 I 2323232
1000010 J 2323232

Member Avatar for diafol

OK, this is a slightly different question to the one you originally asked. Your file has mysql details. DO you need to involve a DB? The code does not involve a DB.

If you want to access cols/rows data from csv, you need to create a multidimensional array, like:

array[row][col]

$str = file_get_contents($file);
$lines = explode("\n",$str);
foreach($lines as $line){
 $array[] = explode(" ",$line); 
}

Not tested - so you may need to fiddle with it. You could build with

function getRowCol($array,$row,$col){
  return $array[$row][$col];
}

I don't know how to use it! Could you give me more support!

<?php


$str = file_get_contents('./info.csv', true);
$lines = explode("\n",$str);
foreach($lines as $line){
 $array[] = explode(",",$line);
}




function getRowCol($array,$row,$col){
  return $array[$row][$col];
}

getRowCol($array,3,1);


?>
for(){
 if (condition) {
           $errors[] = 'something.';
    }
}

echo implode('<br/>',$errors);

$errors contains multiple data or indexed value if you want to show that error in a next line then user implode. hope u understand

sorry , i just want to know what is the error ?

<?php


$str = file_get_contents('./info.csv', true);
$lines = explode("\n",$str);
foreach($lines as $line){
 $array[] = explode(",",$line);
}




function getRowCol($array,$row,$col){
  return $array[$row][$col];
}

getRowCol($array,3,1);


?>

i just want to get the value of specific cell value from the imported csv. Anyone can help ! Can anyone provide the code?

What is the mistake about the code?

<?php


$str = file_get_contents('./info.csv', true);
$lines = explode("\n",$str);
foreach($lines as $line){
 $array[] = explode(",",$line);
}




function getRowCol($array,$row,$col){
  return $array[$row][$col];
}

getRowCol($array,3,1);


?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.