Two arrays comibation under the same key - php

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.

Related

Combining Arrays while merging the values with the same key

I have two arrays with same amount of values. I need to combine them ( array1 value to key, array2 value as value) without losing the values of the second array due to duplicate key. when I use combine_array() as expected it just gets the last value of the second array with the same key.
Array
(
[0] => 1
[1] => 2
[2] => 2
[3] => 3
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Desired result
Array
(
[1] => 1
[2] => Array(
[0]=>2
[1]=>3
)
[3] => 2
)
This code will meet your request
$array1 = array("0"=>1,"1"=>2,"2"=>2,"3"=>3);
$array2 = array("0"=>1,"1"=>2,"2"=>3,"3"=>4);
$array = array();
foreach($array1 as $key => $value){
if($value != $array2[$key]){
$array[$key][] = $value;
$array[$key][] = $array2[$key];
}else{
$array[$key] = $value;
}
}
print_r($array);
The desired result is
Array
(
[0] => 1
[1] => 2
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 3
[1] => 4
)
)
I'm sure there are way better solutions than this, but it does the job for now. I would appreciate if someone can send a better written solution.
$combined = array();
$tempAr = array();
$firstMatch = array();
$count = 0;
foreach ($array1 as $index => $key) {
if (array_key_exists($key, $combined)) {
$tempAr[] = $array2[$index];
$count++;
} else {
$totalCount = $count;
}
if (!array_key_exists($key, $firstMatch)) {
$firstMatch[$key] = $array2[$index];
}
$output = array_slice($tempAr, $totalCount);
$combined[$key] = $output;
}
$combined = array_merge_recursive($firstMatch, $combined);

array comparison using php

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++)

Combine two different dimensional arrays PHP

I have two different dimensional arrays.
Array 1:
Array1
(
[0] => Array
(
[id] => 123
[price] => 5
[purchase_time] => 2014/4/10
)
[1] => Array
(
[id] => 123
[price] => 5
[purchase_time] => 2014/5/17
)
)
Array 2:
Array2
(
[0] => 5
[1] => 8
)
I want something like this:
Array
(
[0] => Array
(
[id] => 123
[price] => 5
[purchase_time] => 2014/4/10
[Qty] => 5
)
[1] => Array
(
[id] => 123
[price] => 5
[purchase_time] => 2014/5/17
[Qty] => 8
)
)
Basically the first array is the information I got from a SQL table. The second array contains the quantity for the products sold. I now want to combine these two array together and use the combined array to create a new table. Since these two arrays have different dimensions. I'm not sure how to do it. Here is my try:
$i = 0;
foreach($array1 as $row)
{
$newarray = array_merge($row,$array2[$i]);
$i++;
}
Might be a simpler way, but for fun without foreach:
array_walk($array1, function(&$v, $k, $a){ $v['Qty'] = $a[$k]; }, $array2);
The simplest way is:
$i = 0;
foreach($array1 as &$row) {
$row['Qty'] = $array2[$i++];
}
Or if keys of both arrays are the same (0,1,2...) and array have the same length:
foreach($array1 as $k => &$row) {
$row['Qty'] = $array2[$k];
}
If the $array1 and $array2 are mapped by the same indexes, so they have same length, you can try:
foreach($array2 as $index=>$quantity){
$array1[$index]['Qty'] = $quantity;
}
And it´s done!
If you want to keep the original $array1 untouched, you can make a copy before the foreach.
Create new array and store it there. You can access the value of $array2 because they have the same index of $array1 so you can use the $key of these two arrays.
$array3 = [];
foreach($array1 as $key => $val) {
$array3[] = [
'id' => $val['id'],
'price' => $val['price'],
'purchase_time' => $val['purchase_time'],
'Qty' => $array2[$key]
];
}
print_r($array3);

Check an array value exists another array key in PHP

I have two arrays with the following values,
First Array:
Array
(
[Strongly Agree] => 100
)
Second Array:
Array
(
[0] => Strongly Agree
[1] => Agree
[2] => Neither Agree or Disagree
[3] => Strongly Disagree
)
I need the output should like this,
Array (
[0] => 100
[1] => 0
[2] => 0
[3] => 0
)
Try like
foreach($array2 as $key => $value) {
$temp = array_key_exists($value, $array2) ? $array1[$value] : 0;
$newArr[$key] = $temp;
}
array key exists won't trigger notices
$sample = array('Strongly Agree' => 100);
$alternatives = array( 'Strongly Agree', 'Agree', 'Neither Agree or Disagree', 'Strongly Disagree');
$output=array();
foreach($alternatives as $alternative) {
$output[$alternative] = array_key_exists($alternative, $sample)? $sample[$alternative]:0;
}
print_r($output);
Try
$arr2 = array_merge(array_fill_keys($arr2, 0), $arr1);
See demo here

Create an array from 2 other arrays

$arrayA = Array (
[0] => 1,
[1] => 2,
[2] => 4
)
$arrayB = Array (
[1] => Dog,
[2] => Cat,
[3] => Cow,
[4] => Duck
)
How do I create an $arrayC that takes the value from the above 2 arrays:
$arrayC = Array (
[1] => Dog,
[2] => Cat,
[4] => Duck
)
Theoretically, it's something like this:
$arrayC = Array (
[$arrayA[0]] => $arrayB[$arrayA[0]],
[$arrayA[1]] => $arrayB[$arrayA[1]],
[$arrayA[2]] => $arrayB[$arrayA[2]]
)
Thanks.
You can do this in elegant way without foreach (Demo):
$arrayC = array_intersect_key($arrayB, array_flip($arrayA));
See array_intersect_key[Docs] and array_flip[Docs]
$arrayC = array();
foreach ($arrayA as $key) {
if (isset($arrayB[$key])) {
$arrayC[$key] = $arrayB[$key];
}
}
No need to write the foreach loop yourself:
//get only the keys that are in both
$arrayA = array_intersect_key(array_fill_keys($arrayA , true), $arrayB);
$arrayB = array_intersect_key($arrayB, $arrayA);
//combine the arrays
$arrayC = array_combine(array_keys($arrayA), $arrayB);
foreach($arrayA as $i => $key) {
$arrayC[$key] = $arrayB[$arrayA[$i]];
}
$arrayC will be:
Array ( [1] => Dog [2] => Cat [4] => Duck )
You can try to do something like this :-
foreach ($arrayA as $number)
{
if(isset($arrayB[$number])
{
$arrayC[$number] = $arrayB[$number];
}
}

Categories