I want to convert a multi array in 2-D array. I have following result of multi array.
Array
(
[1] => Array
(
[0] => Array
(
[0] => Id
[1] => Name
[2] => Fname
[3] => School
[4] => Photo
)
)
[2] => Array
(
[0] => Array
(
[0] => 32
[1] => kamal
[2] => hjhbg
[3] => hnp
[4] => B612_16.jpg
)
)
[3] => Array
(
[0] => Array
(
[0] => 33
[1] => dg
[2] => fa
[3] => f
[4] => bg.jpg
)
)
[4] => Array
(
[0] => Array
(
[0] => 35
[1] => mohit
[2] => bc
[3] => jhbvj
[4] => B612.jpg
)
)
)
Now I need to convert this array in below format.
Array
(
[0] => Array
(
[0] => Id
[1] => Name
[2] => Fname
[3] => School
[4] => Photo
)
[1] => Array
(
[0] => 32
[1] => kamal
[2] => hjhbg
[3] => hnp
[4] => B612_16.jpg
)
[2] => Array
(
[0] => 33
[1] => dg
[2] => fa
[3] => f
[4] => bg.jpg
)
[3] => Array
(
[0] => 35
[1] => mohit
[2] => bc
[3] => jhbvj
[4] => B612.jpg
)
)
Try this
function array_to1d($a) {
$out = array();
foreach ($a as $b) {
foreach ($b as $c) {
if (isset($c)) {
$out[] = $c;
}
}
}
return $out;
}
echo "<pre>"; print_r(array_to1d($array)); // $array your array name
The shortest solution would be using array_walk() here:
array_walk($array, function(&$v) {
$v = $v[0];
});
You just have to do
$two_d_array = array_values($three_d_array);
Related
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 7 months ago.
There are many references on S/O showing various methods to flatten a multidimensional recursive array (with more than two levels). I have been through dozens (and tried most) but I'm still running into an odd problem with every one I've tried. What I am getting as a result is:
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000043
[1] => 1000045
[2] => 1000050
)
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000030
[1] => 1000032
[2] => 1000058
[3] => 1000064
) ...
But what I'm expecting is a truly flattened single array:
Array
[0] => 1000043
[1] => 1000045
[2] => 1000050
[3] => 1000030
[4] => 1000032
[5] => 1000058
[6] => 1000064
)
The method I found on S/O is supposed to handle an "empty array" (which I assume is the problem) but I'm still getting the wrong output. Here is my code:
function array_flatten5(array $array)
{
$flat = array(); // initialize return array
$stack = array_values($array); // initialize stack
while($stack) // process stack until done
{
$value = array_shift($stack);
if (is_array($value)) // a value to further process
{
$stack = array_merge(array_values($value), $stack);
}
else // a value to take
{
$flat[] = $value;
}
}
return $flat;
}
Could someone point out what I missing here because I'm thinking it's something simple but at this point my eyes are crossed with the number of attempts I've made. Thank you for any help you can provide.
Here is the original array. It is 4-deep:
Array ( [0] => 1000043 [1] => 1000045 [2] => 1000050 ) Array ( [0] => 1000030 [1] => 1000032 [2] => 1000058 [3] => 1000064 ) Array ( [0] => 1000041 [1] => 1000059 [2] => 1000069 ) Array ( [0] => 1000021 [1] => 1000044 [2] => 1000049 [3] => 1000071 ) Array ( [0] => 1000009 [1] => 1000013 [2] => 1000015 [3] => 1000017 [4] => 1000053 ) Array ( [0] => 1000022 [1] => 1000034 [2] => 1000070 ) Array ( [0] => 1000038 [1] => 1000047 [2] => 1000055 [3] => 1000063 ) Array ( [0] => 1000019 [1] => 1000054 [2] => 1000060 [3] => 1000066 [4] => 1000068 ) Array ( [0] => 1000006 [1] => 1000014 [2] => 1000016 [3] => 1000072 ) Array ( [0] => 1000024 [1] => 1000025 [2] => 1000046 [3] => 1000061 [4] => 1000067 ) Array ( [0] => 1000028 [1] => 1000039 [2] => 1000048 ) Array ( [0] => 1000042 [1] => 1000057 ) Array ( [0] => 1000027 [1] => 1000033 [2] => 1000036 [3] => 1000037 ) Array ( [0] => 1000008 [1] => 1000010 [2] => 1000012 [3] => 1000018 ) Array ( [0] => 1000026 [1] => 1000062 [2] => 1000065 ) Array ( [0] => 1000020 [1] => 1000023 [2] => 1000031 [3] => 1000035 [4] => 1000040 ) Array ( [0] => 1000007 [1] => 1000011 [2] => 1000029 ) Array ( [0] => 1000051 [1] => 1000052 [2] => 1000056 ) Array ( [0] => 1000001 [1] => 1000002 [2] => 1000003 [3] => 1000004 [4] => 1000005 ) Array ( [0] => 1000073 )
And here is the outcome using the array_walk_recursive suggestion ...
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000111
[1] => 1000113
[2] => 1000129
[3] => 1000134
)
Array
(
)
Array
(
)
Array
(
[0] => 1000012
[1] => 1000085
)
Array
(
) ...
You didn't prepare suitable array, but looking on this code you need probably just array_walk_recursive() function.
$array = [
[1, 2, 3, 4],
[[5, 6], [7, 8]],
[[[9], [10]], [11]]
];
$result = [];
array_walk_recursive($array, function ($tempV) use (&$result) {
$result[] = $tempV;
});
print_r($result);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
)
here's the result of my array
Array
(
[1] => Array
(
[0] => ABC
[1] => abc
[2] => rodriguez
[3] => Barkleys, 15 NO.
[4] => A
[5] => 1234567890
[6] =>
[7] => YES
[8] => a
)
[2] => Array
(
[0] => DEF
[1] => def
[2] => DAM
[3] => Barkleys, 15 NO.
[4] => A
[5] => 1234567891
[6] =>
[7] => YES
[8] => b
)
[3] => Array
(
[0] => GHI
[1] => ghi
[2] => TEG
[3] => Street4
[4] => B
[5] => 1234567892
[6] => YES
[7] =>
[8] => c
)
[4] => Array
(
[0] => JKL
[1] => jkl
[2] => CHA
[3] => Street4
[4] => B
[5] => 1234567893
[6] => YES
[7] =>
[8] => d
)
)
I need some of values from this array by using other value for exp. if I have value of 4th index in two separate arrays.
array (
[0] => A
[1] => A
)
array (
[0] => B
[1] => B
)
Now I need to get values of 2nd index in two seperate array using above two array value
array (
[1] => rodriguez
[2] => DAM
)
array (
[1] => TEG
[2] => CHA
)
How can I do this please suggest solution.
You can try to do this way :
<?php
$firstArray = array();
foreach($bigArray as $key => $array) {
$value = $array[4]; //fetch the 4th indice
if(! isset($firstArray[$value])) {
$firstArray[$value] = array();
}
$firstArray[$value][$key] = $value;
}
$secondArray = array();
foreach($firstArray as $name => $array) {
foreach($array as $key => $info) {
$value = $bigArray[$key][2]; //fetch the 2nd indice
if(! isset($secondArray[$value])) {
$secondArray[$value] = array();
}
$secondArray[$value][$key] = $value;
}
}
Below array is formed when i fetch data from xsl sheet
i have used empty(), isset(), is_null but all are not working
bellow is my code
for($i = 0; $i < count($newSheetData); $i++){
if($newSheetData[$i][0] == ''){
unset($newSheetData[$i]);
}else{
//unset($newSheetData[$i]);
}
}
This is my output:
<pre/>Array
(
[0] => Array
(
[0] => quetion
[1] => option1
[2] => option2
)
[1] => Array
(
[0] => What
[1] => a
[2] => b
)
[2] => Array
(
[0] => now
[1] => a
[2] => b
)
[3] => Array
(
[0] => What
[1] => a
[2] => b
)
[4] => Array
(
[0] =>
[1] =>
[2] =>
)
[5] => Array
(
[0] =>
[1] =>
[2] =>
)
[6] => Array
(
[0] =>
[1] =>
[2] =>
)
[7] => Array
(
[0] =>
[1] =>
[2] =>
)
[8] => Array
(
[0] =>
[1] =>
[2] =>
)
[9] => Array
(
[0] =>
[1] =>
[2] =>
)
[10] => Array
(
[0] =>
[1] =>
[2] =>
)
)
)
i want this type of array
Array (
[0] => Array
(
[0] => quetion
[1] => option1
[2] => option2
)
[1] => Array
(
[0] => What
[1] => a
[2] => b
)
[2] => Array
(
[0] => now
[1] => a
[2] => b
)
[3] => Array
(
[0] => What
[1] => a
[2] => b
)
foreach ($newSheetData as $key => $value) {
$dataFiltred = array_filter($value, function($var) {
if ($var === 0 || $var === '0' || !empty($var)) {
return true;
}
return false;
});
if (empty($dataFiltred)) {
unset($newSheetData[$key]);
}
}
What I've tried so far : I have an array as shown below, what i wanted to do is, create multiple arrays based on the value BEGIN_DAY:
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
[6] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[7] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
and so on ...
I want to split this array into multiple different arrays when ever it begin with BEGIN_DAY so it should look like this:
The first :
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
The second :
[0] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[1] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
What I've tried so far :
$days=array();
$split_by='BEGIN_DAY';
foreach ($day_all as $key => $value) {
if($day_all[$key][0]==$split_by){
$days=array_slice($day_all, 0,$split_by);
}
}
var_dump($days);
Much Appreciated!
$sk = "first";
foreach ($array as $key=>$value)
{
if(in_array(BEGIN_DAY, $value)&&$key!=0)
{
$sk = "second";
}
$result[$sk][] = $value;
}
echo "<pre>";
print_r($result);
$first = $result['first'];
$second = $result['second'];
Something like this :
$yourArray = ...; //is your array as described above
$finalArray = [];
$tempArray = [];
for($idx = 0; idx<$yourArray .length; idx++){
$tempArray = $yourArray[$idx]
if($yourArray[$idx][0] == 'BEGIN_DAY'){
$finalArray[] = $tempArray;
$tempArray = [];
}
}
The final array will contain the arrays. Each time the BEGIN_DAY is found, the array will be inserted into the finalArray and a new one is created.
To improve the code, check the existence of the cell too, to avoid index exception.
consider below array:-
Array
(
[0] => Array
(
[0] => 99895
[1] => 35378
[2] => 0.01
)
[1] => Array
(
[0] => 99895
[1] => 813
[2] => -0.97
)
[2] => Array
(
[0] => 99895
[1] => 771
[2] => 0.29
)
[3] => Array
(
[0] => 442
[1] => 833
[2] => -1.06
)
[4] => Array
(
[0] => 442
[1] => 485
[2] => -0.61
)
[5] => Array
(
[0] => 442
[1] => 367
[2] => -0.14
)
[6] => Array
(
[0] => 442
[1] => 478
[2] => 0.77
)
[7] => Array
(
[0] => 442
[1] => 947
[2] => -0.07
)
[8] => Array
(
[0] => 7977
[1] => 987
[2] => 0.76
)
[9] => Array
(
[0] => 7977
[1] => 819
[2] => 0.37
)
[10] => Array
(
[0] => 7977
[1] => 819
[2] => 0.36
)
[11] => Array
(
[0] => 7977
[1] => 653
[2] => 1.16
)
[12] => Array
(
[0] => 7977
[1] => 1653
[2] => 1.15
)
)
from the above array how will I determine the below array?
array
(
99895 => -0.223
442 => -0.22
7977 => 0.76
)
Actually I need the average value of column 3 in respect of column 1.
First collect all the column 3 elements into an array keyed off column 1:
$arrays = array();
foreach ($input as $vals) {
$key = $vals[0];
$val = $vals[2];
if (isset($arrays[$key])) {
$arrays[$key][] = $val;
} else {
$arrays[$key] = array($val);
}
}
Now go through all of them, calculating the averages:
foreach ($arrays as &$array) {
$array = array_sum($array)/count($array);
}