Code does not stop running.i need to compare the two arrays in order to match each string. two arrays with different sizes.
First array:
Array (
[0] => '+2+x=1'
[1] => '+x+2=1'
[2] => 'x+2=1'
[3] => '-1+2=7'
[4] => '+2-1=7'
[5] => '+x+27=3+2'
[6] => 'x+27=3+2'
[7] => 'x=3'
[8] => '+x=3'
)
Second array:
Array (
[0] => '+x+2=1'
[1] => '-1+2=7'
[2] => '+x+27=3+2'
[3] => '+x=3'
)
my current code: (first array = $step_1, second array = $arr_result)
$count1 = 0;
for ($k=0; $k < count($arr_result); $i++) {
for ($l=0; $l < count($step_1); $l++) {
if (strcmp($arr_result[$k],$step_1[$l]) == 0) {
$count1++;
echo "$k "."$l ".strcmp($arr_result[$k],$step_1[$l])."<br>";
}
}
}
thanks in advance.
Use the function array_intersect, it will return an array with the matching values of both arrays :
$array1 = ['foo', 'bar', 'abc'];
$array2 = ['foo', 123, 456, 789, 4654, 'abcdef'];
$matching_values = array_intersect($array1, $array2);
$matching_values will result in
Array
(
[0] => foo
)
Edit: Notice that in your FOR you use $k but you increment $i... That's why your code doesn't stop;
for ($k=0; $k < count($arr_result); $i++)
Related
I have two dynamic arrays of integer, one thing that I wanna do is swap value inside my array-based on input.
For example my two arrays:
$myArray_a =
Array
(
[0] => 21306000
[1] => 50627412
[2] => 2560227681
[3] => 2796924085
[4] => 0
[5] => 0
)
$myArray_b =
Array
(
[0] => 505909732
[1] => 400831144
[2] => 2693575413
[3] => 3072271817
[4] => 5277000763
[5] => 4944000763
)
And my expected output was when the input = 3, array B index number 4 and 5 swap to array A in the same index.
$output =
Array
(
[0] => 21306000
[1] => 50627412
[2] => 2560227681
[3] => 2796924085
[4] => 5277000763
[5] => 4944000763
)
I want to switch, is there an easy way to do this? Or will it require a loop + creating a new array?
Provided your are using an numeric index, you could leverage array_slice
This will create an array with the first four entries, then append the second array skipping existing keys.
$count = 4; // which is 3 + 1
$a = [21306000,50627412,2560227681,2796924085,0,0];
$b = [505909732,400831144,2693575413,3072271817,5277000763,4944000763];
$output = array_slice( $a, 0, $count ) + $b;
//Array
//(
// [0] => 21306000
// [1] => 50627412
// [2] => 2560227681
// [3] => 2796924085
// [4] => 5277000763
// [5] => 4944000763
//)
You can do it with,
$index = 3;
$result = $B;
for($i = 0; $i<= $index; $i++){
$result[$i] = $A[$i];
}
You can use foreach
foreach($myArray_a as $k => &$v){
empty($v) && isset($myArray_b[$k]) ? ($v = $myArray_b[$k]) : '';
}
DEMO :- https://3v4l.org/nRj68
<?php
$a = [2,3,4,5,0,0];
$b = [20,30,40,50,60,70];
$counter = 0;
$out = array_map(function($m, $n ) use (&$counter)
{
return $counter++>3 ? $n : $m;
}, $a, $b);
var_export($out);
Output:
array (
0 => 2,
1 => 3,
2 => 4,
3 => 5,
4 => 60,
5 => 70,
)
I am trying to split array data into multiple arrays based on change in data value at known position (column).
$input = array(
array(1,2,3),
array(4,5,3),
array(7,8,4),
array(9,10,4),
array(11,12,4)
);
Here column 3 changes values from 3 to 4
and expected result is to have 2 arrays
$out1 = array(array(1,2,3),array(4,5,3));
$out2 = array(array(7,8,4), array(9,10,4), array(11,12,4));
since number of rows are variable, cannot use array_chunk
since column 3 values are variable, cannot use array_filter
number of output arrays are also variable.
trying splice but failing...
You can use array_reduce to make new array, where index will be equal to last numbers in items
$new = array_reduce($input, function($c, $x) { $c[$x[2]][] = $x; return $c; }, [] );
$out1 = $new[3];
$out2 = $new[4];
demo
But if array is not sorted and you want to split at points of changing that number, the code can be
$i = -1;
$last = null;
$new = [];
foreach($input as $x) {
if ($x[2] != $last) {
$i++;
$last = $x[2];
}
$new[$i][] = $x;
}
demo
You can use split index with index of array,
$out1 = $out2 = [];
$splitIndex = 2;
foreach($input as $k => $v){
if($k < $splitIndex){
$out1[] = $v;
}else{
$out2[] = $v;
}
}
print_r($out1);
print_r($out2);
Working demo
Output:-
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 3
)
)
Array
(
[0] => Array
(
[0] => 7
[1] => 8
[2] => 4
)
[1] => Array
(
[0] => 9
[1] => 10
[2] => 4
)
[2] => Array
(
[0] => 11
[1] => 12
[2] => 4
)
)
I have one problem with randomness.
I have example array.
$example=array(
'F1' => array('test','test1','test2','test5'),
'F2' => array('test3', 'test4'),
'F3' => array('one','twoo','threee','x'),
'F5' => array('wow')
)
I want to choose random keys from array to other array with specified size. In this second array I want values from other groups.
For example i got
$amounts = array(4,3,1,2,1);
I want to choose randomly specified ammount of variables ($amount) from $example, but of course - always from other groups.
Example result:
$result=(
array(4) => ('test','test4','x','wow'),
array(3) => ('test2','test3','three'),
array(1) => ('test1')
array(2) => ('test5','one')
array(1) => ('twoo')
What I tried so far?
foreach($amounts as $key=>$amount){
$random_key[$key]=array_rand($example,$amount);
foreach($result[$key] as $key2=>$end){
$todelete=array_rand($example[$end]);
$result[$key][$key2]=$example[$amount][$todelete]
}
}
I don't know now, what to fix or to do next.
Thanks for help!
$example = array(
'F1' => array('test', 'test1', 'test2', 'test5'),
'F2' => array('test3', 'test4'),
'F3' => array('one', 'twoo', 'threee', 'x'),
'F5' => array('wow')
);
$amounts = array(4, 3, 1, 2, 1);
$result = array();
$example = array_values($example);
//randomize the array
shuffle($example);
foreach ($example as $group) {
shuffle($group);
}
//sort the example array by child length
usort($example, function ($a, $b) {
return count($b) - count($a);
});
foreach ($amounts as $amount) {
$tmpResult = array();
for ($i = 0; $i < $amount; $i++) {
if(empty($example[$i])){
throw new \InvalidArgumentException('The total number of values in the array exceed the amount inputed');
}
$tmpResult[] = array_pop($example[$i]);
}
$result[] = $tmpResult;
//sort the example array again by child length
usort($example, function ($a, $b) {
return count($b) - count($a);
});
}
print_r($result);
test result:
Array
(
[0] => Array
(
[0] => test5
[1] => x
[2] => test4
[3] => wow
)
[1] => Array
(
[0] => test2
[1] => threee
[2] => test3
)
[2] => Array
(
[0] => test1
)
[3] => Array
(
[0] => twoo
[1] => test
)
[4] => Array
(
[0] => one
)
)
Why $val is Array(1), but not the numeric value? I expected that $selected as $k => $val should return each line from the array $selected. Thus, $k must be a numeric key (it is) and $val must be corresponding numeric value (but it's an array instead of simple integer).
So, how do I correctly save sorted keys and values in array $ind and $ranks?
<?php
$selected = array();
for ($i=0; $i<5; $i++) {
$selected[] = array($i => rand(0,5));
}
arsort($selected);
$ind = array();
$rank = array();
foreach($selected as $k => $val) {
$ind[] = $k;
$rank[] = $val;
}
?>
UPDATE:
For incstance, this code..
for ($i=0; $i<5; $i++) {
$selected[$i] = rand(0,5);
}
provided the array:
[0] => 5, [1] => 3, [2] => 2, [3] => 5, [4] => 3
Once I sorted it, initial keys are deleted, right? How can I find initial keys [0]-[4] of randomly generated values after sorting the array?
I think your likely solution is to change
$selected[] = array($i => rand(0,5));
to
$selected[] = rand(0,5);
Doing so will yield $ind and $rank like this:
Array
(
[0] => 0
[1] => 3
[2] => 2
[3] => 4
[4] => 1
)
Array
(
[0] => 4
[1] => 3
[2] => 1
[3] => 0
[4] => 0
)
The best way to do what you want, is to just use the resultant array, for example:
$selected
Array
(
[1] => 5
[2] => 5
[4] => 4
[0] => 2
[3] => 1
)
I think this is what you need
for ($i=0; $i<5; $i++) {
$selected[$i] = rand(0,5);
}
Your array structure will look like this:
array(
0 => array(0 => 1),
1 => array(1 => 4),
...
)
because you're assigning an array here:
$selected[] = array($i => rand(0,5));
You just want this instead:
$selected[] = rand(0,5);
Hi I am not pretty much sure what you are trying to do but following code is creating an array of arrays.
$selected = array();
for ($i=0; $i<5; $i++) {
$selected[] = array($i => rand(0,5));
}
So $val will be an array. You can try following code:
$selected = array();
for ($i=0; $i<5; $i++) {
$selected[] = rand(0,5);
}
Thanks
I need to combine 2 arrays under the same key by appending Array2 to Array1:
Array1
(
[0] => value1
[1] => value2
[2] => value3
[3] => value4
[4] => value5
[5] => value6
[6] => value7
)
Array2
(
[0] => add1
[1] => add2
[2] => add3
[3] => add4
[4] => add5
[5] => add6
[6] => add7
)
so that Array3 looks like this:
Array1
(
[0] => value1add1
[1] => value2add2
[2] => value3add3
[3] => value4add4
[4] => value5add5
[5] => value6add6
[6] => value7add7
)
I searched through php.net but I wasn't able to find anything. Any help would be much appreciated! Thank you in advance for your input.
array_mapdocs makes looping superfluous in this case:
$arr1 = array('value1','value2','value3');
$arr2 = array('add1','add2','add3');
$merged = array_map(function($x, $y) { return $x . $y; }, $arr1, $arr2);
If you don't have PHP5.3+ (or you don't like lambda) you'll need to define the closure in a separate function and reference that function's name instead inside your array_map call.
Just do a loop:
if (count($arr1) == count($arr2))
{
$arr3 = array();
foreach ($arr1 as $key => $val)
$arr3 = $val . $arr2[$key];
}
else
{
echo "Arrays should be of same size!";
$arr3 = array();
for ($i = 0, $i < min(count($arr1),count($arr2)); $i++)
$arr3 = $arr1[$i] . $arr2[$i];
}
(Exact implementation should depend on your input values)
Soemthing like this:
foreach($array1 as $k=> $value){
$array3[$k] = $value . $array2[$k];
}
print_r($array3);
Mind you, it will only take as many items as $array1 contains. If $array2 is larger, other values will be ignored.