I have 2 arrays , $array1 & $array2. I want to create new array such that its key value is values of array1 and values are values of array2. Is it possible..
I used following approach to create this new array named $inputs. Is this correct?
$inputs=array();
$array1=array("3","4","6");
$array2=array("a","b","c");
$inputs=array_fill("$array1",count($array1),$array2);
print_r($inputs);
If I read your question correctly, you can use array_combine() PHP Manual
$array1 = array("3","4","6");
$array2 = array("a","b","c");
$inputs = array_combine($array2, $array1); # keys, values
Use array_combine.
http://www.php.net/manual/de/function.array-combine.php
$inputs = array_combine($array1, $array2);
<?php
$inputs=array();
$array1=array("3","4","6");
$array2=array("a","b","c");
$inputs=array_combine($array1,$array2);
print_r($inputs);
?>
http://codepad.org/6fTXCZa5
use php array_combine function
http://php.net/manual/en/function.array-combine.php
Related
How do I combine these two arrays into a single array say $array3?
$array1 =[1,2,3];
$array2=['a'=>1,'b'=>2,'c'=>3];
Try this
$temp = array_slice(array_keys($array1), 0, count($array2));
$array3 = array_merge($array1, array_combine($temp, $array2));
I believe this is what you're asking.
I have 4 arrays for input errors like:
$username_errors=array();
$email_errors=array();
$password_errors=array();
$errors=array();
I want to merge all the arrays values in $errors array and count if $errors is empty then proceed.
Thanks in advance.
use array_merge
$arr = array_merge($arr1, $arr2, $arr3);
$array3 = array_merge($array1, $array2);
If I have two arrays and I want to "combine" them into one array whilst maintaining each individual array and leaving the keys as they are as they are always duplicated each iteration, can I do this?:
$array1 = array('0'=>'Bob', '1'=>'Tom', '2'=>'John');
$array2 = array('0'=>'Michelle', '1'=>'Joan', '2'=>'Susan');
If I use array_merge:
$new_array = array_merge($array1, $array2);
I get:
array('0'=>'Bob','1'=>'Tom','2'=>'John','3'=>'Michelle','4'=>'Joan','5'=>'Susan')
whereas I want to get something like:
array(array('0'=>'Bob', '1'=>'Tom', '2'=>'John'),array('0'=>'Michelle', '1'=>'Joan', '2'=>'Susan'))
Create a new array and add the other arrays to that one like this:
$arr = array($array1, $array2);
$ new_array = array ($ array, $ array2);
Array1:
array('key01'=>321312, 'key03'=>23)
Array2:
array('key01'=>22, 'key04'=>78, 'key05'=>54)
I'm trying to to replace the values if array1 with the values of array2 and leaving any keys untouched that are not in array2.
So the outcome would be:
array('key01'=>22, 'key03'=>23, 'key04'=>78, 'key05'=>54)
You can use array_merge:
$a1 = array('key01'=>321312, 'key03'=>23);
$a2 = array('key01'=>22, 'key04'=>78, 'key05'=>54);
print_r(array_merge($a1,$a2));
$arr1 = $arr2 + $arr1;
The keys will remain as you said:
$arr1 = array('key01'=>22, 'key04'=>78, 'key05'=>54, 'key03'=>23);
But the order is important. In the case above, $arr2, being first, will overwrite values with the same key of $arr1.
I've an array like below :
$array = array('string'=>'hello','somethingeElse'=>'how r u', ....);
I wanna change the keys of array to numeric values (consecutive):
$array = array('1'=>'hello','2'=>'how r u','3'=>....);
any help would be appreciated ;)
You could use the array_values() function, which will basically do what you say.
array_values() returns all the values from the input array and indexes
numerically the array.
Example:
$array = array_values($array);
If you want to avoid PHP loops, you may try something like :
$newArray = array_combine(range(1, count($array)), array_values($array));