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 :)
Related
This question already has answers here:
Reverse an associative array with preserving keys in PHP
(4 answers)
Closed 5 years ago.
I am developing a new website, and I have a quetion.
Input array:
Array ( [1319] => ####,[1316] => ###)
I have an array and I want to revese him, after the reverse the array would be like this:
Expected output:
Array ( [1316] => ###,[1319] => ####)
but when i'm using array_reverse function, it doesnt work for me, I got this array:
Array ( [0] => ###,[1] => ####)
Why it is happen?
For preserving keys you just pass second parameter to true in array_reverse.
Try this code snippet here
$array=Array ( 1319 => "####",1316 => "###");
print_r(array_reverse($array,true));
you can try this:
$a = []; //your array
$keys = array_keys($arr);
$values = array_values($arr);
$rv = array_reverse($values);
$newArray = array_combine($keys, $rv);
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 duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 8 years ago.
I have array A :
Input:
A ={2,3,2,{1},3,2,{0},3,2,0,11,7,9,{2}}
I want output to be Array B
Output:
B={0,1,2,3,7,9,11}
How can i remove the duplicate values and sort them ascending with PHP?
if you have:
$a = array(2,3,2,1,3,2,0,3,2,0,11,7,9,2);
you can use array_unique() to remove duplicates:
$a = array_unique($a);
and then use asort() to sort the array values:
asort($a);
//Try this out...
$array = array(2,3,2,(1),3,2,(0),3,2,0,11,7,9,(2));
$array_u = array_unique($array);
sort($array_u);
print_r($array_u);
Sample output
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 7
[5] => 9
[6] => 11
)
First step: flattern
function flatten(array $array) {
$return = array();
array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
return $return;
}
then using asort and array_unique you can remove duplicates and sort ascendent.
$result = array_unique(flattern($array));
asort($result);
Sources:
How to Flatten a Multidimensional Array?
This question already has answers here:
Remove duplicates from Array
(2 answers)
Closed 9 years ago.
I have an array like this
Array
(
[0] => u1,u2
[1] => u2,u1
[2] => u4,u3
[3] => u1,u3
[4] => u1,u2
)
I want to remove similar values from the array
I want an out put like
Array
(
[0] => u1,u2
[1] => u4,u3
[2] => u1,u3
)
I tried to loop thru the input array, sort the value of the indexes alphabetically and then tried array_search to find the repeated values. but never really got the desired output
any help apprecated
You cannot use array_unique() alone, since this will only match exact duplicates only. As a result, you'll have to loop over and check each permutation of that value.
You can use array_unique() to begin with, and then loop over:
$myArray = array('u1,u2', 'u2,u1', 'u4,u3', 'u1,u3', 'u1,u2');
$newArr = array_unique($myArray);
$holderArr = array();
foreach($newArr as $val)
{
$parts = explode(',', $val);
$part1 = $parts[0].','.$parts[1];
$part2 = $parts[1].','.$parts[0];
if(!in_array($part1, $holderArr) && !in_array($part2, $holderArr))
{
$holderArr[] = $val;
}
}
$newArr = $holderArr;
The above code will produce the following output:
Array (
[0] => u1,u2
[1] => u4,u3
[2] => u1,u3
)
Use array_unique() PHP function:
http://php.net/manual/en/function.array-unique.php
Use the function array_unique($array)
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
php manual
since u1,u2 !== u2,u1
$array=array('u1,u2','u2,u1','u4,u3','u1,u3','u1,u2');
foreach($array as $k=>$v)
{
$sub_arr = explode(',',$v);
asort($sub_arr);
$array[$k] = implode(',',$sub_arr);
}
$unique_array = array_unique($array);
//$unique_array = array_values($unique_array) //if you want to preserve the ordered keys
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
any particular function or code to put this kind of array data
ori [0] => 43.45,33,0,35 [1] => 74,10,0,22 [2] => 0,15,0,45 [3] => 0,0,0,340 [4] => 12,5,0,0 [5] => 0,0,0,0
to
new [0] => 43.45,74,0,0,12,0 [1] => 33,10,15,0,5,0 [2] => 0,0,0,0,0,0, [3] => 35,22,45,340,0,0
As you can see, the first value from each ori are inserted into the new(0), the second value from ori are inserted into new(1) and so on
If $ori is an array of arrays, this should work:
function transpose($array) {
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}
$newArray = transpose($ori);
Note: from Transposing multidimensional arrays in PHP
If $ori is not an array of arrays, then you'll need to convert it first (or use the example by Peter Ajtai), like this:
// Note: PHP 5.3+ only
$ori = array_map(function($el) { return explode(",", $el); }, $ori);
If you are using an older version of PHP, you should probably just use the other method!
You essentially want to transpose - basically "turn" - an array. Your array elements are strings and not sub arrays, but those strings can be turned into sub arrays with explode() before transposing. Then after transposing, we can turn the sub arrays back into strings with implode() to preserve the formatting you want.
Basically we want to go through each of your five strings of comma separated numbers one by one. We take each string of numbers and turn it into an array. To transpose we have to take each of the numbers from a string one by one and add the number to a new array. So the heart of the code is the inner foreach(). Note how each number goes into a new sub array, since $i is increased by one between each number: $new[$i++][] =$op;
foreach($ori as $one) {
$parts=explode(',',$one);
$i = 0;
foreach($parts as $op) {
$new[$i++][] =$op;
}
}
$i = 0;
foreach($new as $one) {
$new[$i++] = implode(',',$one);
}
// print_r for $new is:
Array
(
[0] => 43.45,74,0,0,12,0
[1] => 33,10,15,0,5,0
[2] => 0,0,0,0,0,0
[3] => 35,22,45,340,0,0
)
Working example