How to remap an array to save it to couples - php

I need to remap an array from
array(1,2,3,4,5,6);
to output in couples like this:
array(1,2)
array(3,4)
array(5,6)
which I can do with foreach, but is the an built in php function that does this for you?

Use array_chunk:
<?php
$in = array(1, 2, 3, 4, 5, 6);
$out = array_chunk($in, 2);

Related

Inserting multiple values from an array using array_unshift() method

array_unshift is using for inserting one or many values at the beginning of an array. Now I have an array-
$data = [1, 3, 4];
Another array is needed to insert at the beginning of the $data array.
$values = [5, 6];
Here I want to insert the values 5, 6 at the beginning of the $data array, and the resulting $data would be-
$data = [5, 6, 1, 3, 4];
Note: I know there is a way like array_unshift($data, ...$values); but this is working from php7.3.x AFAIK. I need to do it below php7.3.
Is there any way to do this without looping the $values array in the reverse order?
Function array_merge exists since PHP 4:
<?php
$data = [1, 3, 4];
$values = [5, 6];
$data = array_merge($values, $data);
print_r($data);
Live PHP sandbox
You should use array_merge instead of array_unshift.
$data = [1, 3, 4];
$values = [5, 6];
$result = array_merge($values, $data); // the sequence of the array inside array_merge will decide which array should be merged at the beginning.

PHP array_intersect() till the first match

I have 2 arrays to compare and find if there is at least a single value in common.
This works just fine:
$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(2, 3, 4, 5, 6);
if (array_intersect($arr1, $arr2)) {
// good, at least one match found
}
However, the question is performance. It doesn't make sense to continue looping thru the arrays after the first match was found. Is there a native PHP function or a useful snippet to achieve this?
Will a combination of foreach() and in_array() do the trick?
How about this?
foreach ($arr1 as $key => $val) {
if (in_array($val, $arr2)){
// do something, maybe return so you wouldn't need break
break;
}
}
Just compare the first value?
$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(2, 3, 4, 5, 6);
if (array_intersect($arr1, $arr2)[0]) {
// good, at least one match found
}

PHP Array within an Array

$row[]; // Declare array. PRETEND ITS AN ARRAY
$row2[]; // Declare another
$row3[]; // Declare one more
$rowarray[];
$rowarray[0] = $row
$rowarray[1] = $row2
$rowarray[2] = $row3 // Store array in an array
My Questions:
1. Is this valid or even useful?
2. If I do this, how do I access $row[0] $row[1] etc.
The concept is valid, but the syntax is not -- arrays are not explicitly declared like that in PHP. A correct way to initialize this would be something like:
$row1 = array(1, 2, 3);
$row2 = array(4, 5, 6);
$row3 = array(7, 8, 9);
$rowarray = array($row1, $row2, $row3);
Or, equivalently and more succinctly:
$rowarray = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
$rowarray[1][2]. Indexes are in order, so, given the example data I used, this would be 6 (element 2 of the array which is element 1 of $rowarray).
What you're looking for is a Multi-dimensional array.
For example:
$a1 = array(
array(1, 2, 3, 4),
array(1, 2, 3, 4)
);

Compare two arrays, is there common elements between the two arrays?

I need to test if one element of an array is in another array.
$array_one = array("gogo", "blabla", "toto");
$array_two = array("stackov", "renaul", "toto");
I would like to know if one element of array_one is in array_two ???
How to test that? Am trying in_array but it seems to have problems.
array_intersect()
$array1 = array("gogo", "blabla", "toto");
$array2 = array("stackov","renaul","toto");
$commonElements = array_intersect($array1,$array2);
var_dump($commonElements);
Try this one:
array_intersect($array_one, $array_two);
Mark's answer should be enough for your problem.
If you ever wish to find the intersect of more than 2 arrays, use this:
$arrays = array(
array(1, 2, 3),
array(2, 4, 6),
array(2, 8, 16)
);
$intersection = call_user_func_array('array_intersect', $arrays);

Array sorting/searching technique in PHP

I have an array let say $array = array(2, 1, 8, 3, 6, 0, 10, 10)and I want to get second largest value of that array.
Which sorting/searching technique will be best & how could I use it?
I'd just remove the duplicates from your array (using array_unique) and then use rsort (which uses Quicksort) with the SORT_NUMERIC flag to sort numerically from highest to lowest:
$array = array(2, 1, 8, 3, 6, 0, 10, 10);
$unique_array = array_unique($array);
rsort($unique_array, SORT_NUMERIC);
$second_highest = $unique_array[1]; // will be 8
The PHP manual has a comparison of sorting techniques.
Something like this?
$array = array(2, 1, 8, 3, 6, 0, 10, 10);
rsort($array);
echo $array[1]; // 10
this reverse sorts the array and then outputs the second element.
Edit: if you want the second highest unique value, just add an array_unique call:
$array = array(2, 1, 8, 3, 6, 0, 10, 10);
$array = array_unique($array);
rsort($array);
echo $array[1]; // 8
you could use rsort, but it will do a lot of additional operations which are not needed (quick sort has O(logn) complexity).
It might be a lot faster if you pass the array only in one pass like this (O(n)):
$largest=$array[0];
$secondLargest=null; //none present, by default
foreach($array as $value)
if($value>$largest)
{
$secondLargest=$largest;
$largest=$value;
}
it will be significantly faster on large arrays, but it is only good when you want the first, second, maybe third largest element. In all other cases it would be more reasonable to sort the array and take the N'th largest element dynamically.

Categories