This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
This is my array
Array
(
[0] => Array
(
[0] => SC1MTTCS6J1WK
)
[1] => Array
(
[0] => SC1MTTCSHJ1WK
)
)
But when I try to implode them using
$in_text = implode(",", $myArray3);
I can't get the value instead I got this:
Array,Array
Please assist thank you.
Here is the simple code, try this
$in_text = implode(',',array_map('implode',$myArray3));
echo $in_text;
Try this code,
$in_text = '';
foreach ($myArray3 as $key=>$val){
if(is_array($val)) {
$in_text .= ($in_text != '' ? ',' : ''). implode(",", $val);;
} else {
$in_text .= ($in_text != '' ? ',' : ''). $val;
}
}
echo $in_text;
PS: This will work upto two dimensional array.
You call implode on an array of array, which result the Array, Array. You can call like this,
implode(",", array_column($myArray3, 0));
Related
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 6 years ago.
Hi I have an array just like below
$arr = array ( [0] => Array ( [allergy] => test ),[1] => Array ( [allergy] => test1 ) );
Here from that array I just want allergy value as comma separated string like test,test1
I tried implode but it's not working
$arr = array ( [0] => Array ( [allergy] => test ),[1] => Array ( [allergy] => test1 ) );
$str = implode (", ", $arr);
echo $str;
here is my sample
//array_column will work from php version 5.5,
$arr = array ( '0' => Array ( 'allergy' => 'test' ),'1' => Array ( 'allergy' => 'test1' ) );
$str = '';
foreach($arr as $row){
$str .=$row['allergy'].',';
}
$str = trim($str,',');
echo $str;
You can use array_column() for that, and then use implode() to comma separated string.
Your code might look something like this,
$arr = array (array ('allergy' => 'test'),array ('allergy' => 'test1') );
$arr=array_column($arr,"allergy");
$str = implode (",", $arr);
echo $str;
array_column() returns the values from a single column of the input,
identified by the second parameter(column_key).
Demo: https://eval.in/620454
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I have been searching for an answer to a PHP code problem. While it may sound easy to some users, I am having problem below:
I managed to retrieve data from a particular table with PHP and MySql. Unfortunately, I am unable to display result as a string rather than array.
I used print_r($loggedin_users).
Result:
Array ( [0] => Array ( [0] => Test ) [1] => Array ( [0] => Test1 ) )
I have tried using implode function to return me a string.
Result:
ArrayArray
May I know how do I get a result as below?
Desired result:
Test; Test1
Thank you in advance.
The problem is, that you have a two dimensional array. So you are trying to implode two arrays, which can't work. So you first have to implode the subArrays and then implode it again, e.g.
echo implode(";", array_map("implode", $loggedin_users));
Side note:
If you would have error reporting turned on you would have got a notice, saying:
Notice: Array to string conversion
You can use array_reduce():
echo array_reduce($array, function($carry, $item) {
if(is_null($carry)) {
return $item[0];
} else {
return $carry . "; " . $item[0];
}
});
Use foreach them use implode because you have multidimensional array.
foreach($loggedin_users as $key => $val){
$string = implode(', ', $val);
}
You have to create a recursive function here. so, no matter what array it is and no matter upto what extent that is nested. you'll always get desired result.
$a = array(
0 => array(0 => 'Test'),
1 => array(0 => 'Test1')
);
function implodeCustom($array){
$string = "";
foreach($array as $key => $value)
if(is_array($value)){
$string .= implodeCustom($value);
} else{
$string .= $value.";";
}
return $string;
}
echo rtrim(implodeCustom($a),';');
This question already has answers here:
How to remove empty values from multidimensional array in PHP?
(9 answers)
Closed 9 years ago.
I have this array:
$aryMain = array(array('hello','bye'), array('',''),array('',''));
It is formed by reading a csv file and the array('','') are the empty rows at the end of the file.
How can I remove them?
I've tried:
$aryMain = array_filter($aryMain);
But it is not working :(
Thanks a lot!
To add to Rikesh's answer:
<?php
$aryMain = array(array('hello','bye'), array('',''),array('',''));
$aryMain = array_filter(array_map('array_filter', $aryMain));
print_r($aryMain);
?>
Sticking his code into another array_filter will get rid of the entire arrays themselves.
Array
(
[0] => Array
(
[0] => hello
[1] => bye
)
)
Compared to:
$aryMain = array_map('array_filter', $aryMain);
Array
(
[0] => Array
(
[0] => hello
[1] => bye
)
[1] => Array
(
)
[2] => Array
(
)
)
Use array_map along with array_filter,
$array = array_filter(array_map('array_filter', $array));
Or just create a array_filter_recursive function
function array_filter_recursive($input)
{
foreach ($input as &$value)
{
if (is_array($value))
{
$value = array_filter_recursive($value);
}
}
return array_filter($input);
}
DEMO.
Note: that this will remove items comprising '0' (i.e. string with a numeral zero). Just pass 'strlen' as a second parameter to keep 0
Apply array_filter() on the main array and then once more on the inner elements:
$aryMain = array_filter($aryMain, function($item) {
return array_filter($item, 'strlen');
});
The inner array_filter() specifically uses strlen() to determine whether the element is empty; otherwise it would remove '0' as well.
To determine the emptiness of an array you could also use array_reduce():
array_filter($aryMain, function($item) {
return array_reduce($item, function(&$res, $item) {
return $res + strlen($item);
}, 0);
});
Whether that's more efficient is arguable, but it should save some memory :)
This question already has answers here:
How to insert an item at the beginning of an array in PHP?
(8 answers)
Closed 1 year ago.
I tried with array_push(), but I get fatal error.
function get_data($table, $id = '', $condition){
if($id != '')
array_push( " WHERE `id` = '".$id."' ", $condition );
...
}
The question is, how to add a value (in my case a string) to the start of an array?
array_unshift() is the function you are looking for!
array_unshift — Prepend one or more elements to the beginning of an array
$arr = array(1,2,3);
print_r($arr);
/*
Array
(
[0] => 1
[1] => 2
[2] => 3
)
*/
array_unshift($arr,0);
print_r($arr);
/*
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
*/
The fatal error is because you have the arguments in reverse order:
function get_data($table, $id = '', $condition){
if($id != '')
array_push($condition, " WHERE `id` = '".$id."' " );
...
}
if $condition is an array, this will not give a fatal error, but it will place the item at the end of the array. As mentioned in other answers, array_unshift is the function to prepend an item.
array_unshift should do the trick
This question already has answers here:
Create a comma-separated string from a single column of an array of objects
(15 answers)
Closed 7 months ago.
I've got an array like this:
Array
(
[0] => Array
(
[name] => Something
)
[1] => Array
(
[name] => Something else
)
[2] => Array
(
[name] => Something else....
)
)
Is there a simple way of imploding the values into a string, like this:
echo implode(', ', $array[index]['name']) // result: Something, Something else, Something else...
without using a loop to concate the values, like this:
foreach ($array as $key => $val) {
$string .= ', ' . $val;
}
$string = substr($string, 0, -2); // Needed to cut of the last ', '
Simplest way, when you have only one item in inner arrays:
$values = array_map('array_pop', $array);
$imploded = implode(',', $values);
EDIT: It's for version before 5.5.0. If you're above that, see better answer below :)
In PHP 5 >= 5.5.0
implode(', ', array_column($array, 'name'))
You can use a common array_map() trick to "flatten" the multidimensional array then implode() the "flattened" result, but internally PHP still loops through your array when you call array_map().
function get_name($i) {
return $i['name'];
}
echo implode(', ', array_map('get_name', $array));