I have my main array:
array(6) {
[1]=> array(3) {
[0]=> string(15) "Extension"
[1]=> int(1)
[2]=> string(6) "3,00 "
}
[2]=> array(3) {
[0]=> string(32) "Physics "
[1]=> string(1) "1"
[2]=> string(6) "3,00 "
}
[3]=> array(3) {
[0]=> string(31) "Physics "
[1]=> int(1)
[2]=> string(6) "6,00 "
}
[4]=> array(3) {
[0]=> string(34) "Desk"
[1]=> int(4)
[2]=> string(8) "127,00 "
}
[5]=> array(3) {
[0]=> string(18) "assistance"
[1]=> int(1)
[2]=> string(7) "12,50 "
}
[6]=> array(3) {
[0]=> string(15) "Extension"
[1]=> int(1)
[2]=> string(6) "3,00 "
}
}
My expected output is:
Extension 2
Physics 2
Desk 1
Assistance 1
The result must be in an resultarray
How can I do? I tried with array_count_values function but don't work.
How can I stock answear:
I tried this code but It doesn't work
$tabrecap = array();
foreach($counts as $key=>$value){
//echo $key." qte".$value;
$tabrecap = array ($key,$value,$valueOption);
}
As you asked in comment,Please try this:-
<?php
$array = array( '1'=> array('0'=>"Extension", '1'=> 1, '2'=>"3,00 " ), '2'=> array('0'=>"Physics",'1'=>"1","3,00 " ),'3'=> array('0'=>"Physics",'1'=>1,"6,00 "),'4'=> array('0'=>"Desk",'1'=>4,"127,00 "),'5'=> array('0'=>"assistance",'1'=>1,"12,50 " ),'6'=> array('0'=>"Extension",'1'=>1,"3,00 "));
$count = array();
$i = 0;
foreach ($array as $key=>$arr) {
// Add to the current group count if it exists
if (isset($count[$i][$arr[0]])) {
$count[$i][$arr[0]]++;
}
else $count[$i][$arr[0]] = 1;
$i++;
}
print_r($count);
?>
Output:- https://eval.in/379176
Looping is the answer.
<?php
// untested
$counts = Array();
foreach( $array as $subArray ){
$value = $subArray[0];
$counts[ $value ] = ( isset($counts[ $value ]) )
? $counts[ $value ] + 1
: 1;
}
var_dump( $counts);
Just make a loop and use first item of each array as key :
$array = array(
array("Extension", 1, "3,00"),
array("Physics", "1", "3,00"),
array("Physics", 1, "6,00 ")
);
$count = array();
foreach($array as $a)
$count[$a[0]]++;
var_dump($count); // array(2) { ["Extension"]=> int(1) ["Physics"]=> int(2) }
Related
So I sorted a multidimensional array with uasort in descending order. I did a var_dump($winrateArray) and it is sorted properly. The highest value is in the first returned array. However when I try a var_dump($winrateArray[0][3]) which is where I expect the highest value to be it isn't there. Instead it is in $winrateArray[1][3]. Am I using uasort properly?
Unsorted dump:
array(2) { [0]=> array(4) { [0]=> string(2) "18" [1]=> string(1) "1" [2]=> int(0) [3]=> int(1) } [1]=> array(4) { [0]=> string(2) "31" [1]=> string(1) "1" [2]=> int(100) [3]=> int(101) } }
Sorted dump:
array(2) { [1]=> array(4) { [0]=> string(2) "31" [1]=> string(1) "1" [2]=> int(100) [3]=> int(101) } [0]=> array(4) { [0]=> string(2) "18" [1]=> string(1) "1" [2]=> int(0) [3]=> int(1) } }
Specific dump:
int(1)
.
$winrateArray[0][0] = '18';
$winrateArray[0][1] = '1';
$winrateArray[0][2] = 0;
$winrateArray[0][3] = 1;
$winrateArray[1][0] = '31';
$winrateArray[1][1] = '1'
$winrateArray[1][2] = 100;
$winrateArray[1][3] = 101;
var_dump($winrateArray);
function cmp($a, $b){
if ($a[3] == $b[3]){
return 0;
}
return ($a[3] < $b[3]) ? 1 : -1;
}
uasort($winrateArray, 'cmp');
var_dump($winrateArray);
var_dump($winrateArray[0][3]);
You can do:
$newArray = $winrateArray;
uasort($winrateArray, 'cmp');
$i = 0;
foreach($winrateArray as $key => $item) {
$newArray[$i][3] = $item[3];
$i++;
}
and use the new array after that. You will preserve keys and you will reorder and replace 3rd elements only
I have this foreach loop that outputs the below array, and I'm having a senior moment, I need it to return one array with no duplicate values, and I just can't it right.
foreach ( $post_groups as $post_group => $id ) {
group = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $table_name WHERE ID = %d", $id), ARRAY_A);
$groups[$group['group_name']] = $group['group_name'] = unserialize( $group['group_users'] );
}
output:
array(2) {
["Registered Users"]=>
array(1) {
[0]=>
string(1) "2"
}
["Admin Users"]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
}
Cheers
I believe the following is what you're after. Simply merge the arrays together and then ensure the result is unique.
$userIds = [
'Registered Users' => array(1,2,3),
'Admin Users' => array(3,4,5),
];
$allUserIds = array_unique(call_user_func_array('array_merge', $userIds));
var_dump($userIds);
/*
array(2) {
["Registered Users"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
["Admin Users"]=>
array(3) {
[0]=>
int(3)
[1]=>
int(4)
[2]=>
int(5)
}
}
*/
var_dump($allUserIds);
/*
array(5) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[4]=>
int(4)
[5]=>
int(5)
}
*/
Use to php functions : array_unique(array_merge())
test code:
$userIds = array('RegisteredUsers' => array('A','a','b','B'),'AdminUsers' => array('C','c','B','d','a'));
$allUserIds = array_unique(array_merge($userIds['RegisteredUsers'], $userIds['AdminUsers']));
echo "<br><br>";
var_dump($userIds);
var_dump($allUserIds);
I have an array like:
print_r($arr);
array(2) {
["'type'"]=>
array(3) {
[0]=>
string(17) "tell" // <----
[1]=>
string(6) "mobile" // <----
[2]=>
string(6) "address" // <----
}
["'value'"]=>
array(3) {
[0]=>
string(11) "+00.0000000" // tell
[1]=>
string(11) "12345678" // mobile
[2]=>
string(11) "Blah SQ." // address
}
}
I want a final string like:
tell = +00.0000000<br />mobile = 12345678<br />address = Blah SQ.
Now it's been more than an hour I'm struggling with this but no results yet, anyone could help me with this? I would appreciate anykind of help.
Thanks
=======================================
What I have tried:
$arr is an array so I did:
foreach($arr as $values){
// here also $values is an array, so I needed another foreach to access to items:
$i = 0;
foreach($values as $items){
// now making the final output
#$output.= $items['type'][$i] . '=' . $items['value'][$i] . '<br />';
$i++;
}
}
I'd go for array_combine(). Basically it does what you request:
$yourArray = [
"type" => ["tell", "mobile", "address"],
"value" => ["+00.0000000", "12345678", "Blah SQ."]
];
$combined = array_combine($yourArray["type"], $yourArray["value"]);
will be
$combined = [
"tell" =>"+00.0000000",
"mobile" =>"12345678",
"address" =>"Blah SQ."
];
Lastly, you can iterate through that array and then join the values:
$finalArray=array();
foreach($combined as $type=>$value)
$finalArray[]="$type=$value";
$string = join("<br/>", $finalArray); // Will output tell=+00.000000<br/>mobile=12345678<br/>address=Blah SQ.
It's not the fastest method but you'll learn quite a bit about arrays.
EDIT (using array_combine by #Dencker)
foreach($arr as $values) {
// here also $values is an array, so I needed another foreach to access to items:
$v = array_combine($values["'type'"], $values["'value'"]);
foreach($v as $key => $val) {
// now making the final output
$output.= $key . '=' . $val . '<br />';
}
}
Try this
$arr = array(
array(
"'type'" => array('tell', 'mobile', 'address'),
"'value'" => array('+00000', '123123', 'foo')
),
array(
"'type'" => array('tell', 'mobile', 'address'),
"'value'" => array('+10000', '123123', 'bar')
),
array(
"'type'" => array('tell', 'mobile', 'address'),
"'value'" => array('+20000', '123123', 'foobar')
),
);
var_dump($arr);
$output = '';
foreach($arr as $values) {
// here also $values is an array, so I needed another foreach to access to items:
$i = 0;
foreach($values as $items) {
// now making the final output
$output.= $values["'type'"][$i] . '=' . $values["'value'"][$i] . '<br />';
$i++;
}
}
echo $output;
You were referencing the other array in the second loop.
=============================================================
EDIT:
var_dump($arr);
array(3) {
[0]=> array(2) {
["type"]=> array(3) {
[0]=> string(4) "tell"
[1]=> string(6) "mobile"
[2]=> string(7) "address"
}
["value"]=> array(3) {
[0]=> string(6) "+00000"
[1]=> string(6) "123123"
[2]=> string(3) "foo"
}
}
[1]=> array(2) {
["type"]=> array(3) {
[0]=> string(4) "tell"
[1]=> string(6) "mobile"
[2]=> string(7) "address"
}
["value"]=> array(3) {
[0]=> string(6) "+10000"
[1]=> string(6) "123123"
[2]=> string(3) "bar"
}
}
[2]=> array(2) {
["type"]=> array(3) {
[0]=> string(4) "tell"
[1]=> string(6) "mobile"
[2]=> string(7) "address"
}
["value"]=> array(3) {
[0]=> string(6) "+20000"
[1]=> string(6) "123123"
[2]=> string(6) "foobar"
}
}
}
OUTPUT:
tell=+00000
mobile=123123
tell=+10000
mobile=123123
tell=+20000
mobile=123123
I have this array and I was wondering how can I :
Sum qty so finaly I receive only unique products ids with their qty-s, for example:
product 805 - 1 piece
product 1118 - 2+3+4 = 9pieces
array(2){
["product"]=> array(4){
[0]=> string(3) "805"
[1]=> string(4) "1118"
[2]=> string(4) "1118"
[3]=> string(4) "1118"
}
["qty"]=> array(4) {
[0]=> string(1) "1"
[1]=> string(1) "2"
[2]=> string(1) "3"
[3]=> string(1) "4"
}
}
Thank you in advance,
$productQuantities = array();
$products = array("805","1118","1118","1118");
$quantities = array(1,2,3,4);
foreach($products AS $key=>$productId){
$quantity = (int) $quantities[$key];
if(isset($productQuantities[$productId])){
$productQuantities[$productId] += $quantity;
} else {
$productQuantities[$productId] = $quantity;
}
}
var_dump($productQuantities);
You could try this:
$zipped=array_map(
null,
$your_array['product'],
$your_array['qty']
);
$compact = array();
foreach ($zipped as $k => $v){
if(!array_key_exists($v[0], $compact)){
$compact[$v[0]] = $v[1];
} else {
$compact[$v[0]] += $v[1];
}
}
Then you will find your result in $compact
I have the array below:
[1]=>
array(2) {
[0]=>
object(stdClass)#23 (7) {
["AddressesTableID"]=> string(1) "8"
["AccreditNo"]=> string(13) "5129876de28ff"
["Type"]=> string(4) "home"
["Street"]=> string(34) "Wallace, Village, Under the bridge"
["Municipality"]=> string(8) "Tortuous"
["Province"]=> string(8) "Sardonic"
["ContactNo"]=> string(8) "92012010"
}
[1]=>
object(stdClass)#24 (7) {
["AddressesTableID"]=> string(1) "9"
["AccreditNo"]=> string(13) "5129876de28ff"
["Type"]=> string(6) "office"
["Street"]=> string(25) "Rasputin Query, Palpitate"
["Municipality"]=> string(7) "Opulent"
["Province"]=> string(6) "Number"
["ContactNo"]=> string(8) "29101011"
}
}
Can you guys help me on how to transform this into:
Where all the values are grouped as an array into similar keys?
["AccreditNo"]=> array(2) { "5129876de28ff", "GKIJUGUIKGI" }
["Type"]=> array(2) { "home", "home" }
["Street"]=> ...
["Municipality"]=> ...
["Province"]=> ...
["ContactNo"]=> ...
Try with:
$input = array( /* your input data*/ );
$output = array();
foreach ( $input as $data ) {
foreach ( $data as $key => $value ) {
if ( !isset($output[$key]) ) {
$output[$key] = array();
}
$output[$key][] = $value;
}
}
You can use the array_merge_recursive() function, as follows:
$arr = array_merge_recursive($arr1, $arr2)
for more information, please see here:http://www.php.net/manual/en/function.array-merge-recursive.php