count element in array? [duplicate] - php

This question already has answers here:
Working with arrays in php
(4 answers)
Closed 3 years ago.
I have an Array like this (when I print_r). How can I sum all elements in this Array?
In this example, the sum is 0+1=1
Array ( [0] => Array ( [0] => 0 )
[1] => Array ( [0] => 1 ) )

Take a look at the 'array_reduce' function: array_reduce: http://nl.php.net/manual/en/function.array-reduce.php
Taken from the documentation:
function rsum($v, $w)
{
$v += $w;
return $v;
}
$sum = array_reduce($a, "rsum");

Related

Array key value to string in PHP [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 want to convert an array of this style:
Array ( [0] => Array ( [post_id] => 20752 ) [1] => Array ( [post_id] => 20753 ) )
into the string:
20752, 20753
How can I do this?
You can use PHP array_column function with combination of implode:
implode(', ', array_column($data, 'post_id'));

Get array of user IDs from multi-dimensional array [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 years ago.
I've the following array:
Array
(
[0] => Array
(
[ID] => 1
[more_user_data] => More
)
[1] => Array
(
[ID] => 2
[more_user_data] => More
)
)
Now I want to have a comma separated list of the IDs to use them in an own array. To get something like this:
array(1,2)
How could I only extract the IDs from the second array?
Use array_column() function like:
$arr = array_column($array, 'ID');
Working Example

Sorting Array Values with underscore first [duplicate]

This question already has answers here:
php sort filenames with an underscore
(3 answers)
Closed 5 years ago.
I have an array of values (keys are not important):
$Array = array("File01","File02","File00","_File03");
I want to sort this by value, to match my Windows file system, e.g.:
Array ( [3] => _File03 [2] => File00 [0] => File01 [1] => File02 )
I have tried asort($Array), but this gives me:
Array ( [2] => File00 [0] => File01 [1] => File02 [3] => _File03 )
Is it possible to sort with underscores first?
try this.. its working.
<?php
$array = array("File01","File02","File00","_File03");
function sortUnderscoreToFront($a, $b) {
if (substr($a, 0, 1) == '_' || substr($b, 0, 1) == '_') {
return ((substr($a,0,1)=='_')?-1:1);
}
return strcmp(strval($a), strval($b));
}
usort($array, 'sortUnderscoreToFront');
echo "<pre>";
print_r($array)."</pre>";
?>

sort in explode variable [duplicate]

This question already has answers here:
PHP Sorting Array ASC
(3 answers)
Closed 6 years ago.
Hello everyone I have a small problem with the php sort I have basically a variable example
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
rsort($prova);
echo $prova[0];
but that's out 4,v Instead I would like so 1,x
Use sort() simply.
<?php
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova);
echo $prova[0]; // Prints 1,x
?>
See it working live
have a look at http://php.net/manual/en/function.sort.php, here you can use second parameter i.e. sort_flags
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova, SORT_STRING); //SORT_STRING - compare items as strings
print_r($prova);
sort($prova, SORT_NUMERIC); //SORT_NUMERIC - compare items numerically
print_r($prova);
output
Array
(
[0] => 1,x
[1] => 2,f
[2] => 22,a
[3] => 4,v
)
Array
(
[0] => 1,x
[1] => 2,f
[2] => 4,v
[3] => 22,a
)

Transformation multidimensional array into single array? [duplicate]

This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Closed 8 years ago.
My code looks like:
Array ( [0] => Array ( [uid] => 3456345345 [name] = test))
What to do to look like this:
Array ( [uid] => 3456345345 [name] = test)
<?php
function arrayflatten($array)
{
if(!is_array($array))
return false;
$result=array();
foreach($array as $key=>$value)
{
if(is_array($value))
$result=array_merge($result,arrayflatten($value));
else
$result[$key]=$value;
}
print_r ($result);
}
$a=array(array( "Volvo","22"));
arrayflatten($a);
?>

Categories