single indexed multiple arrays to one array - php

I have a number of arrays:
Array ( [0] => A-I-only )
Array ( [0] => B-III-only )
Array ( [0] => C-I-and-II-only )
Array ( [0] => D-II-and-III-only )
Array ( [0] => E-I,-II,-III )
I want to put each array's first row in one array, like this:
Array( [0] => A-I-only [1] =>B-III-only [2] => C-I-and-II-only [3] => D-II-and-III-only [4] => E-I,-II,-III )
Is there a way to do it?

Use can use array merge function. Like:
$array = array_merge($array1,$array2,...);
Please note though that this would not work properly if your common indices were of string type (there would be a value overriding). Take a look here for more.

$array0 = array('A-I-only');
$array1 = array('B-III-only');
$array2 = array('C-I-and-II-only');
$array3 = array('D-II-and-III-only');
$array4 = array('E-I,-II,-III');
$result = array_merge($array0, $array1, $array2, $array3,
$array4);
print_r($result);
The Above code will give result as below
Array
(
[0] => A-I-only
[1] => B-III-only
[2] => C-I-and-II-only
[3] => D-II-and-III-only
[4] => E-I,-II,-III
)

Related

How to remove duplicate values from array - php

I am trying to remove duplicate and empty values from array with array_unique() function, but getting wrong output.
Data:
Array (
[0] => Array (
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
)
[1] => Array (
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
[5] => 101
)
[2] => Array (
[0] =>
[1] =>
[2] => 108
[3] =>
)
)
PHP:
$array = array_filter($userids);
$arrays = array_unique($array, SORT_REGULAR);
print_r($arrays);
nothing happens with SORT_REGULAR - output comes same as raw data, and without SORT_REGULAR this output is coming:
$array = array_filter($userids);
$arrays = array_unique($array);
print_r($arrays);
output:
Array (
[0] => Array
(
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
)
)
output I am looking for:
Array (
[0] => Array
(
[0] => 1
[1] => 108
[2] => 101
)
)
Those array functions only works on a single level. If you flatten the array (adding all elements in a single element array), it should be pretty straight forward.
Flatten the array
$array = array_merge(...$array);
Note: This method works fine for flattening indexed arrays like in your example, but not for associative arrays where any of the sub arrays contains the same keys.
Then filter out all empty
$array = array_filter($array);
and then remove all duplicates
$array = array_unique($array);
Or as a one-liner:
$array = array_unique(array_filter(array_merge(...$array)));
Demo: https://3v4l.org/pEJAJ

Find difference in arrays based on values php

I have 2 arrays -
Array
(
[0] => Array
(
[description] => 5390BF675E1464F32202B
[to_email] => test#test.com
)
[1] => Array
(
[description] => 5390BF675E1464F32202B
[to_email] => test3#test.com
)
[2] => Array
(
[description] => 5390BF675E1464F32202B
[to_email] => testagain#gmail.com
)
)
Array
(
[0] => Array
(
[to_email] => test#test.com
)
[1] => Array
(
[to_email] => test3#test.com
)
)
I want to get the values from Array 1 which are different from the second array.
I have tried using -
$result = array_diff_assoc($array1, $array2);
AND
$result = array_diff($array1, $array2);
But both gave error like -
Notice: Array to string conversion in
Outcome that I am expecting is
Array
(
[0] => Array
(
[description] => 5390BF675E1464F32202B
[to_email] => testagain#gmail.com
)
)
You can generate a list of the email addresses to exclude using array_column. We use the 3 parameter form to index that array by the email addresses as it makes it easier to filter with:
$exclude_ids = array_column($array2, 'to_email', 'to_email');
We can then use array_filter to filter $array1:
$output = array_filter($array1, function ($v) use ($exclude_ids) {
return !isset($exclude_ids[$v['to_email']]);
});
print_r($output);
Output:
Array
(
[2] => Array
(
[description] => 5390BF675E1464F32202B
[to_email] => testagain#gmail.com
)
)
Demo on 3v4l.org
Note if you want the output array re-indexed to 0, just use
$output = array_values($output);

Merge a simple array and nested array according to same keys

I have array 1 like this
Array
(
[0] => 1
[1] => 2
)
Second array would be
Array
(
[0] => Array
(
[FullName] => Bhupat Chippa
)
[1] => Array
(
[FullName] => Dvs Patel
)
)
I want to merge it the way values would be added to second array with same keys. Desired Output will look like this or some way around so that I can use the Array 1's value with Second Array Only:
Array
(
[0] => Array
(
[FullName] => Bhupat Chippa
[0] => 1
)
[1] => Array
(
[FullName] => Dvs Patel
[1] => 2
)
)
You can apply simple foreach() to do that
$final = [];
foreach($array2 as $key =>$arr2 ){
$final[$key]['FullName'] = $arr2['FullName'];
$final[$key][$key] = $array1[$key];
}
print_r($final);
Output:- https://eval.in/1010437
If both arrays are of the same length, you might use array_map passing the array_keys as the second parameter:
$array1 = ["1", "2"];
$array2 = [
["FullName" => "Bhupat Chippa"],
["FullName" => "Dvs Patel"]
];
$result = array_map(function($x, $y) use ($array1){
$x[$y] = $array1[$y];
return $x;
}, $array2, array_keys($array1));
print_r($result);
Demo
That will give you:
Array
(
[0] => Array
(
[FullName] => Bhupat Chippa
[0] => 1
)
[1] => Array
(
[FullName] => Dvs Patel
[1] => 2
)
)

Add common element to multi-dimensional array in php with out using loop

I have an array, which is given below:
$test = Array
(
[0] => Array
(
[0] => stud 1
)
[1] => Array
(
[0] => stud 2
)
[2] => Array
(
[0] => stud 3
)
);
I want to add a common element to above array with out using loop. For example, I want to add "test" to each element of array. After adding "test", array will look like:
$test = Array
(
[0] => Array
(
[0] => stud 1
[1] => 'test'
)
[1] => Array
(
[0] => stud 2
[1] => 'test'
)
[2] => Array
(
[0] => stud 3
[1] => 'test'
)
);
Is there any way to add common element array with out using any kind of loop(for, foreach etc...)?
You can use array_map(), check the live demo
array_map(function($v){$v[] = 'test'; return $v;}, $array);

Two arrays in one array PHP

I have one question: How can I push two arrays into one array, so scheme should look like:
array1 = array("element1", element2, "element3");
array2 = array("element4", element5, "element6");
[0] => FinalArray
(
[0] => array1[] =>
(
[0] = element1
[1] = element2
[2] = element3
)
[1] => array2[] =>
(
[0] = element4
[1] = element5
[2] = element6
)
)
I tried use array_push() or array_merge() but without success.
Thanks for hints.
Marek
First of all create the array
$superArray = array();
then add your arrays in the array
$superArray[] = $array1;
$superArray[] = $array2;
And you should be ok ;)
Hope this helped,
Try below method.
<?php
$array1 = array("element1", element2, "element3");
$array2 = array("element4", element5, "element6");
$finalArray = array($array1,$array2);
print_r($finalArray);
?>
Output:
Array
(
[0] => Array
(
[0] => element1
[1] => element2
[2] => element3
)
[1] => Array
(
[0] => element4
[1] => element5
[2] => element6
)
)
Check code and result in link: Answer
Try with
METHOD 1
$final = array("finalArray"=>array($array1,$array2));
OUTPUT
Array
(
[finalArray] => Array
(
[0] => Array
(
[0] => element1
[1] => element2
[2] => element3
)
[1] => Array
(
[0] => element4
[1] => element5
[2] => element6
)
)
)
METHOD 2
$final = array_merge(array($array1),array($array2));
OUTPUT
Array
(
[0] => Array
(
[0] => element1
[1] => element2
[2] => element3
)
[1] => Array
(
[0] => element4
[1] => element5
[2] => element6
)
)
Try $finalArray = [$array1, $array2];
What you want is called a multi-dimensional array and is explained in the PHP documentation. You create an array and then set the values of that array to be arrays as well. You can go as deep as you need, the only limit is the available RAM. To create a multidimensional array you set your key and then another array as the value, such as this example:
$finalArray = array(
0 => array (
0 => "element1",
1 => "element2",
2 => "element3"
),
1 => array (
0 => "element4",
1 => "element5",
2 => "element6"
),
);
Of course the keys are entirely optional in that example, I included them for clarity.
You can create the child arrays as separate variables first if you wish. For example:
$array1 = array (
0 => "element1",
1 => "element2",
2 => "element3"
);
$array2 = array (
0 => "element4",
1 => "element5",
2 => "element6"
);
$finalArray = array(
$array1,
$array2
);
I've done an eval.in example so you can see that they both output the same results.

Categories