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 />";

Dani AI

Generated

Quick diagnosis and a simple, reliable approach.

As and pointed out, the original line had a syntax error and the bigger issue is a type: implode() expects an array, not a single string. The replies show two different situations: (A) the CSV contains one id per row (the usual case), or (B) there is a single concatenated string like id101102103.... The solutions differ — the following gives a robust, production-friendly way for each and explains why the repeated output was happening.

For a normal CSV (one id per row) the recommended flow is: parse the file with fgetcsv() (handles quoted fields and delimiters), collect the first-column values into an array, then call implode() once — echoing outside the loop so the string is not printed repeatedly:

<?php
$ids = [];
if (($f = fopen($_FILES['file']['tmp_name'], 'r')) !== false) {
    $header = fgetcsv($f);            // skip header row if present
    while (($row = fgetcsv($f)) !== false) {
        if (isset($row[0]) && trim($row[0]) !== '') {
            $ids[] = trim($row[0]);
        }
    }
    fclose($f);
}
if ($ids) {
    echo 'id-' . implode('-', $ids);
}
?>

If the starting point really is one long, delimiter-free string like id101102103104105 (as addressed with fixed-width chunking), a regex insertion avoids an extra trailing dash and works when the numeric groups are fixed-width:

$s = 'id101102103104105';
if (preg_match('/^([A-Za-z]+)(\d+)$/', $s, $m)) {
    $grouped = preg_replace('/(\d{3})(?=\d)/', '$1-', $m[2]); // insert dashes every 3 digits
    echo $m[1] . '-' . $grouped;
}

Notes and pitfalls: do not echo inside the read loop (that causes the repeated lines seen in the thread), trim values, skip or detect the header, and use fgetcsv() for robust parsing. For a single cell (third row, first column) use zero-based indexing after building the rows array (e.g. the third id is $ids[2]).

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 Member #120589

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 Member #120589

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.