This is my first array
Array
(
[0] => Array
(
[0] => 1
[1] => Elite
[2] => Air-Con Bus
[3] => Monday
)
[1] => Array
(
[0] => 4
[1] => KBZ
[2] => Airplane
[3] => Wednesday
)
[2] => Array
(
[0] => 5
[1] => Yoma
[2] => Cruise
[3] => Tuesday
)
)
I want to be inner array[0] to the outer array key. Like the following array: Can I or not? Please suggest me.
Array(
[1] => Array
(
[0] => 1
[1] => Elite
[2] => Air-Con Bus
[3] => Monday
)
[4] => Array
(
[0] => 4
[1] => KBZ
[2] => Airplane
[3] => Wednesday
)
[5] => Array
(
[0] => 5
[1] => Yoma
[2] => Cruise
[3] => Tuesday
)
)
$new_array = array();
foreach ($old_array as $el) {
$new_array[$el[0]] = $el;
}
one way:
foreach ($array as $a){
$new[$a[0]]=$a;
}
Related
I have an array-
$arraydelivered =
Array
(
[0] => Array
(
[0] => 1
[1] => pending
[2] => January
)
[1] => Array
(
[0] => 4
[1] => pending
[2] => April
)
[2] => Array
(
[0] => 7
[1] => pending
[2] => July
)
[3] => Array
(
[0] => 10
[1] => pending
[2] => October
)
)
I want to resort this array dynamically to
Array
(
[0] => Array
(
[0] => 4
[1] => pending
[2] => April
)
[1] => Array
(
[0] => 7
[1] => pending
[2] => July
)
[2] => Array
(
[0] => 10
[1] => pending
[2] => October
)
[3] => Array
(
[0] => 1
[1] => pending
[2] => January
)
)
I have tried to find out the subarray based on which I know exactly from where to re-sort
foreach ($arraydelivered as $keyD => $valueD) {
if($valueD[0] == $cycle){
print_r($valueD);
}
}
This has given me the output -
Array ( [0] => 4 [1] => pending [2] => April )
Now I want to use this sub-array as the identifier to resort to the main array. Basically this sub-array will be the resort starting point for the big array.
Here is the solve array
$keyD1=0;
if(!empty($arraydelivered)){
foreach ($arraydelivered as $keyD => $valueD) {
if($valueD[0] == $cycle){
$keyD1= $keyD;
}
}
$outputsec = array_slice($arraydelivered, $keyD1);
$outputfirst = array_slice($arraydelivered, -4, $keyD1);
$finalarray= array_merge($outputsec,$outputfirst);
$arraydelivered=$finalarray;
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);
I have a dataset similar to this in which I am trying to replace the numeric key values within DATA to the corresponding values in COLUMNS. I can do this in a loop but I don't think I'm doing it in the most efficient way possible. Can anyone suggest any nice functions that I haven't considered to accomplish this?
Existing Style
stdClass Object
(
[COLUMNS] => Array
(
[0] => MATCHID
[1] => SEASON
[2] => COMPETITION
[3] => ROUNDID
[4] => ROUNDSORT
[5] => ROUNDNAME
)
[DATA] => Array
(
[0] => Array
(
[0] => 141627
[1] => 2013/2014
[2] => The Scottish Cup
[3] => 18
[4] => 11
[5] => Final
)
[1] => Array
(
[0] => 140895
[1] => 2013/2014
[2] => The Scottish Cup
[3] => 16
[4] => 10
[5] => Semi-Final
)
)
)
Desired Style
stdClass Object
(
[COLUMNS] => Array
(
[0] => MATCHID
[1] => SEASON
[2] => COMPETITION
[3] => ROUNDID
[4] => ROUNDSORT
[5] => ROUNDNAME
)
[DATA] => Array
(
[0] => Array
(
[MATCHID] => 141627
[SEASON] => 2013/2014
[COMPETITION] => The Scottish Cup
[ROUNDID] => 18
[ROUNDSORT] => 11
[ROUNDNAME] => Final
)
[1] => Array
(
[MATCHID] => 140895
[SEASON] => 2013/2014
[COMPETITION] => The Scottish Cup
[ROUNDID] => 16
[ROUNDSORT] => 10
[ROUNDNAME] => Semi-Final
)
)
)
foreach ($data->DATA as $key => $array) {
$data->DATA[$key] = array_combine($data->COLUMNS, $array);
}
$data is the object you showed.
Loop trough the data and combine the keys with the data, see array_combine
$data->DATA = array_map(function (array $entry) use ($data) {
return array_combine($data->COLUMNS, $entry);
}, $data->DATA);
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.
I'd like to sum products by the same ref number, but I have 3rd parameter like dimension e.g. (..., 24, 26mm,...) and I can't sum this values only when they have the same dimension. I tried this: Group a multidimensional array by a particular value? but how to sum values?
My array looks like this:
Array
(
[0] => Array
(
[0] => 2
[1] => 790180X
[2] => 26mm
)
[1] => Array
(
[0] => 4
[1] => 762182Z
)
[2] => Array
(
[0] => 2
[1] => 072182X
)
[3] => Array
(
[0] => 4
[1] => 660122Y
)
[4] => Array
(
[0] => 2
[1] => 790180X
[2] => 24mm
)
[5] => Array
(
[0] => 1
[1] => 225160Y
)
[6] => Array
(
[0] => 1
[1] => 244160Y
)
[7] => Array
(
[0] => 1
[1] => 225160Y
)
[8] => Array
(
[0] => 8
[1] => 954120Y
)
[9] => Array
(
[0] => 3
[1] => 072182X
)
)
I'd like to something like this:
Array
(
[0] => Array
(
[0] => 2
[1] => 790180X
[2] => 26mm
)
[1] => Array
(
[0] => 2
[1] => 790180X
[2] => 24mm
)
[2] => Array
(
[0] => 4
[1] => 762182Z
)
[3] => Array
(
[0] => 5
[1] => 072182X
)
[4] => Array
(
[0] => 4
[1] => 660122Y
)
[5] => Array
(
[0] => 2
[1] => 225160Y
)
[6] => Array
(
[0] => 1
[1] => 244160Y
)
[7] => Array
(
[0] => 8
[1] => 954120Y
)
)
The array in short version:
Array => TO => Array
( (
[0] => 2:790180X:26mm [0] => 2:790180X:26mm
[1] => 4:762182Z [1] => 2:790180X:24mm
[2] => 2:072182X [2] => 4:762182Z
[3] => 4:660122Y [3] => 5:072182X
[4] => 2:790180X:24mm [4] => 4:660122Y
[5] => 1:225160Y [5] => 2:225160Y
[6] => 1:244160Y [6] => 1:244160Y
[7] => 1:225160Y [7] => 8:954120Y
[8] => 8:954120Y )
[9] => 3:072182X
)
Just sum into a new array, using the various "grouping" fields as keys in the new array:
$sums = array();
foreach ($yourarray as $element) {
$sums[$element['dimension1']][$element['dimension2']]++;
}
For every dimension you need to 'group' by, you add a key to the $sums array.