This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I'm trying to loop through an array that I got from preg_match_all result in order to create one string from all results.
Array looks like this:
print_r($matches[0]);
Array
(
[0] => Array
(
[0] => 8147
[1] => 3
)
[1] => Array
(
[0] => 8204
[1] => 20
)
)
And my code:
$found = count($matches[0]);
for ($i = 0; $i <= $found; $i++) {
$string = $matches[0][$i];
}
I would like to get result of $string like this: 8147, 8204.
How I can append $matches[0][0] to $matches[0][1] etc. in string variable using loop?
You can do this some ting like that
$string = "";
foreach($matches[0] as $value) {
$string .= $value[0].", ";
}
$string = rtrim(", ",$string);
With php5.5 and more you can use array_column + implode:
echo implode(', ', array_column($matches, 0));
Try following code. Loop through array and get values
$arr =Array
(
0 => Array
(
0 => 8147,
1 => 3
),
1 => Array
(
0 => 8204,
1 => 20
)
);
$match_array = array();
foreach($arr as $key=>$value)
{
$match_array[] = $value[0];
}
$str = implode(",",$match_array);
echo $str;
DEMO
OR simply use array_column to get specific column as array then use implode
$arr =Array
(
0 => Array
(
0 => 8147,
1 => 3
),
1 => Array
(
0 => 8204,
1 => 20
)
);
$match_array = array_column($arr,0);
$str = implode(",",$match_array);
echo $str;
DEMO
You can use array_column, no need to loop over the array
$result = join(',', array_column($arr, 0));
Related
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 am using codeigniter geting comma separated values but not working fine. i am sharing all code like first output foreach after gating result not show comma separated values than second show array print value aftra need output comma separated values like SLR,ACCN
Model code here
public function display_coach_name($coachID='')
{
$db2 = $this->load->database('rail',TRUE);
$ids = explode(',',$coachID);
$db2->select('coach_name');
$db2->from('mcc_coach');
$db2->where_in('id',$ids);
$query = $db2->get();
//echo $db2->last_query(); die;
if ($query->num_rows() > 0):
return $query->result_array();
else:
return 0;
endif;
}
output
<?php foreach ($coachname as $val){ echo $pizza = $val['coach_name']; }?>
//---------------------foreach-----------------output---
SLRACCN
array print
$coachname = $this->rail_ceil_model->display_coach_name($coachID);
echo"<pre>";
print_r($coachname);
//---------------------output----------------------
Array
(
[0] => Array
(
[coach_name] => GS
)
[1] => Array
(
[coach_name] => SLR
)
)
Array
(
[0] => Array
(
[coach_name] => GS
)
[1] => Array
(
[coach_name] => SLR
)
)
Array
(
[0] => Array
(
[coach_name] => GS
)
[1] => Array
(
[coach_name] => SLR
)
)
Array
(
[0] => Array
(
[coach_name] => GS
)
[1] => Array
(
[coach_name] => SLR
)
)
I have need output
SLR,ACCN
You can use implode function for converting array into comma separated values
$coachname = $this->rail_ceil_model->display_coach_name($coachID);
foreach($coachname as $val){
if(is_array($val)){
$list .= $val['coach_name'].',';
}}
print_r(substr ( $list , 0 , strlen($list) -1 ));
or
$coachname = $this->rail_ceil_model->display_coach_name($coachID);
print_r(implode(', ', array_map('coach_name', $coachname)));
or
echo implode(', ', array_column($coachname, 'coach_name'));
You may use implode function for getting comma separated values.
<?php
$coachname = array("ACCN", "SLR", "CN");
$new = implode(',',$coachname);
echo $new;
?>
Output will be:
ACCN,SLR,CN
Edit your code accordingly.
Please use array_map function then use implode function
$coachname_separated = array_map (function($value){
return $value['coach_name'];
} , $coachname);
$List = implode(', ', $coachname_separated);
echo $List;
I just tried to parse my array that contains numbers separated with comma into numbers without the comma, but still in array form. But it didn't work.
My code:
$total = $this->input->post('total');
$arrTot = array_filter(array_slice($total, 20));
print_r($arrTot);
Array result:
Array
(
[0] => 10,000
[1] => 100,000
[2] => 200,000
)
My desired output was to erase the comma in all number:
Array
(
[0] => 10000
[1] => 100000
[2] => 200000
)
I've tried with something just like this but it seems not even close with my desired output:
$total = $this->input->post('total');
$arrTot = array_filter(array_slice($total, 20));
for ($i=0; $i < count($arrTot); $i++) {
$valTot=str_replace( ',', '', $arrTot[$i]);
print_r($valTot);
}
Is there any way to solve this problem?
Thanks.
You can use array_walk to process each of the values in the array:
$arrTot = array('10,000', '100,000', '200,000');
array_walk($arrTot, function (&$v) {
$v = str_replace(',', '', $v);
});
print_r($arrTot);
Output:
Array
(
[0] => 10000
[1] => 100000
[2] => 200000
)
Demo on 3v4l.org
you might assign new value to current variable.
$arrTot = array_filter(array_slice($total, 20));
for ($i=0; $i < count($arrTot); $i++) {
$arrTot[$i]=str_replace( ',', '', $arrTot[$i]);
}
print_r($arrTot);
If you want the desired output, you need to replace the elements in the main array without comma.
$total = $this->input->post('total');
$arrTot = array_filter(array_slice($total, 20));
foreach ($arrTot as $key => $aTot) {
$arrTot[$key] = str_replace(',','',$arrTot[$i);
}
var_dump($arrTot);
Try this-
echo "<pre>";
$arr = array('10,000','100,000','200,000');
print_r($arr);
//result
Array
(
[0] => 10,000
[1] => 100,000
[2] => 200,000
)
foreach ($arr as $key => $value) {
$new[] = str_replace(',','',$value);
}
print_r($new);
Array
(
[0] => 10000
[1] => 100000
[2] => 200000
)
try this ,
$arr = ['10,000','100,000','200,000'];
foreach($arr as $key=>$val){
$arr[$key] = (int)str_replace(',','',$val);
}
var_dump($arr);
You could simply use str_replace to achieve desired result
$arrTot = array('10,000', '100,000', '200,000');
foreach($arrTot as $key => $value){
$arrTot[$key] = str_replace(",","",$value);
}
print_r($arrTot);
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 this return array
$leaseArray = Array ( [0] => Array ( [LeaseNumber] => OL/2011/0343 ) [1] => Array ( [LeaseNumber] => 184 ) [2] => Array ( [LeaseNumber] => OL/2011/0118 ) [3] => Array ( [LeaseNumber] => OL/2016/1759 ) [4] => Array ( [LeaseNumber] => OL/2013/0858 ) [5] => Array ( [LeaseNumber] => OL/2012/0535 ) [6] => Array ( [LeaseNumber] => OL/2017/2208 ) [7] => Array ( [LeaseNumber] => 2355 ) )
I want to save all the values of LeaseNumber in to one comma separated string
like $string = "OL/2011/0343 , 184 , OL/2011/0118 , OL/2016/1759"
please help me
$lease = array();
for($i=0;$i<=count($leaseArray); $i++){
$lease = $leaseArray[$i]['LeaseNumber'];
}
If you want a one-liner then this is the way
$csv = implode(' , ', array_column($a, 'LeaseNumber'));
As I said in the comments, 1 line, 2 function calls.
If you want a one-liner then this could work:
$csv = implode( ' , ', array_map( function( $a ){ return $a[ 'LeaseNumber' ]; }, $leaseArray ) );
expanded:
$csv = implode( ' , ', // step 2: implode on ' , ': space-comma-space
array_map( // step 1: pass $lease array into array_map so that we can get a new array
function( $a ){ return $a[ 'LeaseNumber' ]; }, // Accept each array in $a and only return the LeaseNumber. array_map will build a new array out of just these values
$leaseArray // the array to be processed
)
);
In essence, this is the same as:
$csv = implode( ' , ', array_column( $a, 'LeaseNumber' ) );
but array_map() allows you to transform the data before output/return if you need to.
One-liners are great but sometimes hard to read. If you want to stick with what you have here are some note
<?php
$array = array(array("LeaseNumber" => "OL/2011/0343"), array("LeaseNumber"=> 184 ), array("LeaseNumber"=> "OL/2011/0118") , array("LeaseNumber"=> "OL/2016/1759"), array("LeaseNumber"=> "OL/2013/0858"), array("LeaseNumber"=> "OL/2012/0535"), array("LeaseNumber"=> "OL/2017/2208"), array("LeaseNumber"=> 2355));
$lease = array();
//Not equal to the count it starts at 1 not 0
for($i=0;$i<count($array); $i++){
//lease is an array add to the index not overwrite
$lease[] = $array[$i]['LeaseNumber'];
}
//You needed to finish with this
$csv = implode(", ",$lease);
echo $csv;
As an fyi switch to a foreach in this case makes life easier:
$lease = array();
foreach($array as $obj){
$lease[] = $obj["LeaseNumber"];
}
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 this kind of array in my $tag variable.
Array
(
[0] => Array
(
[tag_name] => tag-1
)
[1] => Array
(
[tag_name] => tag-2
)
[2] => Array
(
[tag_name] => tag-3
)
)
What I'm trying to do is get all the tag names and implode them with a coma then make it a value for a text input field.
I've tried for and foreach loops so many different ways but with not much success. I'm using CodeIgniter if that helps.
You can use array_column followed by join or implode
Try this :
$string = join(',', array_column($array, 'tag_name'));
Explanation:
array_column returns the values from a single column from the input array
For your array, array_column($array 'tag_name') returns an array containing values of index tag_name, i.e returned array would be :
Array
(
[0] => tag-1
[1] => tag-2
[2] => tag-3
)
Joining with join or implode , you get your desired string,
//$string = "tag-1,tag-2,tag-3"
A simple and obvious solution might be:
$res = "";
for ($i = 0; $i < count($tag); $i++) {
$res .= $tag[$i]["tag_name"] . ",";
}
$res = trim($res, ","); //Removing the extra commas
echo $res;
You basically iterate through the array, and every element you iterate through, you add it's tag_name to a $res string.
Use array_column
$tag = implode(', ', array_column($array, 'tag_name'));
Using array_map:
$tag = implode(', ', array_map(function ($tag) {
return $tag['tag_name'];
}, $array));
Simple one liner !!
$array = [
[
"tag_name" => 'tag-1'
],
[
"tag_name" => 'tag-2'
],
[
"tag_name" => 'tag-3'
],
];
implode(',', array_column($array, 'tag_name'));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php values of one array to key of another array
i have given two array here.
Array-1
(
[0] => 6
[1] => 11
[2] => 13
)
Array-2
(
[0] => 13.339066309
[1] => 0
[2] => 100
)
I want to replace value of one array to key of another array. something like this:
Array
(
[6] => 13.339066309
[11] => 0
[13] => 100
)
Use array_combine:
$new_array = array_combine($array1, $array2);
take a look at array_combine()
$result = array_combine(array_values($firstArr), array_values($secondArr));
Try this:
$result = array();
foreach ($array1 as $key => $value) {
$result[$value] = $array2[$key];
}
Try something like this
$t = array();
$keys = array_keys($arr1);
for ($i = 0; $i < min(count($keys), count($arr2)); $i++) {
$t[$keys[$i]] = $arr2[$i];
}