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)
Related
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
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);
$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;
});
}
I have array like this:
$array1 = array(Array('a','d'),
Array('c','a'),
Array('d','a'),
Array('a','b','c','d','e'),
);
$array2 = array(array('a','d'), array('a','b','c','d','e')) ;
$result = array();
Here's my code:
foreach ($array2 as $part) {
$key = implode(', ', $part);
if( ! array_key_exists ($key, $array1)) {
$result[$key] = 0;
}
$result[$key] = $result[$key] + 1;
}
foreach ($result as $key => $value) {
echo "$value of {$key}<br/>";
}
I want to count values $array2 based on $array1
I got this one:
1 of a,d
1 of a,b,c,d,e
But I want a result like this:
3 of a,d
1 of a,b,c,d,e
If anybody wonders why there's (3 of a,d), it count from array('a','d'), array('d','a') also counted as array('a','d') and array('a','b','c','d','e')
Try this. Here is a working demo https://eval.in/117810
<?
$array1 = array(array('a','d'),
array('c','a'),
array('d','a'),
array('a','b','c','d','e'),
);
$array2 = array(array('a','d'), array('a','b','c','d','e')) ;
$result = array();
foreach ($array2 as $key=>$part2) {
sort($part2);
if(!isset($result[$key]))$result[$key]=0;
foreach($array1 as $part1) {
$intersect = array_intersect($part1, $part2);
sort($intersect);
if ($intersect === $part2) {
$result[$key]++;
}
}
}
foreach($result as $k=>$v) {
echo $v . " of " . implode(',', $array2[$k]) . "<br/>";
}
?>
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);