I have an array of the following format:
$var = Array
(
[0] => Array
(
[name] => Harry
)
[1] => Array
(
[name] => Wayne
)
)
Array
(
[0] => Array
(
[name] => Wayne
)
I want to implode this array such that i get it in the format:
Harry,Wayne
Wayne
From What I have Tried I am getting it in format:
Harry,Wayne
Harry,Wayne,Wayne
What I have Tried (Not important as its wrong)
foreach($var as $a){
foreach($a as $b){
}$c[] = $b
}
$imp = implode(',',$c);
$var is fetched from database using fetch_array.
$this->db->select('name');
$this->db->where('id', $Id);
$this->db->from('info');
$row = $this->db->get();
$var = $row->result_array();
where $Id is array containing certain user ids.
foreach($var as $a)
{
unset($temp);
foreach($a as $b)
{
$temp[] = $b['name'];
}
$c[] = implode(",", $temp);
}
// output all the names
foreach ($c as $csvNames)
{
echo $csvNames;
}
Try this.
foreach($var as $a){
$m = '';
$delim = '';
foreach($a as $k){
$m .= $delim . $k['name'];
$delim = ',';
}
$c[] = $m;
}
foreach($c as $d){
echo $d;
}
Please ignore those hard-coded loops. There is a recursive function for it.
array_walk_recursive($var, create_function('$val, $key', 'array_push($obj, $val);'), &$output);
echo implode(",",$output);
Related
I wish to group a word list in an array with the initial letter.
function alpha($str) {
$result[substr($str,0,1)] = $str;
return $result;
}
$a = ['abc','cde','frtg','acf'];
$b = array_map('alpha', $a);
print_r($b);
What I need:
Array
(
[a] => abc,acf
[c] => cde
[f] => frtg
)
What I get:
Array
(
[0] => Array
(
[a] => abc
)
[1] => Array
(
[c] => cde
)
[2] => Array
(
[f] => frtg
)
[3] => Array
(
[a] => acf
)
)
How about that :
$answer = [];
$a = ['abc','cde','frtg','acf'];
foreach($a as $word){
$key = substr($word,0,1);
if (isset($answer[$key])){
$answer[$key] .= "," . $word;
} else {
$answer[$key] = $word;
}
}
Just add a variable $c and loop over arrays of array using two foreach and group by alphabet...
function alpha($str) {
$result[substr($str,0,1)] = $str;
return $result;
}
$a = ['abc','cde','frtg','acf'];
$b = array_map('alpha', $a);
#print_r($b);
$c = [];
foreach ($b as $key => $values) {
foreach ($values as $key => $value) {
if(!isset($c[$key])){
$c[$key]=$value;
}else{
$c[$key].= "," . $value;
}
}
}
echo "<PRE>";
print_r($c);
Outupt:
Array
(
[a] => abc,acf
[c] => cde
[f] => frtg
)
The function array_map maps to the original indexes but you want new indexes and an altered array, if there are more values with the same initial character. Therefore array_map don't work for you. You could create your new array this way:
$a = ['abc','cde','frtg','acf'];
$b = Array();
$c = Array();
foreach( $a as $v )
{
// multidimensional array
$b[substr($v,0,1)][] = $v;
// comma separated string
$c[substr($v,0,1)] = (isset($c[substr($v,0,1)])) ?
$c[substr($v,0,1)].",$v" : $v;
}
If the first character can also be multibyte Unicode such as ° or €, mb_substr() must be used! Solution with foreach:
$result = [];
$a = ['abc','€de','frtg','acf'];
foreach($a as $word){
$key = mb_substr($word,0,1);
$result[$key] = array_key_exists($key,$result)
? ($result[$key].",".$word)
: $word
;
}
Solution with array_reduce():
$result = array_reduce($a,function($carry,$item){
$key = mb_substr($item,0,1);
$carry[$key] = array_key_exists($key,$carry) ? ($carry[$key].",".$item) : $item;
return $carry;
},[]);
var_export($result);
Output:
array (
'a' => 'abc,acf',
'€' => '€de',
'f' => 'frtg',
)
I think your intention is to store each word into arrays according to its first letter. In this case, the multidimensional array is the right choice.
$array = ['abc','cde','frtg','acf'];
$new_array = array();
foreach($array as $v){
$letter = substr($v,0,1);
if(!isset($new_array[$letter])) {$new_array[$letter] = array();}
array_push($new_array[$letter], $v);
}
print_r($new_array);
I have an array that looks something like this:
Array (
[0] => Array ( [country_percentage] => 5 %North America )
[1] => Array ( [country_percentage] => 0 %Latin America )
)
I want only numeric values from above array. I want my final array like this
Array (
[0] => Array ( [country_percentage] => 5)
[1] => Array ( [country_percentage] => 0)
)
How I achieve this using PHP?? Thanks in advance...
When the number is in first position you can int cast it like so:
$newArray = [];
foreach($array => $value) {
$newArray[] = (int)$value;
}
I guess you can loop the 2 dimensional array and use a preg_replace, i.e.:
for($i=0; $i < count($arrays); $i++){
$arrays[$i]['country_percentage'] = preg_replace( '/[^\d]/', '', $arrays[$i]['country_percentage'] );
}
Ideone Demo
Update Based on your comment:
for($i=0; $i < count($arrays); $i++){
if( preg_match( '/North America/', $arrays[$i]['country_percentage'] )){
echo preg_replace( '/[^\d]/', '', $arrays[$i]['country_percentage'] );
}
}
Try this:
$arr = array(array('country_percentage' => '5 %North America'),array("country_percentage"=>"0 %Latin America"));
$result = array();
foreach($arr as $array) {
$int = filter_var($array['country_percentage'], FILTER_SANITIZE_NUMBER_INT);
$result[] = array('country_percentage' => $int);
}
Try this one:-
$arr =[['country_percentage' => '5 %North America'],
['country_percentage' => '0 %Latin America']];
$res = [];
foreach ($arr as $key => $val) {
$res[]['country_percentage'] = (int)$val['country_percentage'];
}
echo '<pre>'; print_r($res);
output:-
Array
(
[0] => Array
(
[country_percentage] => 5
)
[1] => Array
(
[country_percentage] => 0
)
)
You can use array_walk_recursive to do away with the loop,
passing the first parameter of the callback as a reference to modify the initial array value.
Then just apply either filter_var or intval as already mentioned the other answers.
$array = [
["country_percentage" => "5 %North America"],
["country_percentage" => "0 %Latin America"]
];
array_walk_recursive($array, function(&$value,$key){
$value = filter_var($value,FILTER_SANITIZE_NUMBER_INT);
// or
$value = intval($value);
});
print_r($array);
Will output
Array
(
[0] => Array
(
[country_percentage] => 5
)
[1] => Array
(
[country_percentage] => 0
)
)
You could get all nemeric values by looping through the array. However I don't think this is the most efficient and good looking answer, I'll post it anyways.
// Array to hold just the numbers
$newArray = array();
// Loop through array
foreach ($array as $key => $value) {
// Check if the value is numeric
if (is_numeric($value)) {
$newArray[$key] = $value;
}
}
I missunderstood your question.
$newArray = array();
foreach ($array as $key => $value) {
foreach ($value as $subkey => $subvalue) {
$subvalue = trim(current(explode('%', $subvalue)));
$newArray[$key] = array($subkey => $subvalue);
}
}
If you want all but numeric values :
$array[] = array("country_percentage"=>"5 %North America");
$array[] = array("country_percentage"=>"3 %Latin America");
$newArray = [];
foreach ($array as $arr){
foreach($arr as $key1=>$arr1) {
$newArray[][$key1] = intval($arr1);
}
}
echo "<pre>";
print_R($newArray);
This is kind of a ghetto method to doing it cause I love using not as many pre made functions as possible. But this should work for you :D
$array = array('jack', 2, 5, 'gday!');
$new = array();
foreach ($array as $item) {
// IF Is numeric (each item from the array) will insert into new array called $new.
if (is_numeric($item)) { array_push($new, $item); }
}
I'm trying below code to get the value as ou=grp1 from
dn: uid=john,ou=grp1,ou=people,dc=site,dc=com , but not understanding how to retrieve.
here is the code:
<?php
function pairstr2Arr ($str, $separator='=', $delim=',') {
$elems = explode($delim, $str);
foreach( $elems as $elem => $val ) {
$val = trim($val);
$nameVal[] = explode($separator, $val);
$arr[trim(strtolower($nameVal[$elem][0]))] = trim($nameVal[$elem][1]);
}
return $arr;
}
// Example usage:
$string = 'uid=john,ou=grp1,ou=people,dc=site,dc=com';
$array = pairstr2Arr($string);
echo '<pre>';
print_r($array);
echo '</pre>';
?>
output:
<pre>Array
(
[uid] => john
[ou] => people //here I want to get output ou=grp1,how?
[dc] => com
)
</pre>
find output here: https://ideone.com/rE6eaH
Because of ou and dc might have multiple values, you should store those values in array. Thanks to that you can have easy access to data. Check out this code:
<?php
function pairstr2Arr ($str, $separator='=', $delim=',') {
$elems = explode($delim, $str);
$arr = array();
foreach( $elems as $elem => $val ) {
$val = trim($val);
$tempArray = explode($separator, $val);
if(!isset($arr[trim($tempArray[0])]))
$arr[trim($tempArray[0])] = '';
$arr[trim($tempArray[0])] .= $tempArray[1].';';
}
foreach($arr as $key => $value)
{
$explodedValue = explode(';', $value);
if(count($explodedValue) > 2)
{
$arr[$key] = $explodedValue;
unset($arr[$key][count($explodedValue) - 1]);
}
else
$arr[$key] = substr($arr[$key], 0, -1);
}
return $arr;
}
// Example usage:
$string = 'uid=john,ou=grp1,ou=people,dc=site,dc=com';
$array = pairstr2Arr($string);
echo '<pre>';
print_r($array);
echo '</pre>';
?>
Result is:
Array
(
[uid] => john
[ou] => Array
(
[0] => grp1
[1] => people
)
[dc] => Array
(
[0] => site
[1] => com
)
)
I would like to know if there is a way to convert two Array keys into one Array key?
As an example the Array would look like:
[0] => '12345'
[1] => 'New'
[2] => 'York'
how can I combine [1] => 'New' and [2] => 'York' into [1] => 'New York'?
All I was found is array_merge that combines two Arrays. Even on how to concaternate two Array keys I could not find anything.
Thanks alot.
You can try :
$data = array(12345,"New","York");
echo concat($data, array(1,2)); //New York
//or
$data = array(12345,"New","York");
print_r(concatArray($data, array(1,2)));
Output
Array
(
[0] => 12345
[1] => New York
)
Function Used
function concat($array, $keys , $glue = " ") {
$values = array_intersect_key($array, array_flip($keys));
return implode($glue, $values);
}
function concatArray($array, $keys, $glue = " ") {
$last = null;
foreach ( $array as $key => &$value ) {
if (in_array($key, $keys)) {
if ($last === null) {
$last = $key;
continue;
}
$array[$last] .= $glue . $value;
unset($array[$key]);
}
}
return $array;
}
Here's a hint ;)
$new = $array[1] . ' ' . $array[2];
$array[1] = $new;
unset($array[2]);
I have a collection of keys in this massive flat single array I would like to basically expand that array into a multidimensional one organized by keys - here is an example:
'invoice/products/data/item1'
'invoice/products/data/item2'
'invoice/products/data/item2'
=>
'invoice'=>'products'=>array('item1','item2','item3')
how can I do this - the length of the above strings are variable...
Thanks!
$src = array(
'invoice/products/data/item1',
'invoice/products/data/item2',
'invoice/products/data/item2',
'foo/bar/baz',
'aaa/bbb'
);
function rsplit(&$v, $w)
{
list($first, $tail) = explode('/', $w, 2);
if(empty($tail))
{
$v[] = $first;
return $v;
}
$v[$first] = rsplit($v[$first], $tail);
return $v;
}
$result = array_reduce($src, "rsplit");
print_r($result);
Output is:
Array (
[invoice] => Array
(
[products] => Array
(
[data] => Array
(
[0] => item1
[1] => item2
[2] => item2
)
)
)
[foo] => Array
(
[bar] => Array
(
[0] => baz
)
)
[aaa] => Array
(
[0] => bbb
)
)
Something along these lines: (Didn't test it though!) Works now ;)
$data = array();
$current = &$data;
foreach($keys as $value) {
$parts = explode("/", $value);
$parts_count = count($parts);
foreach($parts as $i => $part) {
if(!array_key_exists($part, $current)) {
if($i == $parts_count - 1) {
$current[] = $part;
}
else {
$current[$part] = array();
$current = &$current[$part];
}
}
else {
$current = &$current[$part];
}
}
$current = &$data;
}
$keys beeing the flat array.
Although it's not clear from your question how the "/" separated strings will map to an array, the basic approach will probably be something like this:
$result = array();
$k1 = $k2 = '';
ksort($yourData); // This is the key (!)
foreach ($yourData as $k => $v) {
// Use if / else if / else if to watch for new sub arrays and change
// $k1, $k2 accordingly
$result[$k1][$k2] = $v;
}
This approach uses the ksort to ensure that keys at the same "level" appear together, like this:
'invoice/products/data1/item1'
'invoice/products/data1/item2'
'invoice/products/data2/item3'
'invoice/products2/data3/item4'
'invoice/products2/data3/item5'
Notice how the ksort corresponds to the key grouping you're aiming for.