I have this array
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
[2] => Array
(
[0] => 5
[1] => 6
)
[3] => Array
(
[0] => 7
[1] => 8
)
)
I want this array to be looks like this one:
Array
(
[0] => Array
(
[0] => 1
[1] => 5
)
[1] => Array
(
[0] => 3
[1] => 7
)
[2] => Array
(
[0] => 2
[1] => 6
)
[3] => Array
(
[0] => 4
[1] => 8
)
)
I've spent almost 24 hours to accomplish this task. As you can understand, I need help. Please don't abuse on me by asking what have you tried. Can anyone accomplish this array job? thanks
Edit:
I have this array:
$yourArray = array(
array(1,2),
array(3,4),
array(5,6),
array(7,8),
);
It outputs this one:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
[2] => Array
(
[0] => 5
[1] => 6
)
[3] => Array
(
[0] => 7
[1] => 8
)
)
I want $yourArray to outputs this one:
Array
(
[0] => Array
(
[0] => 1
[1] => 5
)
[1] => Array
(
[0] => 3
[1] => 7
)
[2] => Array
(
[0] => 2
[1] => 6
)
[3] => Array
(
[0] => 4
[1] => 8
)
)
Let me write this too, I get output by using print_r($yourArray). And of course by using keyboard. Hope this helps
Considering what OP has made us understand, and quite seriously, I think this is what you're looking for:
$yourArray = array(
array(1,5),
array(3,7),
array(2,6),
array(4,8),
);
var_dump($yourArray);
Demo.
I really can not understand whether it is serious or not.
Explain yourself.
Here you go:
<?php
$array = array(
array(1,2),
array(3,4),
array(5,6),
array(7,8)
);
function processArray(&$array) {
for ($i = 0; $i < count($array); $i++) {
if ($array[$i][0] > 4) {
$array[$i][0] = $array[$i][0] - 3;
}
if ($array[$i][1] < 5) {
$array[$i][1] = $array[$i][1] + 3;
}
}
}
processArray($array);
print_r($array);
Outputs:
Array ( [0] => Array ( [0] => 1 [1] => 5 ) [1] => Array ( [0] => 3 [1] => 7 ) [2] => Array ( [0] => 2 [1] => 6 ) [3] => Array ( [0] => 4 [1] => 8 ) )
I don't see the pattern so the only possibility I see is doing it by hand.
$foo = [
[1,2],
[3,4],
[5,6],
[7,8]
];
$bar = [
[$foo[0][0],$foo[2][0]],
[$foo[1][0],$foo[3][0]],
[$foo[0][1],$foo[2][1]],
[$foo[1][1],$foo[3][1]]
];
print_r($bar);
Edit
Use array() instead of [ ] if you're using an older version of PHP.
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
)
I have 2 different arrays with different dimensions that i need to merge in order to get a result with a specific this structure:
the first:
Array
(
[0] => Array
(
[0] => 2017-11-03
[1] => 2017-11-05
[2] => 1
)
[1] => Array
(
[0] => 2017-11-23
[1] => 2017-11-25
[2] => 1
)
)
The second:
Array
(
[0] => 2017-12-26
[1] => 2018-01-30
)
The result should be :
Array
(
[0] => Array
(
[0] => 2017-11-03
[1] => 2017-11-05
[2] => 1
)
[1] => Array
(
[0] => 2017-11-23
[1] => 2017-11-25
[2] => 1
)
[2] => Array
(
[0] =>2017-12-26
[1] => 2018-01-30
[2] => 1
)
)
I tried using array_merge but it does not work because they have not the same dimension. Also I need an element in the second tab ([2] => 1).
<?php
$array=Array
(
0 => Array
(
0 => '2017-11-03',
1 => '2017-11-05',
2 => '1',
),
1 => Array
(
0=> '2017-11-23',
1 => '2017-11-25',
2 => '1'
),
);
$arraySmall=Array
(
0 => '2017-12-26',
1 => '2018-01-30'
);
array_push($arraySmall, "1");
array_push($array, $arraySmall);
echo'<pre>';
print_r($array);
And the output is :
Array
(
[0] => Array
(
[0] => 2017-11-03
[1] => 2017-11-05
[2] => 1
)
[1] => Array
(
[0] => 2017-11-23
[1] => 2017-11-25
[2] => 1
)
[2] => Array
(
[0] => 2017-12-26
[1] => 2018-01-30
[2] => 1
)
)
This way can work even without this line array_push($arraySmall, "1");
You can give it a try. In order to "merge" you need same size but for "push" you don't. So if you commend the line i told you the output will look like this:
Array
(
[0] => Array
(
[0] => 2017-11-03
[1] => 2017-11-05
[2] => 1
)
[1] => Array
(
[0] => 2017-11-23
[1] => 2017-11-25
[2] => 1
)
[2] => Array
(
[0] => 2017-12-26
[1] => 2018-01-30
)
)
What you describe is appending, not merging. Try this:
$arraySecond[] = 1; // This adds [2]=> 1
$arrayFirst[] = $arraySecond; // This adds second array to end of first
for your given example:
$x = ... // first array [2 dimensions]
$y = ... // second array [1 dimension]
$y = array_merge($y, array_diff($x[0], $y)); // add missing '1' to $y or any other key that are present in elements of $x and have to be added to $y
$x[] = $y; // append the 1-dim array as the new element in $x
I have 2 PHP arrays that look like this..
$array1
--------
Array
(
[0] => Array
(
[0] => 64
[1] => Apple
)
[1] => Array
(
[0] => 22
[1] => Pear
)
[2] => Array
(
[0] => 3
[1] => Raisin
)
[3] => Array
(
[0] => 15
[1] => Grape
)
[4] => Array
(
[0] => 11
[1] => Banana
)
[5] => Array
(
[0] => 4
[1] => Orange
)
)
$array2
--------
Array
(
[0] => Array
(
[0] => 22
[1] => Pear
)
[1] => Array
(
[0] => 11
[1] => Banana
)
)
I want to merge the arrays together but put the matching items from $array2 at the top so the result would look like this...
$array3
-------
Array
(
[0] => Array
(
[0] => 22
[1] => Pear
)
[1] => Array
(
[0] => 11
[1] => Banana
)
[2] => Array
(
[0] => 64
[1] => Apple
)
[3] => Array
(
[0] => 3
[1] => Raisin
)
[4] => Array
(
[0] => 15
[1] => Grape
)
[5] => Array
(
[0] => 4
[1] => Orange
)
)
I'm not sure how to approach, should I merge the two first and then try and do some ordering, or is there a more efficient approach?
Get the 2nd array and then a rest of the 1st array
array_merge($arr2, array_udiff($arr1, $arr2, function($i1, $i2) {return $i1[0]-$i2[0];}));
So long story short, I have the array $total which looks like this:
Array (
[0] => Array
(
[0] => 1
[1] => 3
)
[1] => Array
(
[0] => 6
[1] => 7
[2] => 8
)
[2] => Array
(
[0] => 9
[1] => 10
)
)
However I need to pass it to a function that looks like this:
$count = cartesian(
Array("GH20"),
Array(1,3),
Array(6,7,8),
Array(9,10)
);
I have asked a similar question but I suspect the answer they gave was correct, I do not think asked the question in the right way. The cartesain function does not seem to accept:
$count = cartesian(
Array($total[0]),
Array($total[1]),
Array($total[2])
);
After some digging around I believe this is down to the fact that the arrays have keys which is not causing an error however instead of any values being returned, it just returns "array" for everything.
My question, in two parts then, is how do I remove the keys from the arrays so it is formatted as required. If possible, could the be done dynamically?
I have tried what everybody suggestes on google which is use the array_values() function, but this did not help
Any help is greatly appreciated, thanks, Nick
As requested, casertain function:
function cartesian() {
$_ = func_get_args();
if(count($_) == 0)
return array(array());
$a = array_shift($_);
$c = call_user_func_array(__FUNCTION__, $_);
$r = array();
foreach($a as $v)
foreach($c as $p)
$r[] = array_merge(array($v), $p);
return $r;
}
This is my desired output (Every permutation of ($total):
Array (
[0] => Array (
[0] => GH20
[1] => 1
[2] => 6
[3] => 9
)
[1] => Array (
[0] => GH20
[1] => 1
[2] => 6
[3] => 10
)
[2] => Array (
[0] => GH20
[1] => 1
[2] => 7
[3] => 9
)
[3] => Array (
[0] => GH20
[1] => 1
[2] => 7
[3] => 10
)
[4] => Array (
[0] => GH20
[1] => 1
[2] => 8
[3] => 9
)
[5] => Array (
[0] => GH20
[1] => 1
[2] => 8
[3] => 10
)
[6] => Array (
[0] => GH20
[1] => 3
[2] => 6
[3] => 9
)
[7] => Array (
[0] => GH20
[1] => 3
[2] => 6
[3] => 10
)
[8] => Array (
[0] => GH20
[1] => 3
[2] => 7
[3] => 9
)
[9] => Array (
[0] => GH20
[1] => 3
[2] => 7
[3] => 10
)
[10] => Array (
[0] => GH20
[1] => 3
[2] => 8
[3] => 9
)
[11] => Array (
[0] => GH20
[1] => 3
[2] => 8
[3] => 10
)
)
I am terribly sorry but the array looks like this:
Array
(
[1] => Array
(
[0] => msie6.0
[1] => 7
)
[2] => Array
(
[0] => safari5.0.3
[1] => 5
)
[3] => Array
(
[0] => chrome18.0.1025.308
[1] => 1
)
[4] => Array
(
[0] => firefox20.0
[1] => 4
)
[5] => Array
(
[0] => msie7.0
[1] => 915
)
and so on...
When i try to replace for example msie6.0and msie7.0 with InternetExplorer and add it occurence :
preg_match("/#^msie(.*)$#i/is", $results, $matches);
$test = $matches[0] ;
print_array($test);
$results["#^startText(.*)$#i"] = $results['InternetExplorer'];
print_array($results);
unset($results["/#^msie(.*)$#i/is]);
it not match the perfect as i want. any solution for that ?
in order to have :
Array
(
[1] => Array
(
[0] => InternetExplorer
[1] => 922
)
[2] => Array
(
[0] => safari5.0.3
[1] => 5
)
[3] => Array
(
[0] => chrome18.0.1025.308
[1] => 1
)
[4] => Array
(
[0] => firefox20.0
[1] => 4
)
After your clarification in comment. I think you don't need regex, strpos is enough too that job.
$rows["InternetExplorer"] = 0;
foreach($rows as $key => $value){
if(strpos($key,"msie") !== false){
$rows["InternetExplorer"] += $value;
unset($rows[$key]);
}
}
DEMO.