convert array to one variable with multiple values [duplicate] - php

This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
here is my array .
$myarray = Array
(
[0] = Array
(
[name] = 17
)
[1] = Array
(
[name] = 18
)
[2] = Array
(
[name] = 19
)
)
I want myvar to return this '17,18,19'
$var = '17,18,19';

You can use array_map;
$temp = array_map(function($i){return $i['name'];}, $myarray);
$output = implode(',', $temp);

You can do it in many ways. One way to do it with array_column() and implode()
<?php
$myarray = array(array('name' => 17),array('name' => 18),array('name' => 19));
$one_d = array_column($myarray, 'name');
echo implode(',',$one_d);
?>
DEMO: https://3v4l.org/rCmKR

A simple foreach could also do the trick.
$var = '';
foreach ($myarray as $value) {
$var .= $value['name'].',';
}
$var = substr($var, 0, -1);
echo $var; // 17,18,19

Related

Implode Multidimensional Array Keys and Values [duplicate]

This question already has answers here:
Return single column from a multi-dimensional array [duplicate]
(7 answers)
How to implode array indexes?
(1 answer)
Closed 3 years ago.
I have an array that I am trying to convert into two strings, one with dates and one with the data values. This is a sample array:
[$output Array below]
Array
(
[2019-03-19] => Array
(
[data_values] => 1566
)
[2019-03-18] => Array
(
[data_values] => 1542
)
[2019-03-17] => Array
(
[data_values] => 786
)
[2019-03-16] => Array
(
[data_values] => 756
)
)
A desired output would be something like:
$dates = '2019-03-19,2019-03-18,2019-03-17,2019-03-16';
$data_values = '1566,1542,786,756';
I've tried this, which will give me the data_values but I can't get the dates, I assume because its the array key?
function implode_r($g, $p) {
return is_array($p) ?
implode($g, array_map(__FUNCTION__, array_fill(0, count($p), $g), $p)) :
$p;
}
$data_values = implode_r(',', $output);
echo $data_values;
You can just use array_keys and array_column:
$dates = implode(',', array_keys($output));
$data_values = implode(',', array_column($output, 'data_values'));
Demo on 3v4l.org
This will give you the answer you expect:
<?php
// Build strings based on array
function implode_r($output) {
$dates = $data_values = "";
if(is_array($output)){
$dates=implode(',',array_keys($output));
$data_values=implode(',',array_column($output, 'data_values'));
}
return compact('dates', 'data_values');
}
// Example usage
$output = [
"2019-03-17" => ["data_values" => 1],
"2019-03-18" => ["data_values" => 2],
"2019-03-19" => ["data_values" => 3],
"2019-03-20" => ["data_values" => 4]
];
$result = implode_r($output);
echo $result['dates'];
echo $result['data_values'];

How to join a column of values with comma then space? [duplicate]

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));

Remove string start from certain character? [duplicate]

This question already has answers here:
PHP substring extraction. Get the string before the first '/' or the whole string
(14 answers)
Closed 5 years ago.
For example I have an array
$array[] = ['name - ic'];
The result I want is
$new_Array[]=['name'];
How do I remove the string that start from the - since the name and ic will be different for everyone? Anyone can help?
Using explode method you can split string into array.
PHP
<?php
$array[] = ['name - ic'];
$array[] = ['name - bc - de'];
$new_Array = array();
foreach($array as $key=>$values){
foreach($values as $k=>$val){
$str = explode("-",$val);
$new_Array[$key][$k] = trim($str[0]);
}
}
?>
OUTPUT
Array
(
[0] => Array
(
[0] => name
)
[1] => Array
(
[0] => name
)
)
You can use explode function for the same.
$array = array('name - ic','abc - xyz','pqr-stu');
$newArray = array();
foreach($array as $obj):
$temp = explode('-',$obj);
$newArray[] = trim($temp[0]);
endforeach;
print_r($newArray);
Result
Array ( [0] => name [1] => abc [2] => pqr )
Let me know if it not works.

Getting numbers only from an array [duplicate]

This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 6 years ago.
i have this array in php
$name = array("page18.jpg","page16.jpg","page17.jpg","page19.jpg",);
i wanted to strip all alphabets and dots in the string remains only number like
$name = array("18","16","17","19");
how do i achieve that can some one please guide me how to do it in PHP language
Try this,use preg_replace
$name = array("page18.jpg","page16.jpg","page17.jpg","page19.jpg",);
foreach($name as $val)
{
$new_name[] = preg_replace('/\D/', '', $val);
}
DEMO
You can do it without using Regex by using string replace function (str_replace()):
<?php
$name = array("page18.jpg","page16.jpg","page17.jpg","page19.jpg",);
$myArra = array();
foreach ($name as $key => $value) {
$var = str_replace("page", "", $value);
$var = str_replace(".jpg", "", $var);
$myArra[] = $var; // store all value without text and .jpg
}
print_r($myArra);
?>
Result:
Array
(
[0] => 18
[1] => 16
[2] => 17
[3] => 19
)
Or, if you want to use Regex, than you can use /\d+/ pattern as like:
<?php
$name = array("page18.jpg","page16.jpg","page17.jpg","page19.jpg",);
$myArra = array();
foreach ($name as $key => $value) {
preg_match_all('/\d+/', $value, $matches);
$myArra[] = $matches[0][0]; // store all value without text and .jpg
}
echo "<pre>";
print_r($myArra);
?>
Result:
Array
(
[0] => 18
[1] => 16
[2] => 17
[3] => 19
)
Aside from using str_replace() and preg_replace(), you can use array_map() and filter_val() like so:
$name = array("page18.jpg","page16.jpg","page17.jpg","page19.jpg",);
$name = array_map(function($fv) {
return filter_var($fv, FILTER_SANITIZE_NUMBER_INT);
}, $name);
print_r($name);
Output:
Array
(
[0] => 18
[1] => 16
[2] => 17
[3] => 19
)
A very simplified and native solution from the house of PHP:
$name = array("page18.jpg","page16.jpg","page17.jpg","page19.jpg",);
foreach ($name as $n) {
$num_arr[] = filter_var($n, FILTER_SANITIZE_NUMBER_INT);
}
var_dump($num_arr);
exit();
Outputs:
Array ( [0] => 18 [1] => 16 [2] => 17 [3] => 19 )
enjoy fresh air ;)
<?php
$name = array ("page18.jpg","page16.jpg","page17.jpg","page19.jpg");
$myarray =array();
foreach($name as $key =>$value){
$page_replace= str_replace("page", "",$value); //replace page as a blank
$jpg_replace= str_replace(".jpg","",$page_replace); //replace jpg to blank
$myarray[] =$jpg_replace;//stroing without jpg and page getting resulut as key value pair
}
echo "<pre>";
print_r($myarray);
?>

How to combine these two array into one array using php? [duplicate]

This question already has answers here:
Merge two arrays as key value pairs in PHP
(3 answers)
Closed 7 years ago.
Given these two array:
$name=array("alice","ken","wendy");
$frequent=array(3,6,9);
I try to combine it like
$data = array($name=>$frequent);
but it fails. Anyone can help?
I want this:
$data = array(
'alice' => 3,
'ken' => 6,
'wendy' => 9,
);
You can use array_combine
$combined_array = array_combine($name, $frequent);
Documentation here: http://php.net/manual/en/function.array-combine.php
You can use array_combine function as
Syntax:
array_combine ( array $keys , array $values )
So yours is like
$name=array("alice","ken","wendy");
$frequent=array(3,6,9);
$result = array_combine($name,$frequent);
Output
Array
(
[alice] => 3
[ken] => 6
[wendy] => 9
)
[akshay#localhost tmp]$ cat test.php
<?php
$name=array("alice","ken","wendy");
$frequent=array(3,6,9);
// One easy way is
print_r( array_combine($name, $frequent) );
// Another lengthy way
while ( ($key = array_shift($name)) && ($value = array_shift($frequent)) )
{
$combined[$key] = $value;
}
print_r( $combined );
?>
Output
[akshay#localhost tmp]$ php test.php
Array
(
[alice] => 3
[ken] => 6
[wendy] => 9
)
Array
(
[alice] => 3
[ken] => 6
[wendy] => 9
)
write like this $combined_array = array_combine($name, $frequent);
If you want to do it manually.
<?php
$name=array("alice","ken","wendy");
$frequent=array(3,6,9);
$combined=array();
for($i=0; $i<3; $i++)
{
$combined[$name[$i]]=$frequent[$i];
}
var_dump($combined);
?>

Categories