How to combine two arrays [duplicate] - php

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

Related

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

How can I implode() only one column from a multidimensional array? [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 array in following format:
Array
(
[sales] => Array
(
[0] => Array
(
[0] => 1
[1] => 6
)
[1] => Array
(
[0] => 2
[1] => 8
)
[2] => Array
(
[0] => 3
[1] => 25
)
[3] => Array
(
[0] => 4
[1] => 34
)
)
)
Using:
foreach ($data['sales'] as $k => $row) {
$list = implode(",",$row);
}
I get the following as output:
1,62,83,254,34
But I only need the second values from each subArray. The expected result needs to be:
6,8,25,34
How can I remove the first set of values?
Just grab the first column from your array with array_column(), so that you end up with an array, e.g.
Array (
[0] => 6
[1] => 8
[2] => 25
[3] => 34
)
And implode() it then as you already did, e.g.
echo implode(",", array_column($data["sales"], 1));
output:
6,8,25,34
I like array_column() but if you don't have PHP >= 5.5.0:
$list = implode(',', array_map(function($v) { return $v[1]; }, $data['sales']));
Or with the foreach:
foreach ($data['sales'] as $row) {
$list[] = $row[1];
}
$list = implode(',', $list);

sort results of array count values into a new array

I want to sort the results to a new array but without the $key
so the most UNunique number will be first (or the most duplicated number will be first).
<?php
$a = array (1,1,2,2,2,3,3,3,3,4,4,4,4,4,5);
foreach (array_count_values($a) as $key => $value) {
echo $key.' - '.$value.'<br>';
}
//I am expecting to get the most duplicated number FIRST (without the $key)
//so in that case :
// $newarray = array(4,3,2,1,5);
?>
$a = array (1,1,2,2,2,3,3,3,3,4,4,4,4,4,5);
$totals = array_count_values($a);
arsort( $totals );
echo "<pre>";
print_r($totals);
Output
Array
(
[4] => 5
[3] => 4
[2] => 3
[1] => 2
[5] => 1
)
Do like this
<?php
$a = array (1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,6,7,8,9);
$n=array_count_values($a);
arsort($n);
print_r(array_keys($n));
Demo
OUTPUT:
Array
(
[0] => 4
[1] => 3
[2] => 2
[3] => 1
[4] => 9
[5] => 8
[6] => 5
[7] => 6
[8] => 7
)
$newarray = array_keys(array_count_values($a));

Modify array keys of an associative array [duplicate]

This question already has answers here:
Replace keys in an array based on another lookup/mapping array
(25 answers)
Closed 9 years ago.
I have this array
$the_posted = Array
(
0 => Array
(
0 => 1,
1 => 2,
2 => 3,
3 => 4,
),
1 => Array
(
0 => 5,
1 => 6,
2 => 7,
3 => 8,
)
);
whose keys i need to modify.I trying to modify the array keys like
$all_array_keys = array_keys($the_posted);
foreach ( array_keys($the_posted) as $k=>$v )
{
$all_array_keys[$k]= rand();
}
echo '<pre>';
print_r($all_array_keys);
echo "<hr/>";
print_r($the_posted);
echo '<pre>';
I get this result
Array
(
[0] => 25642
[1] => 8731
)
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[1] => Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
)
)
The change in keys is not reflected in the final array.How do i make this work?.
You can use following code :
foreach ( array_keys($the_posted) as $k=>$v )
{
$new_key = rand();
$new_posted[$new_key] = $the_posted[$v];
unset($the_posted[$v])
}
Here, we have created a new array $new_posted which will have data with new keys like this :
Array
(
[28228] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[23341] => Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
)
)
To change the key of an item, do something like this:
$the_posted[$newkey] = $the_posted[$oldkey];
unset($the_posted[$oldkey]);
So in your case:
foreach ( $the_posted as $k=>$v )
{
$newkey = rand();
while(isset($the_posted[$newkey]))
$newkey = rand();
$the_posted[$newkey] = $the_posted[$k];
unset($the_posted[$k]);
}

Array sorting question

So I have an array such as this one:
Array
(
[-1] => Array
(
[3] => 3
[1] => 1
[6] => 6
[7] => 7
[5] => 5
)
)
It also contains some other keys that should not be modified.
I'd like to the numbers which are in a second array to come first (in the order of that second array), and then will be the numbers that don't exist in the second array, if any.
So for that matter, the second array would be:
Array
(
[0] => 6
[1] => 5
[2] => 3
)
And the final array should be as follows (please remember, there are some more keys inside of that array that should stay as they are):
Array
(
[-1] => Array
(
[6] => 6
[5] => 5
[3] => 3
[1] => 1
[7] => 7
)
)
Any ideas how that can be done?
Thanks!
It's not and shouldn't be termed as sorting but may be this code snippet may help you do what you want to:
$a1 = Array ( [-1] => Array ( [3] => 3 [1] => 1 [6] => 6 [7] => 7 [5] => 5 ) );
$a2 = Array ( [0] => 6 [1] => 5 [2] => 3 );
$sorted = getSortedArray($a1[-1] , $array2);
function getSortedArray($array1 , $array2){
$temp = Array();
$count = 0;
$totalKeys = sizeof($array2);
for($i=0;$i<sizeof($array2);$i++){
$temp[i] = $array1[$array2[i]];
unset($array1[$array2[i]]);
}
while($count!=sizeof($array1))
$temp[$totalKeys++] = $array1[$count++];
return $temp;
}
I believe the function you're looking for is called array_multisort().
array_multisort() can be used to sort
several arrays at once, or a
multi-dimensional array by one or more
dimensions.

Categories