user1788542
user1788542

Reputation:

php array to string conversion does not work

I am reading content from xls file and storing each value in string seperating by ,

Here is my code:

$xlsx = new SimpleXLSX('test.xlsx');
echo '<h1>$xlsx->rows()</h1>';
echo '<pre>';
print_r( $xlsx->rows() );
echo '</pre>';
$result = array();
$result = $xlsx->rows();
var_dump($result);
$result=implode(",",$result);
echo $result;

NOw:

print_r( $xlsx->rows() ); gives

Array
(
    [0] => Array
        (
            [0] => test
            [1] => karim
        )

)

var_dump($result); gives

array (size=1)
  0 => 
    array (size=2)
      0 => string 'test' (length=4)
      1 => string 'karim' (length=5)

next line

echo $result; gives error

Notice: Array to string conversion in

what's wrong here?

Upvotes: 0

Views: 110

Answers (2)

r3wt
r3wt

Reputation: 4742

this should implode each row into a comma separated string in a new array, called result2

$xlsx = new SimpleXLSX('test.xlsx');
echo '<h1>$xlsx->rows()</h1>';
echo '<pre>';
print_r( $xlsx->rows() );
echo '</pre>';
$result = array();
$result = $xlsx->rows();
var_dump($result);
$result=
//echo $result;
$rows = count($result);
$result2 = array();
for($i = 0; $i < $rows; $i++)
{
    $result2[$i] = implode(',',$result[$i]);
}
foreach($result2 as $value)
{
    echo $value.'<br/>';
}

Upvotes: 0

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

try

$result=implode(",",$result[0]);

Upvotes: 2

Related Questions