Simplify looping through an array which could be flat or multidimensional
Tag : php , By : user161314
Date : March 29 2020, 07:55 AM
With these it helps Often, I will have an array holding a set of values each of which I need to process. Sometimes, the array will only hold a single set, in which case each value needs to be put through the process. Other times, the array will hold many sets, in which case each value will be an array and each value of those arrays will need to be processed. Here is an example: , Wrap the single value into an array, then proceed as usual: foreach($array as $key => $value) {
if(!is_array($value)) $value = array($value);
foreach($value as $subkey => $subvalue) {
//Process $value/$subvalue here
}
}
|
PHP Array convert and simplify multidimensional array
Tag : php , By : pacorro2000
Date : March 29 2020, 07:55 AM
I wish this help you So just iterate over the original array and assemble the result; something like this: $categories = array();
foreach ($data as $itemId => $itemData) {
foreach ($itemData['categories'] as $categoryId) {
$categories[$categoryId][] = $itemId;
}
}
|
Simplify a multidimensional array to a string
Date : March 29 2020, 07:55 AM
Hope this helps You can use array_column followed by join or implode Try this : $string = join(',', array_column($array, 'tag_name'));
Array
(
[0] => tag-1
[1] => tag-2
[2] => tag-3
)
//$string = "tag-1,tag-2,tag-3"
|
php - Simplify select from multidimensional array according the preferred group of row values
Date : March 29 2020, 07:55 AM
it should still fix some issue On output, I want an array with row per unique height. If there is row with same ext and height (like in 0, 6) on input, just take the first value. If there are rows with the same height prefer jpg over png over gif over the rest (3, 9, 10). , Here is a simple function that can solve your problem function filterAndSort($input){
$output = array();
$extensionsWeght = array(
'jpg' => 0,
'png' => 1,
'gif' => 2
);
foreach ($input as $v) {
//Edit 1: skip if no hieght is present
if(!isset($v['height'])){
continue;
}
if (!isset($output[$v['height']])) {
//If it's a new height, add it
$output[$v['height']] = $v;
} else {
//else, choose the prefered ext [if they have the same ext, the first entry is already choosen]
if ($extensionsWeght[$output[$v['height']]['ext']] > $extensionsWeght[$v['ext']]) {
$output[$v['height']] = $v;
}
}
}
//Sort the ouput by height key
// usort($output, function($a, $b) {
// return $a['height'] - $b['height'];
//});
//Edit 2 :
//For sorting the array, as it is indexed with the height key,
// you can use the built in functions to sort it. (ksort for this case)
ksort($output);
return array_values($output);
}
|
How can manage multidimensional array in foreach() for tree view while multidimensional array form of multi level
Date : March 29 2020, 07:55 AM
wish of those help Indeed array_filter does not work recursively. Here is a recursive function that does the job of taking out null values: function array_filter_recursive($arr, $cb = null) {
if (empty($cb)) {
$cb = function ($el) {
return $el;
};
}
$result = [];
foreach($arr as $key => $val) {
if (is_array($val)) $val = array_filter_recursive($val, $cb);
if ($cb($val)) $result[$key] = $val;
}
return $result;
}
$result.= "{ 'name': '".$child_4."', 'title': '".$child_4."' ,";
function convert($arr) {
$result = [];
$byId = [];
function recurse(&$result, &$byId, $arr) {
if (!is_array($arr)) return;
if (isset($arr["gcn_id"])) {
$parent = $arr["referral_id"];
$record = [
"name" => $arr["fullname"],
"title" => $arr["gcn_id"]
];
$byId[$arr["gcn_id"]] = &$record;
if (isset($byId[$parent])) {
$byId[$parent]["children"][] = &$record;
} else {
$result[] = &$record;
}
} else {
foreach($arr as $child) {
recurse($result, $byId, $child);
}
}
}
recurse($result, $byId, $arr);
return $result;
}
$result = json_encode(convert($legs), JSON_PRETTY_PRINT);
echo $result;
[
{
"name": "Wendy-Lynn Barr",
"title": 11312,
"children": [
{
"name": "Pauline Blake",
"title": 11303
}
]
},
{
"name": "Ed Kilgour",
"title": 5064,
"children": [
{
"name": "Craig Corbitt",
"title": 11302,
"children": [
{
"name": "Kristi Iles",
"title": 5175,
"children": [
{
"name": "Joshua Iles",
"title": 6013
}
]
},
{
"name": "Chris Chowning",
"title": 6308,
"children": [
// ...etc
|