How to 1 output from 2 output [duplicate] - php

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 7 years ago.
How to 1 output from 2 output ?
<?php
[a] => Array
(
[0] => 72.89.122.00:50827
[1] => 62.173.145.00:17211
);
[b] => Array
(
[0] => 61.10.231.00:52151
[1] => 66.171.81.00:41787
);
?>
I need output:
[total] => Array
(
[0] => 72.89.122.00:50827
[1] => 62.173.145.00:17211
[2] => 61.10.231.00:52151
[3] => 66.171.81.00:41787
);

Take a look at array_merge(); http://php.net/manual/en/function.array-merge.php
$newArray = array_merge($a,$b);

Use the array_merge function described here.

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

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

Set one array value as parameter to another array [duplicate]

This question already has answers here:
Push elements from one array into rows of another array (one element per row)
(4 answers)
Closed 8 years ago.
Hi i have an array like this
Array
(
[0] => Array
(
[employeename] => abc
)
[1] => Array
(
[employeename] => def
)
)
Array
(
[0] => 1
[1] => 3
)
I need the second array value to be set in first array like this
Array
(
[0] => Array
(
[employeename] => abc
[othername] => 1
)
[1] => Array
(
[employeename] => def
[othername] => 3
)
)
Any Help Will be appreciated , Thanks In Advance :)
Try this :
<?php
$array1 = array(array("employeename" => "abc"),
array("employeename" => "def")
);
$array2 = array(1,3);
foreach($array1 as $key=>$val){
$array1[$key]["othername"] = $array2[$key];
}
echo "<pre>";
print_r($array1);
?>

array merge issue in php [duplicate]

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.

Remove duplicate values from an array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to remove duplicate values from a multi-dimensional array in PHP
I would like to know how to remove the duplicate array values from the following
Array (
[0] => Array ( )
[1] => Array (
[0] => 7
[1] => 8
)
[2] => Array (
[0] => 7
)
[3] => Array (
[0] => 8
)
)
Use array_unique PHP function.
array_unique($variable);

Categories