PHP Merge two indexed arrays but NOT normally [duplicate] - php

This question already has answers here:
Merge two flat indexed arrays of equal size so that values are pushed into the result in an alternating fashion
(2 answers)
Closed last month.
I have two arrays:
$array1 = array("A","One","C","Z");
$array2 = array("B","K","2","5");
Is there some built-in PHP way to get a final array with (I don't know how to say it, maybe 1-1 correspondence addition) alternate keys appended to each other like this
$final_array = array("A","B","One","K","C","2","Z","5");
If I use array_merge, I get:
$final_array = array("A","One","C","Z","B","K","2","5")
But that's exactly what an array_merge does. Is there any workaround other than looping?

Try this:
$array1 = array(1,3,5,7);
$array2 = array(2,4,6,8);
$final_array = array_merge($array1,$array2);
sort($final_array);
print_r($final_array);
Hope this helps. Sorted the array using the sort function.

Related

Merge Two Aarrys [duplicate]

This question already has answers here:
PHP - Merging two arrays into one array (also Remove Duplicates)
(9 answers)
Closed 3 years ago.
I have two arrays one is the superset of other,how to create a third array which has all the values of both arryas but not repeating
me please
$a = array(1,2,3);
$b = array(1,2,3,4);
output must be like this
$c = array(1,2,3,4);
use array_merge() along with array_unique()
array_unique(array_merge($a,$b));
Output:-https://3v4l.org/lfm1b
Note:- if you want to re-index array use array_values() [added in my working example link]
Reference:
array_values()
$c = array_unique(array_merge($a, $b));
All of this can be seen in: http://php.net/manual/en/function.array-merge.php
Use array_merge with array_unique
print_r(array_unique(array_merge($a,$b)))
Use + sign to merge them
$c = $a + $b;
Working example :- https://3v4l.org/5P2LP

how to remove duplicate elements from an array of arrays in PHP [duplicate]

This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 5 years ago.
"2017-08-31":["5948a0dd21146a43fdcfef5a","5948a0dd21146a43fdcfef5a"]
"2017-08-22":["5948a0dd21146a43fdcfef5a"]
"2017-08-09":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]
"2017-08-08":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]
I have an array like this, key is a date and multiple value associated with that date, but I need unique value with that key.how to do that ?
I've tried with array_unique but no luck!
Just use array_map and array_unique together.
<?php
$a = json_decode('{"2017-08-31":["5948a0dd21146a43fdcfef5a","5948a0dd21146a43fdcfef5a"],
"2017-08-22":["5948a0dd21146a43fdcfef5a"],
"2017-08-09":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"],
"2017-08-08":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]}', true);
echo json_encode(array_map("array_unique", $a));
$array = your array
$array = array_map(function($val){return array_unique($val);}, $array);
You better do this using SQL if it's possible. Otherwise, you can try this:
$array = array_unique($array, SORT_REGULAR);

loop through a multi-dimensional array and echo the value in PHP [duplicate]

This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I have a multi-dimensional array that takes the form:
array = [value1, some number],[value2, some number]...
I need to loop through the array and echo the value followed by a tokenizer so the final output looks like:
value1!##$value2!##$
I know that I have to concatenate the return value with ."!##$" but I do not know how to loop through the array. Can anyone offer some help.
My array is being made from a MySQL Query as follows:
while($row = $results -> fetch_assoc()) {
$return_array[] = array(
$row['uid'],($row['column1] - $row['column2']));
}
Then I am perfoming a usort on the array
To be simple enough, you can use implode and array_column:
$array = [['value1', 123], ['value2', 234]];
echo implode('!##$', array_column($array, 0)) . '!##$';
This gives:
value1!##$value2!##$
Explanation:
implode - Joins array values using some specified value, here !##$
array_column - implode accepts one dimensional array, also you want only the first index of the array to be joined, so creating an array with just first index.

Remove array duplicates PHP [duplicate]

This question already has answers here:
php remove duplicates from array
(5 answers)
Closed 9 years ago.
How do I remove duplicates from an array?
Let's say that my I have two arrays named $array and $new_array. $array has contents while $new_array is empty, as seen below:
$array = array(5,1,2,1,5,7,10);
$new_array = array();
I want $new_array to store the unique values of $array. It kind of goes like this:
$array = array(5,1,2,1,5,7,10);
$new_array = array(5,1,2,7,10); // removing the 1 and 5 after 2 since those numbers are already a duplicate of the preceding numbers.
echo $new_array; // Output: 512710
You can do it through PHP's array_unique function.
This function traverses through your provided array and returns an array with unique values (repeating values will be removed).
Code to return desired string:
$array = array(5,1,2,1,5,7,10);
$new_array = array_unique($array);
echo implode('', $new_array);
Use array_unique() and implode():
$array = array(5,1,2,1,5,7,10);
$new_array = array_unique($array);
echo implode('', $new_array);
Output:
512710

How can i get the common value from same array [duplicate]

This question already has answers here:
Eliminating duplicate values using PHP
(3 answers)
Closed 9 years ago.
I want to get the common value as well as different value from same array
For example :
$array1 = array(1,2,2,2,3,3,4,4,4,5,5,5,5,6,6,6,7,8,9,10);
And I want the array as
$array1 = array(1,2,3,4,5,6,7,8,9,10);
Can anyone give me the idea to bring the array like this
Why don't you try
$array1 = array(1,2,2,2,3,3,4,4,4,5,5,5,5,6,6,6,7,8,9,10);
$array1 = array_unique($array1);
http://php.net/manual/en/function.array-unique.php
Use array unique
array_unique($array1);
$array1 = array(1,2,2,2,3,3,4,4,4,5,5,5,5,6,6,6,7,8,9,10);
$array = array_unique($array1);
print_r($array )
see array_unique . It will remove the Duplicate values from array
try this :
$array1 = array(1,2,2,2,3,3,4,4,4,5,5,5,5,6,6,6,7,8,9,10);
$unique_array=array_unique($array1);
print_r($unique_array);
use array_unique(). It reduceds an array.

Categories