This question already has answers here:
Group multidimensional array data based on two column values and sum values of one column in each group
(5 answers)
Closed 7 months ago.
I have 2D array (showed bellow) - output of wp_query (Wordpress).
I need to group the arrays by index 0, 1, 2 values (lets think that the primary key is composed from these values and it should be unique in result table) and sum the index 3 values.
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(6) "Jack"
[2]=> string(6) "Daniels"
[3]=> string(2) "10"
}
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(3) "Jan"
[2]=> string(6) "Novak"
[3]=> string(2) "33"
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "John"
[2]=> string(3) "Doe"
[3]=> string(2) "11"
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "Jane"
[2]=> string(3) "Doe"
[3]=> string(2) "18"
}
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(6) "Jack"
[2]=> string(6) "Daniels"
[3]=> string(1) "5"
}
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(3) "Jan"
[2]=> string(6) "Novak"
[3]=> string(2) "33"
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "John"
[2]=> string(3) "Doe"
[3]=> string(2) "11"
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "Jane"
[2]=> string(3) "Doe"
[3]=> string(2) "18"
}
ETC...
Output should be someting like:
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(6) "Jack"
[2]=> string(6) "Daniels"
[3]=> string(2) "15" (5+10)
}
array(4) {
[0]=> string(6) "Team 1"
[1]=> string(3) "Jan"
[2]=> string(6) "Novak"
[3]=> string(2) "66" (33+33)
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "John"
[2]=> string(3) "Doe"
[3]=> string(2) "22" (11+11)
}
array(4) {
[0]=> string(6) "Team 2"
[1]=> string(4) "Jane"
[2]=> string(3) "Doe"
[3]=> string(2) "36" (18+18)
}
}
I tried to do this by recursive for loops, but I cannot find the reason why is it not working properly.
$vysledna_tabulka = array();
$aktualni;
foreach($seznam_vsech_hracu_vsech_tymu as $radek){
if(empty($vysledna_tabulka)){
array_push($vysledna_tabulka,$radek);
} else{
foreach($vysledna_tabulka as $vysledny_radek){
if($vysledny_radek[0]==$radek[0] && $vysledny_radek[1]==$radek[1] && $vysledny_radek[2]==$radek[2]){
$vysledny_radek[3]+=$radek[3];
} else {
$aktualni = $radek;
}
}
array_push($vysledna_tabulka,$aktualni);
}
}
I think there should be a better way to do this. Is there anyone who could help me?
Thanks.
I would create a key from the first three values of your array elements and then create a new array using this key. Like this:
<?php
$teamArray = [
[
"Team 1",
"Jack",
"Daniels",
"10",
],
[
"Team 1",
"Jan",
"Novak",
"33",
],
[
"Team 2",
"John",
"Doe",
"11",
],
[
"Team 2",
"Jane",
"Doe",
"18",
],
[
"Team 1",
"Jack",
"Daniels",
"5",
],
[
"Team 1",
"Jan",
"Novak",
"33",
],
[
"Team 2",
"John",
"Doe",
"11",
],
[
"Team 2",
"Jane",
"Doe",
"18",
]
];
$result = [];
foreach ($teamArray as $team) {
$key = $team[0] . $team[1] . $team[2];
if (isset($result[$key])) {
$result[$key][3] += $team[3];
}
else {
$result[$key] = $team;
}
}
var_dump($result);
var_dump(array_values($result));
This gives you:
array(4) {
["Team 1JackDaniels"]=>
array(4) {
[0]=>
string(6) "Team 1"
[1]=>
string(4) "Jack"
[2]=>
string(7) "Daniels"
[3]=>
int(15)
}
["Team 1JanNovak"]=>
array(4) {
[0]=>
string(6) "Team 1"
[1]=>
string(3) "Jan"
[2]=>
string(5) "Novak"
[3]=>
int(66)
}
["Team 2JohnDoe"]=>
array(4) {
[0]=>
string(6) "Team 2"
[1]=>
string(4) "John"
[2]=>
string(3) "Doe"
[3]=>
int(22)
}
["Team 2JaneDoe"]=>
array(4) {
[0]=>
string(6) "Team 2"
[1]=>
string(4) "Jane"
[2]=>
string(3) "Doe"
[3]=>
int(36)
}
}
array(4) {
[0]=>
array(4) {
[0]=>
string(6) "Team 1"
[1]=>
string(4) "Jack"
[2]=>
string(7) "Daniels"
[3]=>
int(15)
}
[1]=>
array(4) {
[0]=>
string(6) "Team 1"
[1]=>
string(3) "Jan"
[2]=>
string(5) "Novak"
[3]=>
int(66)
}
[2]=>
array(4) {
[0]=>
string(6) "Team 2"
[1]=>
string(4) "John"
[2]=>
string(3) "Doe"
[3]=>
int(22)
}
[3]=>
array(4) {
[0]=>
string(6) "Team 2"
[1]=>
string(4) "Jane"
[2]=>
string(3) "Doe"
[3]=>
int(36)
}
}
If you need to remove the keys from your $result array you could use array_value as shown above.
Related
I'm attempting to create a multidimensional array which should have the ID and quantity from the $_POST array. At the moment it seems to put every quantity into each an element with each ID.However I want it to take the first elements from each array and then add them together to a new array and so on.
Whereas it should be
ID 1 - Quantity 100
ID 2 - Quantity 50
etc
But at the moment I get this
array(16) {
[0]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(2) "50"
}
[1]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(3) "100"
}
[2]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(3) "100"
}
[3]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(3) "100"
}
[4]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(2) "50"
}
[5]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(3) "100"
}
[6]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(3) "100"
}
[7]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(3) "100"
}
[8]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(2) "50"
}
[9]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(3) "100"
}
[10]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(3) "100"
}
[11]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(3) "100"
}
[12]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(2) "50"
}
[13]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(3) "100"
}
[14]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(3) "100"
}
[15]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(3) "100"
}
}
Here is my PHP code.
foreach($_POST['sweetids'] as $id) {
foreach($_POST['quantites'] as $quantity) {
$stock_array[] = array(
"id"=> $id,
"quantity" => $quantity
);
}
}
I think this is what you're trying to achieve:
foreach($_POST['sweetids'] as $key=>$id) {
$stock_array[] = array(
"id"=> $id,
"quantity" => $_POST['quantities'][$key]
);
}
You're iterating $_POST['quantities'] for every $_POST['sweetids'] which is probably not what you intend. When you iterate both, your result will be every combination of sweetids and quantities, not each pair of them.
I'm guessing you meant something more like:
// Assuming you already verified that $_POST['quantities'] and $_POST['sweetids'] exist
// and that both of them have the same number of elements
for ( $i = 0, $len = count($_POST['sweetids']); $i < $len; $i++ ) {
$stock_array[] = array(
'id' => $_POST['sweetids'][$i],
'quantity' => $_POST['quantities'][$i]
);
}
Below is example of my array
["value"]=>
array(16) {
[0]=>
string(5) "4"
[1]=>
string(4) "2"
[2]=>
string(4) "1"
[3]=>
string(4) "3"
}
["id"]=>
array(16) {
[0]=>
string(4) "four-id"
[1]=>
string(4) "two-id"
[2]=>
string(4) "one-id"
[3]=>
string(4) "three-id"
}
Now I am sorrting using array_multisort($arr["value"],SORT_NUMERIC, SORT_DESC); , which results in below output.
["value"]=>
array(16) {
[0]=>
string(5) "4"
[1]=>
string(4) "3"
[2]=>
string(4) "2"
[3]=>
string(4) "1"
}
["id"]=>
array(16) {
[0]=>
string(4) "four-id"
[1]=>
string(4) "two-id"
[2]=>
string(4) "one-id"
[3]=>
string(4) "three-id"
}
I want $arr["id"] to be sorted based on same sorting order of $arr["value"] like below
["id"]=>
array(16) {
[0]=>
string(4) "four-id"
[1]=>
string(4) "three-id"
[2]=>
string(4) "two-id"
[3]=>
string(4) "one-id"
}
You can use krsort
$array_1 = Array(4,2,1,3);
$array_2 = Array('four-id','two-id','one-id','three-id');
foreach ($array_1 as $key => $value) {
$array_merged[$value] = $array_2[$key];
}
krsort($array_merged);
foreach ($array_merged as $key => $value) {
$new_array_1[] = $key;
$new_array_2[] = $value;
}
echo '<pre>'.print_r( $new_array_1, true ).print_r( $new_array_2, true ).'</pre>';
I'm looking for a solution to create an associative array from an flat array data in foreach loop:
What i have is a csv/xls file that has header on first row and data in next rows.
Row1: header
Row2,3,4,5: data
the array looks:
array(3) {
[0]=>
array(7) {
[0]=>
string(3) "country"
[1]=>
string(7) "state"
[2]=>
string(3) "city"
[3]=>
string(5) "name"
[4]=>
string(4) "address"
[5]=>
string(6) "gender"
[6]=>
string(6) "status"
}
[1]=>
array(7) {
[0]=>
string(12) "Argentina"
[1]=>
string(12) "Corrientes"
[2]=>
string(12) "Corrientes"
[3]=>
string(12) "Jorge"
[4]=>
string(12) "Avenida Avellaneda 12"
[5]=>
string(12) "Masculino"
[6]=>
string(12) "Activo"
}
[2]=>
array(7) {
[0]=>
string(12) "Argentina"
[1]=>
string(12) "Chaco"
[2]=>
string(12) "Resistencia"
[3]=>
string(12) "Mariano"
[4]=>
string(12) "Avenida Peron 12"
[5]=>
string(12) "Masculino"
[6]=>
string(12) "Activo"
}
}
The result i need to get at the end is:
array(2) {
[0]=>
array(7) {
['country']=>
string(12) "Argentina"
['state']=>
string(12) "Corrientes"
['city']=>
string(12) "Corrientes"
['name']=>
string(12) "Jorge"
['address']=>
string(12) "Avenida Avellaneda 12"
['gender']=>
string(12) "Masculino"
['status']=>
string(12) "Activo"
}
[1]=>
array(7) {
['country']=>
string(12) "Argentina"
['state']=>
string(12) "Chaco"
['city']=>
string(12) "Resistencia"
['name']=>
string(12) "Mariano"
['address']=>
string(12) "Avenida Peron 12"
['gender']=>
string(12) "Masculino"
['status']=>
string(12) "Activo"
}
}
$array = $your_flat_array;
for ($i = 1; $i < count($array); $i++) {
$new_array[$i-1] = [];
foreach ($array[$i] as $key => $value) {
$new_array[$i-1][$array[0][$key]] = $value;
}
}
print_r($new_array);
create a multidimensional array from an flat array
You already have a multidimensional array, because you got arrays in an array.
What you can do in this specific case is to use array_splice() in combination with array_combine().
Try this:
$oldArray = array(
array( "country", "state", "city", "name" ),
array( "Argentina", "Corrientes", "Corrientes", "Jorge" ),
array( "Argentina", "Chaco", "Resistencia", "Mariano" )
);
$newArray = array_splice( $oldArray, 1 );
foreach( $newArray as $index => $array ) {
$newArray[$index] = array_combine( $oldArray[0], $array );
}
echo "<pre>";
var_dump( $newArray );
OUTPUT:
array(2) {
[0]=>
array(4) {
["country"]=>
string(9) "Argentina"
["state"]=>
string(10) "Corrientes"
["city"]=>
string(10) "Corrientes"
["name"]=>
string(5) "Jorge"
}
[1]=>
array(4) {
["country"]=>
string(9) "Argentina"
["state"]=>
string(5) "Chaco"
["city"]=>
string(11) "Resistencia"
["name"]=>
string(7) "Mariano"
}
}
All you have to do is remove the first item (header row) from your array:
array_splice($yourArray, 0, 1);
i have an array like this
array 1
array(3) {
[0]=> string(2) "47"
[1]=> string(2) "48"
[2]=> string(2) "49"
}
i have plan to giving array with name, the array name is number
array 2
array(3) {
[0]=> object(stdClass)#18 (2) {
["address"]=> string(9) "Address 1"
["price"]=> string(16) "120000" }
[1]=> object(stdClass)#21 (2) {
["address"]=> string(9) "Address 2"
["price"]=> string(16) "150000" }
[2]=> object(stdClass)#20 (2) {
["address"]=> string(9) "Address 3"
["price"]=> string(16) "180000" }
}
I want to inserting array 1 into array 2 which same array key
I want to insert array 1 data into array 2 accordance with the key array . so i I was expecting to be joined both of arrays and became joined array like this
array(3) {
[0]=> object(stdClass)#18 (2) {
["address"]=> string(9) "Address 1"
["price"]=> string(16) "120000"
["number"]=> string(2) "47" }
[1]=> object(stdClass)#21 (2) {
["address"]=> string(9) "Address 2"
["price"]=> string(16) "150000"
["number"]=> string(2) "48"}
[2]=> object(stdClass)#20 (2) {
["address"]=> string(9) "Address 3"
["price"]=> string(16) "180000"
["number"]=> string(2) "49"}
}
is there any way to create or manipulate into an array like that ? my array is dynamically so number of array can be changed anytime.
I would greatly appreciate it if you could help me
Read up on the basic language control structures and foreach in particular.
foreach ($array2 as $index => $object) {
if (isset($array1[$index])) {
$object->number = $array1[$index];
}
}
Outcome:
array(3) {
[0]=>
object(stdClass)#1 (3) {
["address"]=>
string(9) "Address 1"
["price"]=>
string(6) "120000"
["number"]=>
string(2) "47"
}
[1]=>
object(stdClass)#2 (3) {
["address"]=>
string(9) "Address 2"
["price"]=>
string(6) "150000"
["number"]=>
string(2) "48"
}
[2]=>
object(stdClass)#3 (3) {
["address"]=>
string(9) "Address 3"
["price"]=>
string(6) "180000"
["number"]=>
string(2) "49"
}
}
Here is a Codepad demo
I currently have an array that looks like the one below but I only want to display show results in the array from which are unique (description), I presume with array_unique? but I keep getting the same results?
Here is my array:
$taglist = array(5) {
[0]=> array(5) {
["id"]=> string(2) "27"
["page_id"]=> string(2) "18"
["description"]=> string(10) "Web Design"
["slug"]=> string(10) "web-design"
["visibility"]=> string(7) "visible"
}
[1]=> array(5) {
["id"]=> string(2) "29"
["page_id"]=> string(2) "18"
["description"]=> string(3) "Tutorials"
["slug"]=> string(3) "tutorials"
["visibility"]=> string(7) "visible"
}
[2]=> array(5) {
["id"]=> string(2) "31"
["page_id"]=> string(2) "21"
["description"]=> string(3) "tag"
["slug"]=> string(3) "tag"
["visibility"]=> string(7) "visible"
}
[3]=> array(5) {
["id"]=> string(2) "32"
["page_id"]=> string(2) "21"
["description"]=> string(10) "Web Design"
["slug"]=> string(10) "web-design"
["visibility"]=> string(7) "visible"
}
}
Here is my while:
$items = array();
$results = $taglist;
foreach ($results as $result)
{
$items[]= $result['description'];
$items = array_unique($items);
}
echo '<ul>';
while ($tag_item = current($items))
{
echo '<li>'.$tag_item['description'].'</li>';
next($items);
}
echo '</ul>';
$taglist = array(
0 => array('description'=>'one'),
1 => array('description'=>'two'),
2 => array('description'=>'one'),
3 => array('description'=>'three'),
4 => array('description'=>'one'),
);
// echo var_export($taglist, 1); // uncomment to see the diff vs var_dump()
foreach($taglist as $tag){
$new[] = $tag['description'];
}
var_dump(array_unique($new));