I have a page that has 4 widgets each getting data from the database. Each widgets gets data in different formats since i have a table widget, a statistics widget and a widget with just one value.
Supposing i have the results of queries returning data say in this format
$arr1 = array(1, 3, 5);
$arr2 = array(2, 4, 6);
$test1 = array(
'11' => '11',
'22' => '22',
'33' => '33',
'44' => '44'
);
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
how can i pass the arrays as json encoded as one but retains their array structure on the client side for easier and faster creating on widget views?.
I have looked at array_merge and according to the docs, that's not what i am looking for.
how can i pass the arrays as json encoded as one but retains their array structure on the client side for easier and faster creating on widget views?.
You might arrange each array to be json encoded together like so:
$bundle = json_encode([$arr1, $arr2, $test1, $array1, $array2]);
Result:
[[1,3,5],[2,4,6],{"11":"11","22":"22","33":"33","44":"44"},{"0":"zero_a","2":"two_a","3":"three_a"},{"1":"one_b","3":"three_b","4":"four_b"}]
If I understood your issue correctly, you can assign your arrays to a multidimensional array.
Based in your example:
$dataArray = [];
$arr1 = array(1, 3, 5);
$arr2 = array(2, 4, 6);
$dataArray['data_type_one'][] = $arr1;
$dataArray['data_type_one'][] = $arr2;
$test1 = array(
'11' => '11',
'22' => '22',
'33' => '33',
'44' => '44'
);
$dataArray['data_type_two'] = $test;
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$dataArray['data_type_three'][] = $array1;
$dataArray['data_type_three'][] = $array2;
You will end up with this array structure:
Array
(
[data_type_one] => Array
(
[0] => Array
(
[0] => 1
[1] => 3
[2] => 5
)
[1] => Array
(
[0] => 2
[1] => 4
[2] => 6
)
)
[data_type_two] => Array
(
[11] => 11
[22] => 22
[33] => 33
[44] => 44
)
[data_type_three] => Array
(
[0] => Array
(
[0] => zero_a
[2] => two_a
[3] => three_a
)
[1] => Array
(
[1] => one_b
[3] => three_b
[4] => four_b
)
)
)
Then, in your views, you can do whatever you need:
foreach ($arrayData['data_type_one'] as $someData) {
echo $someData[0] . ' ';
//will print 1 2
}
echo $dataArray['data_type_two']['11'];
//will print 11
Related
I would like to combine 2 arrays into 1 in PHP or laravel. I've searched this site for similar questions but can't seem to find an answer.
Can someone help me with this?
**array 1 -- $insertData **
Array
(
[0] => Array
(
[prid] => 4
[vendor_id] => 1
)
[1] => Array
(
[prid] => 5
[vendor_id] => 2
)
)
**Array - 2 $requestData **
Array
(
[vendor_id] => Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 2
)
[item] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
[qty] => Array
(
[0] => 12
[1] => 13
[2] => 14
[3] => 15
)
)
**Required Output ---- how can I do this array1 and array2 combine into a single array **
Array
(
[0] => Array
(
[prid] => 4
[vendor_id] => 1
[item] => 2
[qty] => 12
)
[1] => Array
(
[prid] => 4
[vendor_id] => 1
[item] => 3
[qty] => 13
)
[2] => Array
(
[prid] => 5
[vendor_id] => 2
[item] => 4
[qty] => 14
)
[3] => Array
(
[prid] => 5
[vendor_id] => 2
[item] => 5
[qty] => 15
)
)
My controller
public function prtmulti(Request $req)
{
$maxPrId = newpr::max('prid');
// print_r($maxPrId);
echo "<pre>";
$requestData = $req->all();
if (array_key_exists("vendor_name", $requestData)) {
$insertData = [];
$uniqueData = array_unique($requestData["vendor_name"]);
foreach ($uniqueData as $key => $value) {
$maxId = $maxPrId+1;
$insertData[] = ['prid' => $maxId, 'vendor_id' => $value];
$maxPrId = $maxPrId+1;
}
}
print_r($insertData);
print_r($requestData);
}
you can achieve this using the array_combine function in php, for example:
<?php
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_combine($fname,$age);
print_r($c);
?>
I'm pretty sure that Laravel doesn't offer anything out of the box to execute your desired merging technique (and I don't see why it would bother).
Assuming that the vendor_id values in the first array are unique, you will get best performance by creating a lookup array. array_column() can be used to declare an array with vendor_id values as keys and prid values as values.
Because your $requestData has rows with the number of columns desired in the output, loop over the $requestData['vendor_id'] data and manually generate the desired rows of data in the result array.
Code: (Demo)
$insertData = [
['prid' => 4, 'vendor_id' => 1],
['prid' => 5, 'vendor_id' => 2],
];
$requestData = [
'vendor_id' => [1, 1, 2, 2],
'item' => [2, 3, 4, 5],
'qty' => [12, 13, 14, 15]
];
$insertLookup = array_column($insertData, 'prid', 'vendor_id');
$result = [];
foreach ($requestData['vendor_id'] as $index => $vendorId) {
$result[] = [
'prid' => $insertLookup[$vendorId],
'vendor_id' => $vendorId,
'item' => $requestData['item'][$index],
'qty' => $requestData['qty'][$index],
];
}
var_export($result);
Output:
array (
0 =>
array (
'prid' => 4,
'vendor_id' => 1,
'item' => 2,
'qty' => 12,
),
1 =>
array (
'prid' => 4,
'vendor_id' => 1,
'item' => 3,
'qty' => 13,
),
2 =>
array (
'prid' => 5,
'vendor_id' => 2,
'item' => 4,
'qty' => 14,
),
3 =>
array (
'prid' => 5,
'vendor_id' => 2,
'item' => 5,
'qty' => 15,
),
)
You can use the array_merge() function to merge arrays.
array_merge
$merged_array = array_merge($insertData, $requestData);
I have two arrays:
$array1 = array(299945 => [13654 => [9917 => [0 => '0', 9 => '9', 33 => '33']]]);
$array2 = array(13654 => [9940 => [0 => '0']]);
Each of these are created from different DBQueries which create these results.
I know want to take the '9940' key from $array2 and push it into $array1 so that it will be another element in the 13654 array. Thus the final result would be:
$array1 = array(299945 =>[13654 => [9917 => [0 => '0', 9 => '9', 33 => '33'], 9940 => [0 => '0']]])
How can I do this?
There's a few ways of doing this, here's one that uses array_replace_recursive():
<?php
header('Content-type: text/plain');
$array1 = array(299945 => [13654 => [9917 => [0 => '0', 9 => '9', 33 => '33']]]);
$array2 = array(13654 => [9940 => [0 => '0']]);
$array3 = array_replace_recursive($array1, [key($array1) => $array2]);
print_r($array3);
Output:
Array
(
[299945] => Array
(
[13654] => Array
(
[9917] => Array
(
[0] => 0
[9] => 9
[33] => 33
)
[9940] => Array
(
[0] => 0
)
)
)
)
If you just want a union of two arrays, there is not much to it:
$array1 += $array2
You should probably think about more complex situation with duplicate keys and issues like that, so I usually find array_merge to be the better tool for adding two arrays together.
My problem statement is like follows:
Suppose I have 2 two dimensional array. The arrays are:
$array1 = Array
(
[8] => Array
(
[branch_code] => DG-52484
[total_desg] => 11
)
);
$array2 = Array
(
[8] => Array
(
[total_dak] => 0
[total_dak_decision] => 0
)
);
After combining the two array my required out put will be:
Array
(
[8] => Array
(
[branch_code] => DG-52484
[total_desg] => 11
[total_dak] => 0
[total_dak_decision] => 0
)
);
Is there any php function for this type of task. Please note that i am not interested to use foreach or while in my situation.
Thanks in advance.
It will work with array_replace_recursive:
$array1 = Array(
8 => Array(
'branch_code' => 'DG-52484',
'total_desg' => '11',
)
);
$array2 = Array
(
8 => Array(
'total_dak' => 0,
'total_dak_decision' => 0,
)
);
var_dump(array_replace_recursive($array1, $array2));
Output
array (size=1)
8 =>
array (size=4)
'branch_code' => string 'DG-52484' (length=8)
'total_desg' => string '11' (length=2)
'total_dak' => int 0
'total_dak_decision' => int 0
try to use
$array = array(
8 => array_merge($array1[8],$array2[8]);
);
You can use array_merge
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
Output
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
For more info http://php.net/manual/tr/function.array-merge.php
I have two arrays:
$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('d' => 4, 'e' => 5, 'f' => 6, 'a' => 'new value', '123' => 456);
I want to merge them and keep the keys and the order and not re-index!!
How to get like this?
Array
(
[a] => new value
[b] => 2
[c] => 3
[d] => 4
[e] => 5
[f] => 6
[123] => 456
)
I try to array_merge() but it will not be preserved the keys:
print_r(array_merge($array1, $array2));
Array
(
[a] => 'new value'
[b] => 2
[c] => 3
[d] => 4
[e] => 5
[f] => 6
[0] => 456
)
I try to the union operator but it will not overwriting that element:
print_r($array1 + $array2);
Array
(
[a] => 1 <-- not overwriting
[b] => 2
[c] => 3
[d] => 4
[e] => 5
[f] => 6
[123] => 456
)
I try to swapped place but the order is wrong, not my need:
print_r($array2 + $array1);
Array
(
[d] => 4
[e] => 5
[f] => 6
[a] => new value
[123] => 456
[b] => 2
[c] => 3
)
I dont want to use a loop, is there a way for high performance?
You're looking for array_replace():
$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('d' => 4, 'e' => 5, 'f' => 6, 'a' => 'new value', '123' => 456);
print_r(array_replace($array1, $array2));
Available since PHP 5.3.
Update
You can also use the union array operator; it works for older versions and might actually be faster too:
print_r($array2 + $array1);
Let suppose we have 3 arrays as below.
$a = array(0=>['label'=>'Monday','is_open'=>1],1=>['label'=>'Tuesday','is_open'=>0]);
$b = array(0=>['open_time'=>'10:00'],1=>['open_time'=>'12:00']);
$c = array(0=>['close_time'=>'18:00'],1=>['close_time'=>'22:00']);
Now, if you want to merge all these array and want a final array that have all array's data under key 0 in 0 and 1 in 1 key as so on.
Then you need to use array_replace_recursive PHP function, as below.
$final_arr = array_replace_recursive($a, $b , $c);
The result of this will be as below.
Array
(
[0] => Array
(
[label] => Monday
[is_open] => 1
[open_time] => 10:00
[close_time] => 18:00
)
[1] => Array
(
[label] => Tuesday
[is_open] => 0
[open_time] => 12:00
[close_time] => 22:00
)
)
Hope the solution above, will best fit your requirement!!
#Jack uncovered the native function that would do this but since it is only available in php 5.3 and above this should work to emulate this functionality on pre 5.3 installs
if(!function_exists("array_replace")){
function array_replace(){
$args = func_get_args();
$ret = array_shift($args);
foreach($args as $arg){
foreach($arg as $k=>$v){
$ret[(string)$k] = $v;
}
}
return $ret;
}
}
array_replace_recursive() or array_replace() is the function you are looking for
$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('d' => 4, 'e' => 5, 'f' => 6, 'a' => 'new value', '123' => 456);
var_dump(array_replace_recursive($array1, $array2));
Demo
I think this might help if i understand properly:
foreach ($i = 0, $num = count($array2); $i < $num; $i++)
{
$array = array_merge($array1, $arrar2[$i]);
}
I have two arrays, I want to merge these two arrays into single array. Please view the detail below:
First Array:
Array
(
[0] => Array
(
[a] => 1
[b] => 2
[c] => 3
)
[1] => Array
(
[a] => 3
[b] => 2
[c] => 1
)
)
Second Array:
Array
(
[0] => Array
(
[d] => 4
[e] => 5
[f] => 6
)
[1] => Array
(
[d] => 6
[e] => 5
[f] => 4
)
)
I want this result. Does somebody know how to do this?
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 3
[1] => 2
[2] => 1
)
[2] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[3] => Array
(
[0] => 6
[1] => 5
[2] => 4
)
)
Hope you have understand the question.
Thank you in advance.
Try array_merge:
$result = array_merge($array1, $array2);
FIXED (again)
function array_merge_to_indexed () {
$result = array();
foreach (func_get_args() as $arg) {
foreach ($arg as $innerArr) {
$result[] = array_values($innerArr);
}
}
return $result;
}
Accepts an unlimited number of input arrays, merges all sub arrays into one container as indexed arrays, and returns the result.
EDIT 03/2014: Improved readability and efficiency
more simple and modern way is:
$merged = $array1 + ['apple' => 10, 'orange' => 20] + ['cherry' => 12, 'grape' => 32];
new array syntax from php 5.4
If you want to return the exact result you specify in your question then something like this will work
function array_merge_no_keys() {
$result = array();
$arrays = func_get_args();
foreach( $arrays as $array ) {
if( is_array( $array ) ) {
foreach( $array as $subArray ) {
$result[] = array_values( $subArray );
}
}
}
return $result;
}
As a purely native function solution, merge the arrays, then reindex each subarray.
Code: (Demo)
$a = [
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 3, 'b' => 2, 'c' => 1],
];
$b = [
['d' => 4, 'e' => 5, 'f' => 6],
['d' => 6, 'e' => 5, 'f' => 4],
];
var_export(
array_map('array_values' array_merge($a, $b))
);
Output:
array (
0 =>
array (
0 => 1,
1 => 2,
2 => 3,
),
1 =>
array (
0 => 3,
1 => 2,
2 => 1,
),
2 =>
array (
0 => 4,
1 => 5,
2 => 6,
),
3 =>
array (
0 => 6,
1 => 5,
2 => 4,
),
)