Split multidimensional arrays into multiple single arrays [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to split the below array into 3 single arrays. However, the size of the array can be any number. The below array size is 3.
Array (
[0] => Array
(
[0] => a
[1] => bb
[2] => c
[3] => dd
[4] => ee
)
[1] => Array
(
[0] => dd
[1] => ff
[2] => hh
[3] => iji
[4] => kkk
[5] => a
[6] => cc
)
[2] => Array
(
[0] => ee
[1] => kk
[2] => iji
[3] => a
[4] => bb
[5] => lmn
[6] => ppq
[7] => xyz
)
)
Expected output:
Array1 (
[0] => a
[1] => bb
[2] => c
[3] => dd
[4] => ee
)
Array2 (
[0] => dd
[1] => ff
[2] => hh
[3] => iji
[4] => kkk
[5] => a
[6] => cc
)
Array3 (
[0] => ee
[1] => kk
[2] => iji
[3] => a
[4] => bb
[5] => lmn
[6] => ppq
[7] => xyz
)
The name of the individual array should be followed by a number that increments for each array.

You can break a 3d array into 3 separate arrays using list().
list($array1, $array2, $array3) = $mainArray;
Here's an example: https://3v4l.org/hQ1Te
Now if you don't know how many arrays are going to be in the input array you can do something like this...
for($i=0; $i<count($mainArray); $i++){
$variableName = "array$i";
$$variableName = $mainArray[$i];
}
var_dump($array1, $array2, $array3, $array4, ...);

So are you looking for something like this?
for ($i = 0; $i < count($inputArray); $i++) {
echo "Array ". $i;
echo "<pre>";
print_r($inputArray[$i]);
echo "</pre>";
}
Edit: ok now I get it, you want:
for ($i = 0; $i < count($inputArray); $i++) {
$name = "array" . ($i + 1);
$$name = $inputArray[$i];
}

Related

Single to multiple Array in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
In my foreach loop, I'm fetching data from csv file, after rendering it in the array, the data is rendered collectively, but I want it to show column wise as in csv file or I have shown in the below second code example:
Array(
[0] => op1
[1] => op2
[2] => ans 1
[3] => op4
[4] => op1
[5] => op2
[6] => ans 2
[7] => op4
[8] => op1
[9] => op2
[10] => ans 3
[11] => op4
[12] => op1
[13] => op2
[14] => ans 4
[15] => op4
[16] => op1
[17] => op2
[18] => ans 5
[19] => op4
[20] => op1
)
I want to this
Array
(
option[0]=>
Array(
[0] => op1
[1] => op2
[2] => ans 1
[3] => op4
)
option[1]=>
Array(
[0] => op1
[1] => op2
[2] => ans 2
[3] => op4
)
option[2]=>
Array(
[0] => op1
[1] => op2
[2] => ans 2
[3] => op4
)
option[3]=>
Array(
[0] => op1
[1] => op2
[2] => ans 2
[3] => op4
)
)
you can use array_chuck() function like this:
print_r(array_chunk($input_array, 4));
In your existing foreach loop use Modulus for divide array in groups.
$final_arr= array();
$new_key = 0;
foreach($csv_data as $key => $value){
$new_key = $new_key;
if($key % 4 == 0){
$new_key += 1;
}
$final_arr[$new_key][]= $value;
}
Here I assume you got your csv data in $csv_data this variable.
Hope it helps to you.
From str_getcsv's manual page, first two comments:
That does not produce the desired results, if fields contain
linebreaks.
Handy one liner to parse a CSV file into an array:
$csv = array_map('str_getcsv', file('data.csv'));
This will create an array of associative arrays with the first row
column headers as the keys.
$csv = array_map('str_getcsv', file($file));
array_walk($csv, function(&$a) use ($csv) {
$a = array_combine($csv[0], $a);
});
array_shift($csv); // remove column header

Graph represenation logic from array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Array output generated from database values.
Array ( [0] => Array ( [seldate] => 2019-04-28 [count] => 268 )
[1] => Array ( [seldate] => 2019-04-29 [count] => 366 )
[2] => Array ( [seldate] => 2019-04-30 [count] => 85 )
[3] => Array ( [seldate] => 2019-04-28 [count] => 93 )
[4] => Array ( [seldate] => 2019-04-29 [count] => 82 )
[5] => Array ( [seldate] => 2019-04-30 [count] => 31 )
[6] => Array ( [seldate] => 2019-04-28 [count] => 44 )
[7] => Array ( [seldate] => 2019-04-29 [count] => 44 )
[8] => Array ( [seldate] => 2019-04-30 [count] => 22 ) )
I need to create below
string output from above array for google LineChart.
"['2019-04-28', 268, 93, 44],
['2019-04-29', 366, 82, 44],
['2019-04-30', 85, 31, 22]"
Please help to create PHP Code logic.
First prepare your array as key being the date and the values being the values.
Then loop it again and write the lines to a new array with the imploded values.
Lastly output the lines with implode on comma and new line.
foreach($rows as $r){
$dates[$r['seldate']][] = $r['count'];
}
foreach($dates as $date => $vals){
$lines[] = "['" . $date . "', " . implode(", ", $vals) . "]";
}
echo implode(",\n", $lines);
Output:
['2019-04-28', 268, 93],
['2019-04-29', 366],
['2019-04-30', 85]
https://3v4l.org/14smE
You can traverse array by using array_walk as below
$result = [];
array_walk($arr, function($item) use(&$result){
if(empty($result[$item['seldate']])){ // if empty add date and count for initialisation
$result[$item['seldate']] = [$item['seldate'], $item['count']];
}else{
$result[$item['seldate']][] = $item['count']; // just append value to same key as date as we are grouping it by date
}
});
$result = array_values($result); // to remove date keys
print_r($result);
Output
Array
(
[0] => Array
(
[0] => 2019-04-28
[1] => 268
[2] => 93
[3] => 44
)
[1] => Array
(
[0] => 2019-04-29
[1] => 366
[2] => 82
[3] => 44
)
[2] => Array
(
[0] => 2019-04-30
[1] => 85
[2] => 31
[3] => 22
)
)
Demo
Note: If you want to use this data for linechart, json_encode above output.

Reorganise array, move indexes to specific locations php

I have an arbitrary number of arrays all containing the same format of data. There are 2 separate for loops looping through two separate SQL query results and adding them to 2 separate arrays.
Once I have all the information in both arrays, I am walking through them and joining them together to make a longer array.
However, as I am writing this array to a csv file, The information needs to be in order in the array so it writes it in order to the csv file. How can I do this?
Array 1
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
)
//etc
Array 2
[1] => Array
(
[0] => RTGH707321222
[1] => THIS
[2] => IS
[3] => TEXT
)
[2] => Array
(
[0] => RTGH707321220
[1] => SOME
[2] => WORDS
[3] => HERE
)
//etc
Joining the arrays together
array_walk($array2, function($values, $key) use (&$array1) {
$array1[$key] = array_merge($array1[$key], $values);
} );
After The array Merge - print_r($array1)
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
[6] => RTGH707321222
[7] => THIS
[8] => IS
[9] => TEXT
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
[6] => RTGH707321220
[7] => SOME
[8] => WORDS
[9] => HERE
)
//etc
So this is working fine. However, I would like to move some of these indexes around so that they are in a different order. I have looked into array_splice() but I am not sure if this is the correct method to use.
What I want it to look like
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => RTGH707321222
[2] => TEXT
[3] => THIS
[4] => 18.36
[5] => 98.46
[6] => Foo
[7] => 32.63
[8] => IS
[9] => Bar
)
//etc
As you can see, all the information is still the same. The values have just been moved to different indexes. How can I sort the array so that it looks like the above. Can anyone point me in the right direction? Thanks.
This is a simpler method using array_replace() and an ordering array.
No extra loop, no temporary swapping variables.
Code: (Demo)
$array1=[
1=>['2017-07-21 00:00:00','Foo','Bar',32.63,18.36,98.46],
2=>['2017-07-21 00:00:00','Foo','Bar',29.74,148.68,178.42]
];
$array2=[
1=>['RTGH707321222','THIS','IS','TEXT'],
2=>['RTGH707321220','SOME','WORDS','HERE']
];
$order=[0=>'',6=>'',9=>'',7=>'',4=>'',5=>'',1=>'',3=>'',8=>'',2=>''];
array_walk($array2, function($values, $key) use (&$array1,$order) {
$array1[$key] = array_replace($order,array_merge($array1[$key], $values));
});
var_export($array1);
we can use swap technice here like,
<?php
foreach ($arr as $key => $value) {
$swap = $value[1];
$arr[$key][1] = $value[6];
$arr[$key][6] = $swap;
$swap = $value[9];
$arr[$key][9] = $value[2];
$arr[$key][2] = $swap;
$swap = $value[7];
$arr[$key][7] = $value[3];
$arr[$key][3] = $swap;
}
print_r($arr);
?>
$arr is your array.

How to combine two arrays like this?

$blueswards array:
Array ( [0] => 3 [1] => 8 [2] => 1 [3] => 4 [4] => 9 )
$redswards array:
Array ( [0] => 2 [1] => 9 [2] => 3 [3] => 6 [4] => 9 )
What i try to do:
Array ( [0] => 3 [1] => 8 [2] => 1 [3] => 4 [4] => 9 [5] => 2 [6] => 9 [7] => 3 [8] => 6 [9] => 9 )
I cant do it with array_merge.
EDIT = Sorry everyone i did it with array merge now its working. :[
I used array_merge() using your array values. And, it worked.
<?php
$new_array[] = array();
$blueswards = array(3,8,4,9);
$redswards = array(2,9,3,6,9);
$new_array = array_merge($blueswards,$redswards);
print_r($new_array);
?>
OUTPUT:
Array ( [0] => 3 [1] => 8 [2] => 4 [3] => 9 [4] => 2 [5] => 9 [6] => 3 [7] => 6 [8] => 9 )
Why don't you just run them in a loop once they're populated.
$newarray=0;
$arrayCounter=0;
for (i=0; i<count($redswards)-1 ; i++){
$newarray[$arrayCounter] = $redswards[i];
$arrayCounter++
}
for (i=0; i<count($blueswards)-1 ; i++){
$newarray[$arrayCounter] = $blueswards[i];
$arrayCounter++
}
There... That will do it correctly by simply adding on to the new array. It will also count the number of times it has to loop so you don't have to hardcode that.
I mean, that's really simple, and you may have to change the hardcoded i values. But that's the basic idea.
Used durbnpolsns code and edited it to work.
$newarray=0;
for ($i=0; $i<4 ; $i++){
$newarray[$i] = $redswards[$i];
}
$j=0;
for ($i=5; $i<8 ; $i++){
$newarray[$i] = $blueswards[$j];
$j++;
}
I have not tested the code and formatting may be impossible, but I'm typing on my phone.
Sorry for that.

Sorting Multidimensional Array by Specific Key

EDIT: For anyone who might come across this post with a similar problem, It was solved by taking konforce's supplied answer and tweaking around a bit with the custom sorting function:
function cmp($a, $b) {
if ($a[5] == $b[5]) {
return ($a[3] < $b[3]) ? -1 :1;
}
return ($a[5] > $b[5]) ? -1 : 1;
}
Notice $a[5] == $b[5] does not return zero. It was changed to check who has the most losses and then sort it in ASC order. I'm sure you can even keep going and add another if-statement in there in-case they have the same losses.
Lastly, all you do is usort($ARRAY, "cmp"); and finito!!!
Original Post
My apologies for coming up with yet another MD Array sorting question but I'm just not getting it. I've searched aplenty
for a solution and although many sites have provided what seemed like a logical answer I still have not been able to figure it out.
My problem is since I'm still learning its been rather difficult for me to grasp the concept of using usort with a custom comparing
function. Atleast, thats what I have seen the most when others have tried to sort MD Arrays.
I'm working on a small project to sharpen up on my php skills. Its a very basic tournament standings script that holds a team's information within an array. I would like to sort the array by most points($array[X][X][5]).
So the array looks something like this:
Array (
[0] => Array (
[0] => Array (
[0] => cooller
[1] => 6
[2] => 6
[3] => 0
[4] => 0
[5] => 18
)
)
[1] => Array (
[0] => Array (
[0] => strenx
[1] => 9
[2] => 5
[3] => 1
[4] => 3
[5] => 18
)
)
[2] => Array (
[0] => Array (
[0] => rapha
[1] => 10
[2] => 8
[3] => 1
[4] => 1
[5] => 25
)
) [3] => Array (
[0] => Array (
[0] => ronald reagan
[1] => 5
[2] => 4
[3] => 0
[4] => 1
[5] => 13
)
)
)
I would like to sort it by most points(cell #5), so it would look like this after sorting:
Array (
[0] => Array (
[0] => Array (
[0] => rapha
[1] => 10
[2] => 8
[3] => 1
[4] => 1
[5] => 25
)
)
[1] => Array (
[0] => Array (
[0] => cooller
[1] => 6
[2] => 6
[3] => 0
[4] => 0
[5] => 18
)
)
[2] => Array (
[0] => Array (
[0] => strenx
[1] => 9
[2] => 5
[3] => 1
[4] => 3
[5] => 18
)
)
[3] => Array (
[0] => Array (
[0] => ronald reagan
[1] => 5
[2] => 4
[3] => 0
[4] => 1
[5] => 13
)
)
)
The player with 25 points would be at the top, followed by 18, 18, and lastly 13. Sorry for my earlier post, was having difficulty wording my question correctly. Thanks in advanced!
I think you want something like this:
usort($standings, function($a, $b) { return $b[0][5] - $a[0][5]; });
Or prior to PHP 5.3:
function cmp($a, $b) { return $b[0][5] - $a[0][5]; }
usort($standings, 'cmp');
When using usort, the $a and $b parameters will be one "layer" into the supplied array. So in your case, an example of $a or $b will be:
[0] => Array (
[0] => cooller
[1] => 6
[2] => 6
[3] => 0
[4] => 0
[5] => 18
)
I'm not sure why you have an extra containing array there, but as you can see, you want to sort based on the [0][5] position.
usort($standings[0][0][5], 'cmp') won't work because the first parameter isn't an array to sort, it's just a single number, the points.

Categories