How do I flatten an array from three dimension into two dimensions? - php

How would i flatten the following array. I want to get rid of 0 and 1, and just have 0-5 as they keys. I am using php by the way.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 182
)
[1] => Array
(
[id] => 183
)
[2] => Array
(
[id] => 185
)
[3] => Array
(
[id] => 184
)
[4] => Array
(
[id] => 186
)
)
[1] => Array
(
[0] => Array
(
[id] => 171
)
)
)

$flatArray = array();
foreach ($array as $l1Array){
foreach ($l1Array as $k => $v){
$flatArray[]=$v;
}
}

Related

How to Sort multidimensional array by value in php

Hi I need to Sort the multi dimensional array by its value My array is like this I need to group the array by its position to create a new array sorting is just confusing How do i create a sorted array from this
Array
(
[prodata] => Array
(
[4] => Array
(
[position] => 3
[products] => Array
(
[0] => 227
)
)
[3] => Array
(
[position] => 4
[products] => Array
(
[0] => 441
[1] => 54
)
)
)
[richtext] => Array
(
[1] => Array
(
[position] => 1
[header] => qwewqe
[content] => contentadaqsdas
)
)
)
I this array i need to sort by it position and create a new array,
Goal
Array
(
[0]=>Array(
[richtext] => Array
(
[position] => 1
[header] => qwewqe
[content] => contentadaqsdas
)
)
[1]=>Array(
[prodata] => Array
(
[position] => 3
[products] => Array
(
[0] => 227
)
)
)
[2]=>Array(
[prodata] => Array
(
[position] => 4
[products] => Array
(
[0] => 441
[1] => 54
)
)
)
)
How do I achieve this help
You can flat the multi dimension array first, then sort it. Demo
$flat_array = [];
foreach($multi_dimension_array as $k => $array){
foreach($array as $value){
$flat_array[] = array(
$k => $value
);
}
}
usort($flat_array,function($a,$b){return current($a)["position"] - current($b)["position"];});
print_r($flat_array);
Though this code works, But I suggest restruct the data source for better data format. Which can make it a easy work

Combine inner arrays into one

I have this array of userids. I want to combine the child userid arrays into one array within the parent arrays.
[0] => Array
(
[0] => Array
(
[userid] => 1610
)
[1] => Array
(
[userid] => 1614
)
[2] => Array
(
[userid] => 1616
)
)
[1] => Array
(
[0] => Array
(
[userid] => 1610
)
[1] => Array
(
[userid] => 1614
)
[2] => Array
(
[userid] => 1616
)
[3] => Array
(
[userid] => 1618
)
)
My desired result would look like...
[0] => Array
(
[userids] => Array
(
[0] => 1610
[1] => 1614
[2] => 1616
)
)
[1] => Array
(
[userids] => Array
(
[0] => 1610
[1] => 1614
[2] => 1616
[3] => 1618
)
)
Loop the array and use array_column to flatten the subarrays.
foreach($arr as $sub){
$res[][key($sub[0]) . "s"] = array_column($sub, 'userid');
}
var_dump($res);
https://3v4l.org/A68VA

Remove array with single element from assosiative array

I have an array
Array
(
[0] => Array
(
[0] => 96
[1] => ML based
[7] => Open
)
)
Array
(
[0] => Array
(
[0] => 97
[1] => Application
[7] => Open
)
)
Array
(
[0] => Array
(
[0] => 98
)
)
Array
(
[0] => Array
(
[0] => 99
)
)
I want to remove
Array
(
[0] => Array
(
[0] => 98
)
)
Array
(
[0] => Array
(
[0] => 99
)
)
from this array
I tried:
$data = array_map('array_filter', $rowData);
unset($data[0][0]);
Expected output:
Array
(
[0] => Array
(
[0] => 96
[1] => ML based )
[7] => Open
)
)
Array
(
[0] => Array
(
[0] => 97
[1] => Application
[7] => Open
)
)
any help would be Appreciated .
array_filter() will work. Try -
array_filter($array, function ($a) {
return count($a[0]) == 3; // return array with 3 elements only
});
Working code

Filter multidimensional php array with another array

I have an array $result as such:
[0] => Array (
[0] => Array (
[itemid] => 1
[name] => A
)
[1] => Array (
[itemid] => 2
[name] => B
)
)
[1] => Array (
[0] => Array (
[itemid] => 3
[name] => C
)
[1] => Array (
[itemid] => 2
[name] => B
)
)
and an array $items as such:
[0] => Array (
[itemid] => 2
[name] => B
)
[1] => Array (
[itemid] => 4
[name] => D
)
How do I remove all items from the $result array, that occur in the $items array? In this case, the $result would become:
[0] => Array (
[0] => Array (
[itemid] => 1
[name] => A
)
)
[1] => Array (
[0] => Array (
[itemid] => 3
[name] => C
)
)
Since the question is mostly code, here's some extra characters to make StackOverflow accept the question.
I think this is what you want. (Not tested yet)
<?php
foreach ($result as $key => $array) {
$result[$key] = array_diff($array, $items);
}
print_r($result);

php:array sum without foreach

I have an array like this
Array
(
[1_DAY_2017] => Array
(
[SAMSUNG] => Array
(
[0] => 549
[1] => 199
[2] => 999
)
[XIAOMI] => Array
(
[0] => 199
[1] => 2999
[2] => 499
)
)
[2_DAY_2017] => Array
(
[SAMSUNG] => Array
(
[0] => 699
[1] => 999
)
[LENOVO] => Array
(
[0] => 280
[1] => 2550
[2] => 849
)
)
[3_DAY_2017] => Array
(
[OPPO] => Array
(
[0] => 500
[1] => 599
)
[SAMSUNG] => Array
(
[0] => 799
)
)
[4_DAY_2017] => Array
(
[SAMSUNG] => Array
(
[0] => 1299
[1] => 499
[2] => 799
[3] => 2500
)
[OPPO] => Array
(
[0] => 299
[1] => 349
[2] => 499
)
)
[5_DAY_2017] => Array
(
[XIAOMI] => Array
(
[0] => 500
[1] => 270
[2] => 340
)
[VIVO] => Array
(
[0] => 4599
[1] => 299
)
)
[6_DAY_2017] => Array
(
[VIVO] => Array
(
[0] => 240
[1] => 1899
[2] => 759
[3] => 530
)
[OPPO] => Array
(
[0] => 999
)
)
[7_DAY_2017] => Array
(
[OPPO] => Array
(
[0] => 300
[1] => 252
[2] => 1290
)
[LENOVO] => Array
(
[0] => 570
[1] => 1300
[2] => 666
)
)
)
From this i want to get an array
$output= [SAMSUNG => 9341, XIAOMI => 4807]
Here each item contains sum of the items in nested array.
Currently my solution conatins more than 2 for each loops but is there any way to optimize this??
You can use array_sum, array_map and array_column functions to get the result
array_sum(array_map('array_sum', array_column($a, 'SAMSUNG')))
With single array_reduce function:
// $arr is your initial array
$result = array_reduce($arr, function($r, $v){
if (isset($v['SAMSUNG'])) $r['SAMSUNG'] += array_sum($v['SAMSUNG']);
if (isset($v['XIAOMI'])) $r['XIAOMI'] += array_sum($v['XIAOMI']);
return $r;
}, ['SAMSUNG' => 0, 'XIAOMI' => 0]);
$sumArray = array();
foreach ($YourArray as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
print_r($sumArray);

Categories