Two arrays in one array PHP - 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.

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

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

I want replace index array. example $array[x][y] to $array[y][x]

Example. I have:
Array (
[0] => Array (
[comments_id] => 1
[comments_text] => blabla1
)
[1] => Array (
[comments_id] => 2
[comments_text] => blabla2
)
)
I want have:
Array (
[comments_id] => Array (
[0] => 1
[1] => 2
)
[comments_text] => Array (
[0] => blabla1
[1] => blabla2
)
In simplified wants to replace
$array[x][y] to $array[y][x]
I writing in php.
you can do it like this
// the final array which will hold your result array
// considering $results contains your previous array
$final_array = array();
foreach($results as $result) {
$final_array['comments_id'][] = $result['comments_id'];
$final_array['comments_text'][] = $result['comments_text'];
}

single indexed multiple arrays to one array

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
)

Creating a multidimensional array

I have 2 arrays, that I want to put into 1 multidimensional array
$array_result = array();
Array1 = a,b,c,d
Array2 = 1,2,3,4
The result that I want to get is
$array_result = [0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
) etc...
I can't work out how to do this. Then length of Array1 and Array2 varies as it is dynamic data.
Can someone point me in the right direction?
Try this
$arr1 = array(1,2,3,4);
$arr2 = array('a','b','c','d');
$arr3 = array();
for($i = 0;$i< count($arr1);$i++) {
$arr = array();
$arr[] = $arr2[$i];
$arr[] = $arr1[$i];
array_push($arr3,$arr);
}
Output
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
)
[2] => Array
(
[0] => c
[1] => 3
)
[3] => Array
(
[0] => d
[1] => 4
)
)
Codepad Demo
Use array_merge() function. It should do what you want to do.
$array_result=array_merge($array1, $array2, ...);

Categories