I use this function to display any kind of multi-dimensional array as a human readeable html table. For exemple, this array :
$arr = array
(
'root' => array(
'main' => array (
'members' => array(
array ('identifier' => '36', 'fullname' => 'jonathan carter'),
array ('identifier' => '42', 'fullname' => 'hello world')
)
)
)
);
becomes a table (with echo array2table($arr, true)) :
My problem, as you can see, is that there are too many table headers when multiple arrays contains only one sub array. This is what I would like to have :
You probably say: "What did you tried ?"
Well, before displaying the table header, I made and call a recursive function which returns an array of keys if there nested tables :
function nextTableHeaders($array)
{
// requires an array with only 1 child
if (!is_array($array) || count($array) !== 1)
return array();
$key = key($array);
return array_merge(array($key), nextTableHeaders($array[$key]));
}
In that case, it returns :
array('root', 'main', 'members');
Then I join the values with ' > ' and it becomes my new table headers. But I need then to go deeper into the arrays (because I don't wan't to display "main > members", and "members"). But I didn't succeed.
I would appreciate any help.
My suggestion is to keep the table printing as is, and to create a new function that will take the array and then convert it into the sort of array which when printed will display what you wish to be displayed.
function compressArray($array) {
// Non-arrays and multiple-key arrays are unaffected
if (!is_array($array) or count($array) !== 1)
return $array;
// This is a recursive algorithm
$child = compressArray(current($array));
// Compression unnecessary when child is non-array or a multiple-key array
if (!is_array($child) or count($child) !== 1)
return $array;
// Both parent and child are single key arrays so compress
$new_key = key($array) . " > " . key($child);
return array($new_key => current($child));
}
This will convert
$arr = array
(
'root' => array(
'main' => array (
'members' => array(
array ('identifier' => '36', 'fullname' => 'jonathan carter'),
array ('identifier' => '42', 'fullname' => 'hello world')
)
)
)
);
into
$arr = array
(
'root > main > members' => array(
array ('identifier' => '36', 'fullname' => 'jonathan carter'),
array ('identifier' => '42', 'fullname' => 'hello world')
)
);
Related
I want to convert my Associate Array into array based on certain criteria:
It should contain multiple column (Hence, can't user array_column in this case.)
It should ADD duplicate date
It should remove particular sub-array if NULL. (array_filter)
Input :
array (
0 =>
array (
'date' => "2019-03-31",
'a' => '1',
'b' => '1',
),
1 =>
array (
'date' => "2019-04-02", // If "SAME" date then ADD them
'a' => '1',
'b' => '1',
),
2 =>
array (
'date' => "2019-04-02", // If "SAME" date then ADD them
'a' => '2',
'b' => '1',
),
3 =>
array (
'date' => "2019-04-10",
'a' => '', // If "NULL" then remove the particular sub-array
'b' => '1',
),
4 =>
array (
'date' => "2019-04-18",
'a' => '4',
'b' => '10',
),
)
I've tried the following,
Although I was able to select multiple column in array but this is giving me:
"Duplicate" date.
And including sub-array which has "Null" value.
function colsFromArray(array $array, $keys)
{
if (!is_array($keys)) $keys = [$keys];
return array_map(function ($el) use ($keys) {
$o = [];
foreach($keys as $key){
$o[$key] = isset($el[$key])?$el[$key]:false;
}
return $o;
}, $array);
}
$result = colsFromArray($arr, array("date", "a"));
Required Output :
array (
0 =>
array (
'date' => "2019-03-31",
'a' => '1',
),
1 =>
array (
'date' => "2019-04-02",
'a' => '3',
),
2 =>
array (
'date' => "2019-04-18",
'a' => '4',
),
)
You could try something like this:
function colsFromArray(array $array, $keys)
{
if (!is_array($keys)) $keys = [$keys];
$returnArray = [];
foreach($array as $in){
foreach($keys as $key){
//Check if the key already has a value
if(isset($returnArray[$in['date']][$key]) && is_numeric($returnArray[$in['date']][$key])){
$returnArray[$in['date']][$key] += $in[$key];
}
//If the key is empty, continue
elseif(empty($in[$key])){
continue;
}
//If value is to be set, set date as well
else{
$returnArray[$in['date']]['date'] = $in['date'];
$returnArray[$in['date']][$key] = $in[$key];
}
}
}
//Return array after resetting indexes and removing empty entries
return array_values(array_filter($returnArray));
}
The way that you are passing in the array of columns to your customer function is ambiguous about what should be used to group by and what should be summed. OR if you will never have more than two elements in your 2nd parameter, then your custom function lacks flexibility.
Notice how #SverokAdmin's answer has date hardcoded in the logic, but there is no guarantee that this column name will always exist in the first parameter $array. Ergo, the utility function cannot be reliably used as a utility function.
Consider this custom function which provides your desired result, but allows more meaningful parameter declarations. My snippet allows you to group by one or more column, keep one or more columns in the subarrays, and specify which column should be summed. I cannot make the last column iterable because you state that if a column is missing a value, it should be omitted from the result. In other words, your desired result is unknown if you wanted to sum a and b, but one of the a values was empty.
Code: (Demo)
function colsFromArray(array $array, $groupBy, $keepables, $summable): array
{
$groupBy = (array)$groupBy;
$keepables = (array)$keepables;
// cannot make $summable iterable type because question is unclear on the desired outcome when one of the columns is empty
return array_values(
array_reduce(
$array,
function ($result, $row) use($groupBy, $keepables, $summable) {
$uniqueKey = [];
foreach ($groupBy as $col) {
$uniqueKey[] = $row[$col];
}
$uniqueKey = implode('_', $uniqueKey);
if (!$row[$summable]) {
return $result;
}
foreach ($keepables as $keepable) {
$result[$uniqueKey][$keepable] = $row[$keepable];
}
$result[$uniqueKey][$summable] = ($result[$uniqueKey][$summable] ?? 0) + $row[$summable];
return $result;
},
[]
)
);
}
var_export(
colsFromArray($arr, 'date', 'date', 'a')
);
Output:
array (
0 =>
array (
'date' => '2019-03-31',
'a' => 1,
),
1 =>
array (
'date' => '2019-04-02',
'a' => 3,
),
2 =>
array (
'date' => '2019-04-18',
'a' => 4,
),
)
how can I check and delete duplicate arrays?
Example:
$a = array(
array(
'id' => 1,
'name' => 'test'
),
// Next array is equal to first, then delete
array(
'id' => 1,
'name' => 'test'
),
// Different array, then continue here
array(
'id' => 2,
'name' => 'other'
)
);
If the array is the same, then delete the duplicate and get only one array.
You can use a lookup table storing serialized arrays. If an array already exists in the lookup table, you have a duplicate and can splice out the key:
$a = array(
array(
'id' => 1,
'name' => 'test'
),
array(
'id' => 1,
'name' => 'test'
),
array(
'id' => 2,
'name' => 'other'
)
);
$seen = [];
for ($i = count($a) - 1; $i >= 0; $i--) {
if (array_key_exists(json_encode($a[$i]), $seen)) {
array_splice($a, $i, 1);
}
else {
$seen[json_encode($a[$i])] = 1;
}
}
print_r($a);
Output:
Array
(
[0] => Array
(
[id] => 1
[name] => test
)
[1] => Array
(
[id] => 2
[name] => other
)
)
Try it!
array_unique()
Example:
$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)
You can loop through the parent array, serialize each child array and save it in a third array, and as you are looping, check for the existence of the serial of each next child array to all previous ones saved in the third array. If it exists, remove the current duplicate from the parent array by key. The function below demonstrates this.
function remove_duplicate_nested_arrays($parent_array)
$temporary_array = array(); // declare third, temporary array.
foreach($parent_array as $key => $child_array){ // loop through parent array
$child_array_serial = serialize($child_array); // serialize child each array
if(in_array($child_array_serial,$temporary_array)){ // check if child array serial exists in third array
unset($parent_array[$key]); // unset the child array by key from parent array if it's serial exists in third array
continue;
}
$temporary_array[] = $child_array_serial; // if this point is reached, the serial of child array is not in third array, so add it so duplicates can be detected in future iterations.
}
return $parent_array;
}
This can also be achieved in 1 line, using #Jose Carlos Gp suggestion as follows:
$b = array_map('unserialize', array_unique(array_map('serialize', $a)));
The function above kind of expands on what is actually happening in the 1 liner solution.
While renown scientists are looking into other dimensions I'm trying to figure out how to populate multidimensional arrays with dynamic data.
I need to add an undetermined number of values that are calculated from a random function.
function my_random ($min, $max) {
$random_var = rand($min, $max);
return $random_var;
}
I would like my array to look like this:
$array_example = [
'id' => $id,
'value1' => $value1,
'value2' => $value2
];
...or maybe like this:
$array[$i] = [
'id' => $id, array(
'value1' => $value1,
'value2' => $value2
)
];
I figured a simple for-loop would do the trick and so I tried this:
for ($i = 1; $i <= $amount; $i++) {
$array[$i] = $i;
$array[$i] = [
'id' => $i, array(
'value1' => $value1,
'value2' => $value2
)
];
}
...but it comes out all wrong (from console):
string(103) "{"id":2,"value1":[14,{"1":{"id":1,"0":{"value1":[14],"value2":[11]}}},13],"value2":[11,19]}"
The for-loop seems to nest them. I tried different functions, hoping to get it right: range() to get the id and then populate it with data, array_push() and I even tried to combine and merge.
This thread makes it look simple:
$array[] = "item";
$array[$key] = "item";
array_push($array, "item", "another item");
But this solution will only work to create the index.
How do I insert those values into each index dynamically? What I ultimately need is to be able to access the array and its values like this:
$array[0]["value1"].
Thanks.
Indexed array of associative arrays
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
The following function takes one optional integer argument, or defaults to 10, and initiates a for loop to create an indexed Array of associative array()s.
<!DOCTYPE html><html><head></head><body>
<?php
function createArrayOf( $quantity = 10 ) {
$arr = [];
for ( $i = 0; $i < $quantity; ++$i ) {
$arr[ $i ] = array(
"value_1" => "some value",
"value_2" => "another value"
);
}
return $arr;
}
?>
<pre><?php var_export( createArrayOf( 5 ) ); ?></pre>
</body></html>
Output using var_export() which:
gets structured information about the given variable. It is similar to var_dump() with one exception: the returned representation is valid PHP code.
array (
0 =>
array (
'value_1' => 'some value',
'value_2' => 'another value',
),
1 =>
array (
'value_1' => 'some value',
'value_2' => 'another value',
),
2 =>
array (
'value_1' => 'some value',
'value_2' => 'another value',
),
3 =>
array (
'value_1' => 'some value',
'value_2' => 'another value',
),
4 =>
array (
'value_1' => 'some value',
'value_2' => 'another value',
),
)
My problem is i have a multidimensional array posting from form to php, now i want to checking if duplicate values of some indexes exist in multi dimensional array or not?
e.g:
$data=Array
(
0 => Array
(
uid => '100',
name => 'Sandra Shush',
type => 'abc'
),
1 => Array
(
uid => '101',
name => 'Sandra Shushtext',
type => 'xyz'
),
2 => Array
(
uid => '100',
name => 'Sandra Shush',
type => 'abc'
)
);
here name and type of index 1 and 2 are same, so how can i check it?
I am familiar with
$key = array_search('abc', array_column($data, 'type'));
but it is for duplication of single column value in multi rows, in my situation if multi column of same rows same with multi column of any other row then record will be consider as duplicate.
Any help should be appreciated, Thanks in advance.
You can try using array_reduce by creating a key using your desired item keys:
$result = array_reduce($data, function ($carry, $item) {
$key = $item['uid'] . $item['type'];
$item['is_duplicate'] = isset($carry[$key]);
if ($item['is_duplicate']) {
$carry[] = $item;
} else {
$carry[$key] = $item;
}
return $carry;
}, []);
var_dump($result);
The easiest way, well at least the one I would use is to encode your arrays into md5 (or any other kind) string and compare those values. I think it is the most efficient in your case.
Example:
<?php
function arrayToString($array) {
$str = '';
if ( !is_array($array) )
return $str;
foreach ( $array as $key => $val ) {
$str .= $key . ':' . $val;
}
return $str;
}
$data=Array
(
0 => Array
(
'uid' => '100',
'name' => 'Sandra Shush',
'type' => 'abc'
),
1 => Array
(
'uid' => '100',
'name' => 'Sandra Shush',
'type' => 'xyz'
),
2 => Array
(
'uid' => '100',
'name' => 'Sandra Shush',
'type' => 'abc'
)
);
$temp = array();
foreach ( $data as $d ) {
array_push($temp, md5(arrayToString($d)));
}
$unique = array_unique($temp);
var_dump($unique); // prints unique array
This is a very fast designed approach and will find duplicates. Note that duplicates are elements which have the same value for the same key. So if any of uid, name or type match, they will be treated as duplicates. Therefore I adjust the third array element, because all elements in your array share the same values.
$data = [
....
2 =>
[
'uid' => '200',
'name' => 'Mandra Shush',
'type' => 'abcx'
]
];
$duplicates = [];
$valuesToCompare = ["uid", "name", "type"];
function equals($value, $toCompare, $keysToCompare)
{
foreach ($keysToCompare as $compareKey) {
if ($value[$compareKey] === $toCompare[$compareKey]) {
return true;
}
}
return false;
}
foreach ($data as $index => $element) {
foreach ($data as $indexInner => $elementToCompare) {
if ($index !== $indexInner) {
if (equals($element, $elementToCompare, $valuesToCompare)) {
$duplicates[] = [$index => $indexInner];
}
}
}
}
var_dump($duplicates);
This will output the following, which indicates we found 2 duplicates. Where element 0 is duplicate of 1, and 1 is duplicate of 0.
array (size=2)
0 =>
array (size=1)
0 => int 1
1 =>
array (size=1)
1 => int 0
I achieved above scenario like this:
Dont know which one is best mine or other's who posted answers.
foreach($data as $key => $row)
{
$combinedarr[] = array("name"=>$row["name"],"type"=>$row["type"]);
}
//chck if same facilitiy is being visit on same date twice
$countcomb = count($combinedarr);
$uniquearr = array_unique($combinedarr, SORT_REGULAR);
if($countcomb==count($uniquearr)){
}else{
//yes duplicate exists
};
Thanks again for those who answered.
I'm trying to insert a PHP array into a mysql database, but am having trouble with this particular array. I'm trying to create a function that takes
array( 0 => array ( 'col1' => 'value', 'col2' => 'value', 'col3' => 'value', 'col4' => value', 'col5' => 'value', 'col6' => array ( 'string' => array ( 'col7' => 'value' , 'col8' => 'value'), ), ),
1 => array ( 'col1' => 'value', 'col2' => 'value', 'col3' => 'value', 'col4' => array ( ), 'col5' => 'value', 'col6' => array ( 'string' => array ( ), ), ),
2 => array ( 'col1' => 'value', 'col2' => 'value', 'col3' => 'value', 'col4' => array ( ), 'col5' => 'value', 'col6' => array ( 'string' => array ( ), ), ), )
and seperates each numbered array and formats it as:
array( 'col1' => 'value', 'col2' => 'value', 'col3' => 'value', 'col4' => 'value', 'col5' => 'value', ['col6' => 'value','col7' => 'value',] )
array( 'col1' => 'value', 'col2' => 'value', 'col3' => 'value', 'col4' => 'value', 'col5' => 'value', ['col6' => 'value','col7' => 'value',] )
array( 'col1' => 'value', 'col2' => 'value', 'col3' => 'value', 'col4' => 'value', 'col5' => 'value', ['col6' => 'value','col7' => 'value',] )
depending on how many rows there are. Keep in mind these conditions:
+8 cols is just an example, the range can fluctuate greatly.
+Cols containing a array must be non existent if it empty, like in [1] and [2], but not [0].
+Any column could contain a empty or full array. if it contains a full array, it needs to be flatten while retaining it's value.
+Some arrays might have greater then 2 nested arrays. (elements)
+Not all of the arrays have nested arrays, they are already formatted correctly. These arrays can not be affected by the PHP function i'm trying to create.
I'm stumped, every function I've created has failed. Thanks everyone in advance.
UPDATE I used Var_export with this function to get the array above.
function flatten($array, $preserve_keys = false)
{
if (!$preserve_keys) {
$array = array_values($array);
}
$flattened_array = array();
foreach ($array as $k => $v) {
$flattened_array[$k] = $v;
if (is_array($v)) {
$flattened_array = array_merge($flattened_array, call_user_func(__FUNCTION__, $v, $preserve_keys));
} elseif ($preserve_keys) {
$flattened_array[$k] = $v;
} else {
$flattened_array[] = $v;
}
}
return (array)$flattened_array;
}
What you need is what's called a recursive function (see this question for more info: What is a RECURSIVE Function in PHP?).
Also it seems that each "col" in the array is a string (and not a number).
What you might want to try is check whether the first (or any) key in the array is numeric.
The code would look something like this:
public function extractArray($myArray){
$extractedArray = array(); // create a new array where all the flattened data will go
$arrayKeys = array_keys($myArray); // grab all the keys from the array
if (is_numeric($arrayKeys[0])): // if the first key is a number (it's nested)
$extractedArray = $myArray[0]; // remove one level of the array by grabbing everything from the first element
extractArray($extractedArray); // run our function again to check if we need to remove another layer
else:
$extractedArray = $myArray; // seems like there are no layers that need to be removed
endif;
return $extractedArray;
}
Edit:
Here is a possible solution for the second part of the problem (extracting values from possible sub-arrays)
Here are the steps we are going to take:
1) Loop through each element in our $extractedArray (from part 1) and see if it is a sub array
2) If it is an array we extract it's contents and place it in a temporary variable (an array for now)
3) We then call ourselves again (recursively) passing it the new temporary variable (until there are no more sub-arrrays left
4) Once we have extracted all the values and flattened them in to a single array, we can then loop through that array and store them as a string (or whatever else we like)
Updated Code
Here is the code:
public function flattenArrayValues($extractedArray){
$extractedValues = array();
foreach($extractedArray as $key => $eA):
if(is_array($eA)): // check whether current element is an array
reset($eA); // go to the first element of the array and grab it
$first_key = key($eA); // ...
$extractedValues[$key] = $eA[$first_key]; // this allows us to preserve the "cols" for each element
flattenArrayValues($extractedValues); //call ourselves again to check whether there are any sub-arrays
endif;
endforeach;
return $extractedValues; // seems like we extracted all the values and flattened them
// if we want we can loop through this new array and build a string out of it etc.
}
Hope this was helpful