Creating a multidimensional array - php

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, ...);

Related

php - set values as index in array

This is my array:
Array
(
[0] => Array
(
[0] => Q1
[1] => 100
)
[1] => Array
(
[0] => Q2
[1] => 200
)
[2] => Array
(
[0] => Q3
[1] => 300
)
)
I want to have an array like this:
Array
(
[Q1] => 100
[Q2] => 200
[Q3] => 300
)
So basically I want to split all arrays in one, and 0 key of all multi arrays will be key in the new array and 1 value in multi-array will be value in the new array. I tried with array_combine, but that does not work for me, any ideas?
There's a function for that:
$result = array_column($array, 1, 0);
Failing that just loop:
foreach($array as $v) { $result[$v[0]] = $v[1]; }
Use this straight-forward solution :
$arr = [
['Q1',100],
['Q2',200],
['Q3',300]
];
$res = array_combine(
array_column($arr, 0),
array_column($arr, 1)
);
print_r($res);

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

Two arrays to one chunked [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 5 months ago.
I have a two arrays and need to get one from them. array_merge, array_map etc. Doesn't give right result.
$array1 = [1,2,3,4,5];
$array2 = [a,b,c,d,e];
I need $array3 = [[1,a], [2,b], [3,c]]...
What is the best way to get that result?
using array_map(null, $arr1, $arr2) you can achieve the result.
http://php.net/manual/en/function.array-map.php
php > $q = array_map(null, $array1, $array2);
php > print_r($q);
Array
(
[0] => Array
(
[0] => 1
[1] => a
)
[1] => Array
(
[0] => 2
[1] => b
)
[2] => Array
(
[0] => 3
[1] => c
)
[3] => Array
(
[0] => 4
[1] => d
)
[4] => Array
(
[0] => 5
[1] => e
)
)
The for loop approach is the following:
<?php
$array1 = [1,2,3,4,5];
$array2 = ['a','b','c','d','e'];
$array3 = array();
if(count($array1) == count($array2))
{
for($i = 0; $i < count($array1); $i++)
{
$array3[] = [$array1[$i],$array2[$i]];
}
}
else
{
die("SIZE MISMATCH");
}
echo '<pre>';
print_r($array3);
And the output is:
Array
(
[0] => Array
(
[0] => 1
[1] => a
)
[1] => Array
(
[0] => 2
[1] => b
)
[2] => Array
(
[0] => 3
[1] => c
)
[3] => Array
(
[0] => 4
[1] => d
)
[4] => Array
(
[0] => 5
[1] => e
)
)

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.

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
)

Categories