How to combine two arrays like this? - php

$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.

Related

Move the last element in the node (of a multidimensional php array) at the beginning?

Hello guys i would to move the last element in the node (of a multidimensional php array) at the beginning...
This is my array:
$arr= array(
array(2,3,4,5,1),
array(3,4,5,6,2),
array(4,5,6,7,3)
);
and this is the output that i would to have:
Array (
[0] => Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
[1] => Array (
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
)
[2] => Array (
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
)
I created this code that works fine
$number= count($arr);
for($a= 0; $a < $number; $a++){
$element= $arr[$a][4];
unset($arr[$a][4]);
array_unshift($arr[$a],$element);
}
but i would know if there are other faster and efficient methods...
Thanks a lot and sorry for my english :)
Given the input you provided and the output, this is quicker to type:
array_map('sort', $arr);
Whether it runs faster or gives the desired result - I have no idea.
A correct answer that doesn't depend on the sort order of the values inside the array.
foreach($array as &$element) {
array_unshift($element,array_pop($element));
}

Split multidimensional arrays into multiple single arrays [closed]

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];
}

2 arrays count how many times id = date

Ive sat with this for a while now, and its getting late.
Im trying to get a top 3 of most sales from last month, and i need to count how many times a id from array 1 is equal to array 2 last month(6 = last atm.) like id 4 = 2, id 7 = 3
It might not be the perfect solution, but im just trying to break it down by my self, so later on 'maybe' problems, will i take care of when i hit the wall,
so please, if anyone can help me here, ill be greatfull.
UPDATE
- I will add the result im looking for here: (sorry i didnt before, it makes it alot easier :-)
- The result below, is because i want the count from 2014-06-01 and up to the last day of that month monly, on array[0][1] under this array, only 6-7-8 is not from 2014-06
Hope it makes a bit more sense now ^^
Array
(
[0] => Array
(
[0] => Array
(
[4] => 2
[7] => 3
[1] => 2
[3] => 2
[9] => 1
[12] => 1
[2] => 1
[13] => 1
)
)
)
Array
(
[0] => Array
(
[0] => Array
(
[0] => 4
[1] => 4
[2] => 7
[3] => 1
[4] => 7
[5] => 7
[6] => 3
[7] => 3
[8] => 4
[9] => 9
[10] => 12
[11] => 2
[12] => 13
[13] => 1
)
[1] => Array
(
[0] => 2014-06-18
[1] => 2014-06-10
[2] => 2014-06-05
[3] => 2014-06-05
[4] => 2014-06-12
[5] => 2014-06-11
[6] => 2013-12-12
[7] => 2014-07-23
[8] => 2014-05-13
[9] => 2014-06-01
[10] => 2014-06-12
[11] => 2014-06-04
[12] => 2014-06-04
[13] => 2014-06-11
)
)
)
I hope that what I understood is what you're really asking for. let's say your array definition is :
$arr = Array
(
0 => Array
(
0 => Array
(
0 => 4,
1 => 4,
2 => 7,
3 => 1,
4 => 7,
5 => 7,
6 => 3,
7 => 3,
8 => 4,
9 => 9,
10 => 12,
11 => 2,
12 => 13,
13 => 1
),
1 => Array
(
0 => "2014-06-18",
1 => "2014-06-10",
2 => "2014-06-05",
3 => "2014-06-05",
4 => "2014-06-12",
5 => "2014-06-11",
6 => "2013-12-12",
7 => "2014-07-23",
8 => "2014-05-13",
9 => "2014-06-01",
10 => "2014-06-12",
11 => "2014-06-04",
12 => "2014-06-04",
13 => "2014-06-11"
)
)
);
If you need to test if the date is lower than 6 month and then put their id, sales and date you need to use this code
$result = [];
$index=0;
foreach ($arr[0][0] as $key => $value)
{
$date1 = new DateTime($arr[0][1][$key]);
$date2 = new DateTime();
$diff = $date1->diff($date2);
$diff = ($diff->format('%y') * 12) + $diff->format('%m');
if($diff<=6)
{
$result[$index]['id'] = $key;
$result[$index]['sales'] = $value;
$result[$index]['date'] = $arr[0][1][$key];
$index++;
}
}
var_dump($result);
array_count_values() will give you the number of times each value appears in array 1.
$count = array_count_values($array[0][0]);
Result:
Array
(
[4] => 3
[7] => 3
[1] => 2
[3] => 2
[9] => 1
[12] => 1
[2] => 1
[13] => 1
)
Then you can use a loop to combine with array 2:
$result = array();
foreach($count as $key=>$val) {
$result[$array[0][1][$key]] = $val;
}
Result:
Array
(
[2014-06-12] => 3
[2014-07-23] => 3
[2014-06-10] => 2
[2014-06-05] => 1
[2014-06-01] => 1
[2014-06-04] => 1
[2014-06-11] => 1
)
See demo

Re-order PHP array by middle key as start (Circular Sorting)

very basic question however I have had some trouble finding the answers on PHP.NET.
I have the following array:
Array (
[1] => Array
(
[1] => 4
[2] => 1
[3] => 5
[4] => 3
)
[2] => Array
(
[5] => 2
[6] => 8
[7] => 7
[8] => 6
)
[3] => Array
(
[9] => 10
[10] => 9
[11] => 12
[12] => 11
)
[4] => Array
(
[13] => 15
[14] => 16
[15] => 14
[16] => 13
)
)
I want the array to be re-ordered so that the key number 3 in the first series of the array becomes the first, then the rest to be re-ordered from there to eventually get the result of:
Array (
[3] => Array
(
[9] => 10
[10] => 9
[11] => 12
[12] => 11
)
[4] => Array
(
[13] => 15
[14] => 16
[15] => 14
[16] => 13
)
[1] => Array
(
[1] => 4
[2] => 1
[3] => 5
[4] => 3
)
[2] => Array
(
[5] => 2
[6] => 8
[7] => 7
[8] => 6
)
)
I am looking for a way to do this so I can define the array, then the first level key I need to sort by, and then it will return the array in this way.
The standard PHP keys didn't seem to offer something like this, so it would be good to be able to have a separate function such as $newArray = reorder_array($array, $key);
I don't require any sorting of the second level, only the initial 4 main / first level array sections.
You help is greatly appreciated as I have been sitting on this one for awhile without a clear and simple solution.
You re-ordering can be simply implemented with one foreach loop, like:
function reorderArray($array, $key)
{
$found = false;
foreach($array as $k=>$v)
{
$found = $found || $k===$key;
if(!$found)
{
unset($array[$k]);
$array[$k] = $v;
}
//else break can be added for performance issues
}
return $array;
}
with usage
$array=[1=>'foo', 4=>'bar', 9=>'baz', 'test'=>51];
var_dump(reorderArray($array, 9));
var_dump(reorderArray($array, 'test'));
var_dump(reorderArray($array, 'no_such_key'));//original array in result
-check this demo. If keys are consecutive numerics, however, this can be easily implemented with array_slice() calls.

php values of one array to key of another array [duplicate]

This question already has answers here:
Merge two arrays as key value pairs in PHP
(3 answers)
Closed 10 months ago.
I have 2 arrays
$arr1 = Array
(
[0] => 12
[1] => 4
[2] => 8
[3] => xx
[4] => 1
[5] => 1year
[6] => 7
)
$arr2 = Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
)
I want to create a new array with the values of a2 as keys in $arr1.
My resultant array should be like this
$arr3 = Array
(
[1] => 12
[2] => 4
[3] => 8
[4] => xx
[5] => 1
[6] => 1year
[7] => 7
)
$arr3 = array_combine($arr2, $arr1);
print_r($arr3);
Next time please consult the manual first.
You need array_combine.
whats about looking into php manual? and find array_combine($keys, $values)

Categories