Php three array merge combine - php

$arr1 = array(1,2,3);
$arr2 = array("a","b","c");
$arr3 =array("1a","2b","3c");
How can I do the following ?
print
$one = 1,a,1a
$two = 2,b,2b
$three = 3,c,3c

Use array_map() function to map through all arrays at the same time. Like this :
$array_merged = array_map(function($v1,$v2,$v3) {
return $v1 . ','. $v2 . ',' . $v3;
}, $arr1, $arr2, $arr3);
/*
Array (
[0] => "1,a,1a",
[1] => "2,b,2b",
[2] => "3,c,3c",
)
*/

You can try this:
$arr1 = array(1,2,3);
$arr2 = array("a","b","c");
$arr3 = array("1a","2b","3c");
$i = 0;
foreach ($arr1 as $key => $value) {
$newArr[] = $value.",".$arr2[$i].",".$arr3[$i];
$i++;
}
echo implode("<br/>", $newArr);
Result:
1,a,1a
2,b,2b
3,c,3c
You can also perform this by using for loop.

Try this:
$arr1 = [1,2,3];
$arr2 = ["a","b","c"];
$arr3 = ["1a","2b","3c"];
$matrix = [$arr1, $arr2, $arr3];
var_dump(transpose($matrix));
function transpose($matrix) {
return array_reduce($matrix, function($carry, $item) {
array_walk($item, function ($value, $key) use (&$carry) {
$carry[$key][] = $value;
});
return $carry;
});
}

Related

PHP - UNSET values in array not containing prefix [duplicate]

This question already has answers here:
Native function to filter array by prefix
(6 answers)
Closed 3 months ago.
I have an array like:
$array = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');
I want to UNSET the values in the array that are not prefixed with OPTM.
How should that be done?
PHP 5.5
A short approach to reach the same value
$array = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');
$filtred = array_filter($array, function($item){
return (strncmp($item, 'OPTM', 4)==0);
});
var_dump($filtred);
to make your code more dynamic and optimal you can do like this
$array = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');
$prefix = 'OPTM';
$array = array_filter($array, function($item) use($prefix){
return (strncmp($item, $prefix, strlen($prefix))==0);
});
var_dump($array);
// Don't need this if using PHP 8
if (!function_exists('str_starts_with')) {
function str_starts_with($haystack, $needle) {
return (string)$needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0;
}
}
$array = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');
for ($i = 0; $i < count($array); ++$i) {
if (!str_starts_with($array[$i], 'OPTM'))
unset($array[$i]);
}
// Optional to re-index array
$array = array_values($array);
You can use foreach/str_contains/unset like:
$array = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');
foreach($array as $key => $value){
if(!str_contains($value,'OPTM')){
unset($array[$key]);
}
}
print_r($array);
/*
Array
(
[0] => OPTM3000
[2] => OPTM3002
[5] => OPTM3004
)
*/
Reference:
foreach
str_contains
unset
Use this
$array = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');
$new_array = array_filter($array, function($v,$k) {
return strpos($v,'OPTM') !==false;
}, ARRAY_FILTER_USE_BOTH);
try this
$array = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');
$newArray = array();
foreach($array as $value) {
if ( 'OPTM' == substr($value, 0, 4) ) {
$newArray[] = $value;
}
}
Use Unset or if you don like to use unset function one possible way will be to work with array_map and array_filter:
$array = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');
$arr = array_map(function($item) {
if ( str_contains($item,'OPTM')) {
return $item;
}
}, $array);
$arr = array_filter($arr);
print_r($arr);
You can iterate the elements in your array using a regex like:
foreach($array as $element){
$pattern = "OPTM/";
if(echo preg_match($pattern, $element) == 0){
$key = array_search($element, $array);
unset($array[$key]);
}
}
OK, this works for me.
$array = array('OPTM3000', 'All-Stud','OPTM3002','MUSC1001','PATH3000', 'OPTM3004');
$allowed_prefix = 'OPTM';
foreach ( $array as $key => $text )
{
if (strpos($text, $allowed_prefix) !== 0) {
unset($array[$key]);
}
}

in which array is the value I'm looking for?

I have two or more than two arrays in PHP. For example:
$array1 = [1,5,10,15,22,28];
$array2 = [1,8,12,16,25,30];
$array3 = [10,15,20,21,22];
I want to find which of these arrays contains the value 5, so that I can create the output "$array1 contains 5"
try this,
$array1 = [1,5,10,15,22,28];
$array2 = [1,8,12,16,25,30];
$array3 = [10,15,20,21,22];
$f = 5;
for($n=1;$n<=3;$n++){
if(in_array($f, ${"array" . $n})){
echo "\$array$n contains $f";
}
}
Edit different variable name
$zomer = [1,5,10,15,22,28];
$halil = [1,8,12,16,25,30];
$kaya= [10,15,20,21,22];
$f = 5;
$collection = compact("zomer","halil","kaya");
foreach ($collection as $key=>$val){
if(in_array($f, $collection[$key])){
echo "\$$key contains $f";
}
}
As said #KrijnToet, it could be done easy with help of in_array() function:
if(in_array($val, $array1)) echo $val.' comes from $array1'.PHP_EOL;
Demo
Something along these lines may help:
feed the function findVal() with the arrays and a needle value you're looking for and it will return the name of the array(s) holding it.
<?php
$array1 = [1, 5, 10, 15, 22, 28];
$array2 = [1, 8, 12, 16, 25, 30];
$array3 = [10, 15, 20, 21, 22];
findVal([$array1, $array2, $array3], 30);
function findVal($arrays = [], $needle)
{
foreach ($arrays as $key => $array) {
foreach ($array as $value) {
if ($value == $needle) {
foreach ($GLOBALS as $arrayName => $value) {
if ($value === $array) {
echo 'value ' . $needle . ' found in : ' . $arrayName;
}
}
}
}
}
}
$GLOBALS — References all variables available in global scope, so we can use it to retrieve the array name(s).
demo
<?php
$array1 = [1,5,10,15,22,28];
$array2 = [1,8,12,16,25,30];
$array3 = [10,15,20,21,22];
$compacted = compact('array1', 'array2', 'array3');
$needle = 21;
$found = array_filter($compacted, function($item) use ($needle) {
return in_array($needle, $item);
});
var_export(array_keys($found));
Output:
array (
0 => 'array3',
)
Try this, using this function we can search in any number of array
$array1 = [1,5,10,15,22,28];
$array2 = [1,8,12,16,25,30];
$array3 = [10,15,20,21,22];
$findval=5;
$arrayFinal = array('array1' =>$array1 ,'array2' =>$array2 ,'array3' =>$array3 );
function searchForId($find, $array) {
foreach ($array as $key=> $value) {
if(in_array($find, $value)){
echo "\$$key contains $find";
}
}
}
searchForId($findval,$arrayFinal); //Call function

How to find missing number from an array without using function in php?

I have two array i.e $arr1 and $arr2 where I want to find missing value of $arr1 which is not present in $arr2 without using function like array_diff(), count(), explode(), implode() etc. So, How can I do this? Please help me.
code:
<?php
$arr1 = array('2','3','4','5');
$arr2 = array('1','6','7','8');
$array = array_diff($arr1,$arr2);
print_r($arr2);
?>
First approach:-
$missingValuesArray = array();
foreach($arr1 as $arr){
if(!in_array($arr,$arr2)){
$missingValuesArray[] = $arr;
}
}
print_r($missingValuesArray);
Output:- https://3v4l.org/UBS9G
Second approach:-
$missingValuesArray = array();
foreach($arr1 as $arr){
$counter = 0;
foreach($arr2 as $ar){
if($arr != $ar){
$counter++;
}
}
if($counter == sizeof($arr2)){
$missingValuesArray[] = $arr;
}
}
print_r($missingValuesArray);
Output:- https://3v4l.org/Uu6Ob
Requirement can be achieved by :
$arr1 = array('2','3','4','5');
$arr2 = array('1','6','7','8');
$diff = array();
$diff = $arr1;
$arrayDiff = array();
foreach($arr1 AS $value) {
foreach($arr2 AS $val) {
if ($value == $val) {
$arrayDiff[] = $value;
continue;
}
}
}
foreach ($arrayDiff AS $k=>$v) {
if (($key = array_search($v, $diff)) !== false) {
unset($diff[$key]);
}
}
print_r($diff);

get values on same index in PHP

I have 2D array and want to get all values which are at same index say at index '1'. what is the best way to get that as a new array.
Example: we have array(array(1,2,3), array(5,6,7)), the result must be array(2, 6).
Thanks
A simple function would do the trick:
function foobar($array, $index) {
$result = array();
foreach($array as $subarray) {
if(isset($subarray[$index])) {
$result[] = $subarray[$index];
}
}
return $result;
}
Or you can just use array_map (requires PHP 5.3):
array_map(function($array) { return $array[1]; }, $input);
$sample = array(array(1,2,3),
array(4,5,6),
array(7,8,9)
);
$index = 1;
$result = array_map(function($value) use($index) { return $value[$index]; }, $sample);
var_dump($result);
$input = array(
array(1,2,3),
array(5,6,7)
);
$output = array();
foreach ( $input as $data ) {
$output[] = $data[1];
}
$myarray=array(array(1,2,3), array(5,6,7));
$index=1;
$result=array();
foreach($myarray as $a) $result[]=$a[$index];
print_r($result);

php 2 foreach result mixed

I have 2 array datas, ("a","b","c") and ("x","y","z"), how to mixed them and out put a result as (ax)|(ay)|(az)|(bx)|(by)|(bz)|(cx)|(cy)|(cz)? (each $array1 + $array2 combine into a new words).
$array1 = array("a","b","c");
foreach($array1 as $data1){
}
$array2 = array("x","y","z");
foreach($array2 as $data2){
}
//$output = '(ax)|(ay)|(az)|(bx)|(by)|(bz)|(cx)|(cy)|(cz)';
$array = array();
$array1 = array("a","b","c");
$array2 = array("x","y","z");
foreach($array1 as $data1){
foreach($array2 as $data2){
$array[] = '('.$data1.$data2.')';
}
}
echo implode('|', $array);
<?php
$array1 = array("a","b","c");
$array2 = array("x","y","z");
$array3 = array();
foreach($array1 as $data1){
foreach($array2 as $data2){
$array3[] = "($data1$data2)";
}
}
echo implode('|', $array3);
// (ax)|(ay)|(az)|(bx)|(by)|(bz)|(cx)|(cy)|(cz)
You'll want to use a nested loop:
$items = array();
foreach($array1 as $a)
{
foreach($array2 as $b)
{
$items[] = '(' . $a . $b . ')';
}
}
echo implode('|', $items); // (ax)|(ay)|(az)|(bx)|(by)|(bz)|(cx)|(cy)|(cz)

Categories