array merge issue in php [duplicate] - php

This question already has answers here:
Combine arrays in PHP [duplicate]
(4 answers)
Closed 10 months ago.
I want to merge array in php. I have array like this:
Array ( [0] => 6 [1] => 3 [2] => 15 )
Array ( [0] => IMAGE [1] => TICKER [2] => FULL_SCREEN_VIDEO )
Array ( [0] => 434 [1] => 423 [2] => 123 )
And I want result like:
Array( [0] => 6 [1] => IMAGE [2] => 434)
Array( [0] => 3 [1] => TICKER [2] => 423)
Array( [0] => 15 [1] => FULL_SCREEN_IMAGE [2] => 123)
What will be the easiest solution for this kind of problem?
Thanks..

You want to "transpose" an array. Assuming you have these 3 arrays in an array, you can do this:
$array = array(
array(6, 3, 15),
array('IMAGE', 'TICKER', 'FILL_SCREEN_VIDEO'),
array(434, 423, 123)
);
array_unshift($array, null);
$array = call_user_func_array("array_map", $array);
If your arrays are actually 3 separate arrays, then you could just simply do this:
$array = array_map(null, $array1, $array2, $array3);
That's basically what call_user_func_array is doing.

Related

PHP array unique does not work like expected [duplicate]

This question already has answers here:
How to remove duplicate values from an array in PHP
(26 answers)
Closed 1 year ago.
I have a problem with array_unique() in PHP.
Here is my code:
$filterGroupsArray = ['', 'a', 'a', 'b'];
print_r($filterGroupsArray);
array_unique($filterGroupsArray);
print_r($filterGroupsArray);
The output is
Array ( [0] => [1] => a [2] => a [3] => b ) Array ( [0] => [1] => a [2] => a [3] => b )
but I am expecting
Array ( [0] => [1] => a [2] => a [3] => b ) Array ( [0] => [1] => a [2] => b )
What did I do wrong?
Thank you very much!
array_unique returns a new array with the duplicate values removed.
$uniqueArray = array_unique($filterGroupsArray);
print_r($uniqueArray);

Need common arrays from two multidimensional arrays

Hi I have below multidimensional arrays -
Array
(
[user_attempts] => 0
[2] => Array
(
[0] => 1
[1] => 4
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
)
[1] => Array
(
[0] => Earth
)
)
and my second array is like below
Array
(
[1] => Array
(
[0] => Earth
)
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
[1] => asdas
)
)
I need to intersect both multidimensional arrays - so the result would be
Array
(
[1] => Array
(
[0] => Earth
)
[3] => Array
(
[0] => 32
[1] => 23
)
)
Can anyone help me to sort this out.
What I have tried is using array_intersect() but it limits to single array not multidimensional i guess.
PHP comes with a ton of functions already built in, but sometimes you still have to implement things yourself. What you want to do can be easily done by using the existing functions.
The goal is to do the following steps:
Find the keys that exist in both arrays
Loop through the array using these keys
Take the items of both input arrays with each of these keys
Calculate the intersection of those two arrays
Put it into a result array
Here is one way to do this:
function array_intersect_2dim (array $a1, array $a2) {
$keys = array_intersect(array_keys($a1), array_keys($a2));
$return = array();
foreach ($keys as $key) {
$return[$key] = array_intersect($a1[$key], $a2[$key]);
if (sizeof($return[$key]) == 0) {
unset($return[$key]);
}
}
return $return;
}
It works only for two dimensions. If you need more, you have to build a recursive approach, which follows the exact same principle.
To make the easier to compare you can use serialize/unserialize on this one. And then use array_intersect(). Try this example: Sample Output
$array1 = array( 'user_attemps' => 0, 2 => array(1, 4), 3 => array(32, 23), 4 => array('asdsa'), 1 => array('Earth'),);
$array2 = array( 1 => array('Earth'), 2 => array(2, 3), 3 => array(32, 23), 4 => array('asdsa', 'asdas'),);
$result = array_map('unserialize',array_intersect(array_map('serialize', $array1), array_map('serialize', $array2)));
print_r($result);

how to change index of an array in php [duplicate]

This question already has answers here:
Built-in PHP function to reset the indexes of an array?
(4 answers)
Closed 8 years ago.
I want to change the index of an array after doing some operations
My actual output is
Array
(
[0] => Array
(
[0] => 4
[1] => 6
)
[2] => Array
(
[0] => 1
[1] => 7
)
[5] => Array
(
[0] => 1
[1] => 7
)
)
I want to be something like this
Array
(
[0] => Array
(
[0] => 4
[1] => 6
)
[1] => Array
(
[0] => 1
[1] => 7
)
[2] => Array
(
[0] => 1
[1] => 7
)
)
How to achieve this in php???? Thanks in advance
Extract only the values to a new array and the keys will be indexed from 0:
$array = array_values($array);
The array_values() function returns only values from array.
Thus, resetting all the array keys to numeric starting from 0, 1, 2, ...
You can do it using
$array = array_values($array);
http://php.net/manual/en/function.array-values.php
you can use the "array_values" Function for that.
Please refer this link http://www.php.net/manual/en/language.types.array.php

How to combine two arrays [duplicate]

This question already has answers here:
Merge two arrays as key value pairs in PHP
(3 answers)
Closed 8 years ago.
I have two arrays
Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 5 [4] => 6 [5] => 11 )
Array ( [1] => Zwembad [2] => Airconditioning [3] => Telefoon [4] =>
Internet [5] => Wi-Fi [6] => TV [11] => food )
Is there an array function to combine this array to form a new array.
Result Array should be:
Array ( [0] => Zwembad [1] => Airconditioning [2] => Internet [3] => Wi-Fi [4] => 6 [5] => TV )
That is, the values of First array have been replaced by the values corresponding to the index of second array.
Why not have options?
$new = array();
$i = 0;
$ak = array_values( $array2 );
foreach ( array_keys( $array1 ) as $k )
{
$new[$k] = $ak[$i];
$i++;
}
Try with array_values
$second_array = array_values($second_array);
You can use array_combine.
$combineArray=array_combine($array1, $array2);
<?php
$a1 = array("red","green");
$a2 =array("blue","yellow");
print_r(array_merge($a1,$a2));
?>

Refactor (transpose) array to unique keys [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 8 years ago.
I'm transposing some db results for statistic generation.
Original array:
Array
(
[0] => Array
(
[a] => apple
[b] => beer
[c] => chocolate
)
[1] => Array
(
[a] => aardvark
[b] => bear
[c] => chupacabra
)
)
Desired result:
Array
(
[a] => Array
(
[0] => apple
[1] => aardvark
)
[b] => Array
(
[0] => beer
[1] => bear
)
[c] => Array
(
[0] => chocolate
[1] => chupacabra
)
)
Sample code:
$stats[] = array(
'a' => 'apple',
'b' => 'beer',
'c' => 'chocolate'
);
$stats[] = array(
'a' => 'aardvark',
'b' => 'bear',
'c' => 'chupacabra'
);
foreach (array_keys($stats[0]) as $key){
$data[$key] = array_column($stats, $key);
}
The above code is working fine using array_keys and array_column (php 5.5).
Is there a more elegant way or php function to achieve the same result?
What is the common name for this kind of re-factoring?
EDIT:
As per comments below the correct term for this is "transposing"
I had thought there was an array_* function for that sort of thing, but I couldn't remember so I went to the PHP documentation Turns out that array_merge_recursive does just what you want.
array_merge_recursive($array1, $array2)
You have your arrays to combine in a single array, so you'll have to populate the function's arguments with the contents of the array.
call_user_func_array("array_merge_recursive", $stats)
That one line should do what you are looking for.

Categories