I'm trying to concatenate an array, but when PHP comes across an empty array-item, it stops concatenating.
My array looks like this:
Array
(
[0] => Array
(
[0] => Test1
[1] => Test1
[2] =>
)
[1] => Array
(
[0] => Test2
[1] => Test2
[2] =>
)
[2] => Array
(
[0] => Test3
[1] => Test3
[2] => Test3
)
)
The 3th item on the first 2 Array-items are empty. And when I loop over them like this:
$keys = array('uid', 'type', 'some_column', 'other_column');
foreach ($csv as $i => $row) {
$uid = $row[0] . $row[1] . $row[2];
array_unshift($row, $uid);
$csv[$i] = array_combine($keys, $row);
}
I only get Test3Test3Test3
back, instead of the expected
Test1Test1
Test2Test2
Test3Test3Test3
So it looks like PHP is skipping items when concatenating an empty value.
Is this normal PHP behavior? And if so, how can I tackle this?
Try this:
$uid = array();
foreach ($array as $i => $row) {
$uid[] = $row[0] . $row[1] . $row[2];
}
var_dump($uid);
This outputs:
Array
(
[0] => Test1Test1
[1] => Test2Test2
[2] => Test3Test3Test3
)
You can do something similar to produce a string:
$uid = '';
foreach ($arr as $i => $row) {
$uid .= $row[0] . $row[1] . $row[2] . "
";
}
echo $uid;
Output:
Test1Test1
Test2Test2
Test3Test3Test3
Try like
$uid = array();
foreach ($array as $i => $row) {
$uid[] = $row[0] . $row[1] . $row[2];
}
var_dump($uid);
Just you are giving $uid
and it is taking it as an type variable
and it appends the last occurance of loop into that variable.If you want your desired output you need to declare it as an array
before.
To get the expected output
$uid = "";
foreach ($array as $i => $row) {
$uid .= $row[0] . $row[1] . $row[2] . "
";
}
echo $uid;
I'm sorry, but if that is your desired output, you're overcomplicating things:
$foo = array(
array("Test1","Test1"),
array("Test2","Test2"),
array("Test3","Test3","Test3")
);
echo implode(PHP_EOL,
//implode all child arrays, by mapping, passes no delimiter
//behaves as concatenation
array_map('implode',$foo)
);
Returns:
Test1Test1 Test2Test2 Test3Test3Test3
In your case, you can use bits of this code like so:
$csv = array(array("Test1","Test1",''),array("Test2","Test2",''),array("Test3","Test3","Test3"));
$keys = array('uid', 'type', 'some_column', 'other_column');
foreach($csv as $k => $row)
{
array_unshift($row,implode('',$row));
$csv[$k] = array_combine($keys,$row);
}
gives:
Array ( [0] => Array ( [uid] => Test1Test1 [type] => Test1 [some_column] => Test1 [other_column] => ) [1] => Array ( [uid] => Test2Test2 [type] => Test2 [some_column] => Test2 [other_column] => ) [2] => Array ( [uid] => Test3Test3Test3 [type] => Test3 [some_column] => Test3 [other_column] => Test3 ) )
You need to use two foreach
loop..
Procedure:
key => value as $key=> $value
of each array inside.$value
variable since value inside of this is another array key => value
= $innerKey => $innerValue
.echo '<br/>'
inside your first loop to put each secondary array into another newline.`.
$data = array(
0 => array(
0 => 'Test1',
1 => 'Test1',
2 => ''
),
1 => array(
0 => 'Test2',
1 => 'Test2',
2 => ''
),
2 => array(
0 => 'Test3',
1 => 'Test3',
2 => 'Test3'
)
);
foreach($data as $key => $value){
foreach($value as $innerKey => $innerValue){
echo $innerValue;
}
echo '<br/>';
}
// Output would be..
Test1Test1
Test2Test2
Test3Test3Test3
Code Tested at : PHP Fiddle