I'm getting stack-removing duplicated keys and assigning them to a new array.
My array:
array (
[1] => Array
(
[name] => name1
[actions] => add
)
[2] => Array
(
[name] => name1
[actions] => remove
)
[3] => Array
(
[name] => name2
[actions] => dosomething1
)
[4] => Array
(
[name] => name2
[actions] => dosomething1
)
)
What I am trying to achieve:
array (
[1] => Array
(
[name] => name1
[actions] => add
[actions] => remove
)
[2] => Array
(
[name] => name2
[actions] => dosomething1
[actions] => dosomething1
)
)
What i have tried:
public function array_unique_multidimensional($input)
{
$serialized = array_map('serialize', $input);
$unique = array_unique($serialized);
return array_intersect_key($input, $unique);
}
It is incorrectly returning the same array. Any help would be appreciated.
You cannot have two array keys with the save values (so two actions elements for a given element would not be possible) What you can do is have a single action element with multiple values in it.
$results = array();
foreach ($array as $v){
if (!isset($results[$v["name"]]){
$results[$v["name"]] = array("name"=>$v["name"], "actions"=>array($v["actions"]));
} else {
$results[$v["name"]]["actions"][] = $v["actions"];
}
}
if you want to remove the string keys on the top level array you can then do.
$results = array_values($results);
Related
I have an array like this:
Array
(
[0] => Array
(
[id] => 10
[field] => new
[value] => pqr
)
[1] => Array
(
[id] => 14
[field] => test
[value] => abc
)
[2] => Array
(
[id] => 17
[field] => test
[value] => xyz
)
)
Now I want to merge this array with field name with id and value will be comma separated. So my new array will look like:
Array
(
[0] => Array
(
[id] => 10
[field] => new
[value] => pqr
)
[1] => Array
(
[id] => 14,17
[field] => test
[value] => abc,xyz
)
)
Can we do this with any php inbuilt function.
I don't know of any built in function to do this, but it's pretty trivial with a simple foreach loop.
String Concatenation Approach
$new_array = [];
foreach($array1 as $arr) {
$field = $arr['field'];
$id = $arr['id'];
$value = $arr['value'];
//we use $field as $new_array keys so we can combine values
if(!array_key_exists($field, $new_array)) {
//key doesn't exist in new array, so create it
$new_array[$field] = $arr;
} else {
//key exists in new array, append new values
$new_array[$field]['id'] .= ",{$id}";
$new_array[$field]['value'] .= ",{$value}";
}
}
//reset array keys back to sequential
$new_array = array_values($new_array);
Output of $new_array would be
Array
(
[0] => Array
(
[id] => 10
[field] => new
[value] => pqr
)
[1] => Array
(
[id] => 14,17
[field] => test
[value] => abc,xyz
)
)
Normalized Array Approach
$new_array = [];
foreach($array1 as $arr) {
$field = $arr['field'];
$id = $arr['id'];
$value = $arr['value'];
//we use $field as $new_array keys so we can combine values
if(!array_key_exists($field, $new_array)) {
//key doesn't exist in new array, so create it
$new_array[$field] = ['id' => [$id], 'field' => $field, 'value' => [$value]];
} else {
//key exists in new array, append new values
$new_array[$field]['id'][] = $id;
$new_array[$field]['value'][] = $value;
}
}
//reset array keys back to sequential
$new_array = array_values($new_array);
Output of $new_array would be
Array
(
[0] => Array
(
[id] => Array
(
[0] => 10
)
[field] => new
[value] => Array
(
[0] => pqr
)
)
[1] => Array
(
[id] => Array
(
[0] => 14
[1] => 17
)
[field] => test
[value] => Array
(
[0] => abc
[1] => xyz
)
)
)
So I got an array like:
Array
(
[0] => Array
(
[ids] => Array
(
[id] => id1
)
[name] => name1
[number] => 1
)
[1] => Array
(
[ids] => Array
(
[id] => id2
)
[name] => name2
[number] => 2
)
)
And I want to construct new multidimensional array based on the elements of it, but adding some new keys with empty values like(all the keys will have other names in new array, it's just simplified):
Array
(
[0] => Array
(
[id] => id1
[firstname] => name1
[lastname] =>
[somedata] =>
[somemoredata] =>
[ordernumber] => 1
)
[1] => Array
(
[id] => id2
[firstname] => name2
[lastname] =>
[somedata] =>
[somemoredata] =>
[ordernumber] => 2
)
)
How do I do it? Was thinking about array_push inside foreach loop, but it's not gonna do the job because of the empty keys I want and different order of elements. I also know how to access the nested value of [id] but still no idea about how to construct and move values to the new array for each element.
You can do it like below:-
$final_array = array();
foreach($array as $arr){
$final_array[] = array('id'=>$arr['ids']['id'],'firstname'=>$arr['name'],'lastname'=>'','somedata'=>'','somemoredata'=>'','ordernumber'=>$arr['number']);
}
print_r($final_array);
Output:-https://eval.in/831090
I have an array that I'd like to restructure. I want to group items by turn. I can figure out how to extract data from the array using foreach($arr['history'] as $obj) my issue is with populating a new array using a loop.
Currently it looks like this:
Array (
[history] => Array (
[id] => 23452435
[legend] => Array (
[0] => Array (
[player] => me
[turn] => 1
[card] => Array (
[name] => foo
)
)
[1] => Array (
[player] => me
[turn] => 1
[card] => Array (
[name] => bar
)
)
[2] => Array (
[player] => opponent
[turn] => 1
[card] => Array (
[name] => derp
)
)
[3] => Array (
[player] => opponent
[turn] => 2
[card] => Array (
[name] => hoo
)
)
)
))
I want it to look like the following, but I can't figure out how to automatically create and populate this structure. This is an array with a sub-array for each turn, containing an array for me and opponent
Array (
[0] => Array (
[me] => Array (
[0] => foo
[1] => bar
)
[opponent] = Array (
[0] => derp
)
)
[1] => Array (
[me] => Array ()
[opponent] => Array (
[0] => hoo
)
))
Thanks.
Edit:
This is what I needed. Thanks for the answers.
$result = [];
foreach ($arr['history'] as $historyItem) {
foreach ($historyItem['legend'] as $list) {
$result[$list['turn']][$list['player']][] = $list['card']['name'];
}
}
Try this:
$result = [];
foreach ($data['history']['legend'] as $list) {
$result[$list['turn']-1][$list['player']][] = $list['card']['name'];
}
Fiddle it! http://ideone.com/BtKOKJ
You can just start adding data to the new array. PHP is extremely forgiving.
$historyByTurns = array();
foreach ($arr['history'] as $historyItem) {
foreach ($historyItem['legend'] as $legendItem) {
$turn = $legendItem['turn'];
$player = $legendItem['player'];
if (!array_key_exists($turn, $historyByTurns)) {
$historyByTurns[$turn] = array();
}
if (!array_key_exists($player, $historyByTurns[$turn])) {
$historyByTurns[$turn][$player] = array();
}
foreach ($legendItem as $card) {
$historyByTurns[$turn][$player][] = $card['name'];
}
}
}
You will have to test it, as I have no way to do that ATM.
I want to compare two tabdelimeted files. I take the files and converts them into two arrays with the following structure:
Array 1
Array
(
[0] => Array
(
[name] => name1
[qty] => 200
)
[1] => Array
(
[name] => name2
[qty] => 9
)
[2] => Array
(
[name] => name3
[qty] => 3
)
[3] => Array
(
[name] => name4
[qty] => 1
)
)
Array 2
Array
(
[0] => Array
(
[name] => name1
[qty] => 180
)
[1] => Array
(
[name] => name2
[qty] => 9
)
)
How can I compare these two arrays and where the value is different to replace the value in the array 2 with array of value 1.
The easiest way to do this would be to create an associative array for the second set of data, instead of the array format you has used above. Since you only seem to have two "columns", and these are effectively a key/value relationship this should be nice and easy.
This example takes the two input arrays you have generated to do it, but you can probably adjust this so that you create the associative array directly as you read the second:
// first create an associative array from the second indexed array
$secondAssoc = array();
foreach ($secondArray as $row) {
$secondAssoc[$row['name']] = $row['qty'];
}
/*
$secondAssoc now looks like:
Array
(
[name1] => 180
[name2] => 9
)
*/
// Now loop the first array and update it
foreach ($firstArray as $rowId => $row) {
if (isset($secondAssoc[$row['name']]) && $secondAssoc[$row['name']] != $row['qty']) {
$firstArray[$rowId]['qty'] = $secondAssoc[$row['name']];
}
}
/*
$firstArray now looks like this:
Array
(
[0] => Array
(
[name] => name1
[qty] => 180
)
[1] => Array
(
[name] => name2
[qty] => 9
)
[2] => Array
(
[name] => name3
[qty] => 3
)
[3] => Array
(
[name] => name4
[qty] => 1
)
)
*/
See it working.
EDIT Here is a version that also creates an array, $modifiedItems, that holds only the items that have changed:
// first create an associative array from the second indexed array
$secondAssoc = array();
foreach ($secondArray as $row) {
$secondAssoc[$row['name']] = $row['qty'];
}
// Now loop the first array and update it
$modifiedItems = array();
foreach ($firstArray as $rowId => $row) {
if (isset($secondAssoc[$row['name']]) && $secondAssoc[$row['name']] != $row['qty']) {
$firstArray[$rowId]['qty'] = $secondAssoc[$row['name']];
$modifiedItems[] = array('name'=>$row['name'],'qty'=>$secondAssoc[$row['name']]);
}
}
See it working.
i have a multidimensional array whose index/keys (not the values) are like this:
this is how the submitted array looks
[param] => Array
(
[3] => groupedlista
[0] => groupedlistb
[2] => groupedlistc
)
[f_name] => Array
(
[3] => grouplistaa
[0] => grouplistbb
[2] => grouplistcc
)
[f_label] => Array
(
[3] => grouplistL3
[0] => grouplistL0
[2] => grouplistL2
)
this is how the order looks
0,2,3
i want that Result
[param] => Array
(
[0] => groupedlistb
[1] => groupedlistc
[2] => groupedlista
)
[f_name] => Array
(
[0] => grouplistbb
[1] => grouplistcc
[2] => grouplistaa
)
[f_label] => Array
(
[0] => grouplistL0
[1] => grouplistL2
[2] => grouplistL3
)
that's it
PS: i use a jquery sort / add / delete feature in the form and i prefer to do the final sorting php-based. the index array [$i] is required to be declared at the form.
$order = '0,2,3';
$out = array(); // This will hold the sorted values
$order = explode(',',$order); // Turn the order into an array
foreach ($multiDimArray as $key => $subArray) { // Loop outer array
foreach ($order as $pos) { // Loop order array
if (isset($subArray[$pos])) { // Make sure the key exists
$out[$key][] = $subArray[$pos]; // Put the correct value in the correct place
}
}
}
print_r($out);