creating new array with additional keys based on existing multidimensional array PHP - php

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

Related

php formatting array results

I have an array like the following. This is the results of a query on one of our servers.
Array
(
[count] => 1
[0] => Array
(
[name] => Array
(
[count] => 1
[0] => mac
)
[0] => name
[staffid] => Array
(
[count] => 1
[0] => 1234
)
[1] => staffid
[school] => Array
(
[count] => 1
[0] => western
)
[2] => school
[count] => 3
[dn] => cn=mac,cn=staff
)
)
How do I loop through this array and create a new array as follows.
Array
(
[name] => mac
[staffid] => 1234
[school] => western
)
I've tried a foreach loop echoing the key & values, but I'm not sure where to go from there. There will be more results returned as the query is expanded, but original array layout will be the same and the new layout needs to be the same format.
Any ideas ?
Thanks
Try this:
$result = array();
foreach($yourArray as $element){
for($i=0;$i<$element['count']; $i++){
unset($element[$element[$i]]['count']);
$result[$element[$i]] = implode(', ', $element[$element[$i]]);
}
}

Multidimensional Array - Get unique values, but count all duplicates

I have a multi-dimensional array that I would like to get unique sub-values from, but also have a count of how many times those unique sub-values occurred.
For instance, this would be my starting array:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 3333333333333333
)
)
[1] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 5555555555555555
)
)
[2] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 77777777777777777
)
)
In the end, I'd like to have an array that looks like this:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
[count] => 3
)
[1] => Array
(
[id] => 3333333333333333
[count] => 1
)
[2] => Array
(
[id] => 5555555555555555
[count] => 1
)
[3] => Array
(
[id] => 77777777777777777
[count] => 1
)
)
Is there any general/easy way to do this without iterating through the first array for each value, comparing/storing the values in a temporary array, checking them, and adding to the count?
To get this exact format you may need to iterate thought your current array and do the counting manually, however php has the array_count_values() and array_unique() functions for this kind of thing:
http://php.net/manual/en/function.array-count-values.php
http://php.net/manual/en/function.array-unique.php
Because you are only concerned with the deepest values of the array, using array_walk_recursive seems suitable for this. Note that a reference to the output array $counted is used in the callback.
array_walk_recursive($ids, function($id, $k) use (&$counted) {
$counted[$id] = isset($counted[$id]) ? $counted[$id] + 1 : 1;
});
Using the id as the key in the $counted array will simplify the counting. The result of this will be somewhat different from your suggested output, but in my opinion it would actually be simpler to use. (e.g. foreach ($counted as $id => $count) {...).
$counted = array(
"1533438473619168" => 3
"3333333333333333" => 1
"5555555555555555" => 1
"77777777777777777" => 1);

Remove duplicated values from nested array

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);

Comparing&editing two tabdelimeted files with PHP?

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.

How to rationalise a multidimensional array in php?

I've been trying this all day!
How would I convert the top multidimensional array into the bottom.
Array (
[0] => Array ( [id] => 34 [email] => a#example.com )
[1] => Array ( [id] => 34 [email] => b#example.com )
[2] => Array ( [id] => 33 [email] => c#example.com )
[3] => Array ( [id] => 33 [email] => d#example.com )
[4] => Array ( [id] => 33 [email] => e#example.com )
)
Array (
[0]=>Array ([id] => 34 [email] => Array ([0]=> a#example.com [1]=>b#example.com )
[1]=>Array ([id] => 33 [email] => Array ([0]=> c#example.com [1]=>d#example.com [2]=>e#example.com)
)
Many thanks.
$new_array = array();
foreach ($orig_array as $child) {
$new_array[$child['id']][] = $child['email'];
}
$final_array = array();
foreach($new_array as $child) {
$final_array[] = $child;
}
The first loop produces an array keyed off the id fields, and simply pushes each email address onto it. The second loop then takes that intermediate array and wraps another array around it for the 0,1,etc... keys.
Would not just using keys in order to store IDs be an easier way to do that? Like this:
Array (
[34]=>Array ([email] => Array ([0]=> a#example.com [1]=>b#example.com )
[33]=>Array ([email] => Array ([0]=> c#example.com [1]=>d#example.com [2]=>e#example.com)
)
Then grouping emails would become a trivial task.

Categories