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);
Related
Probably a duplicate...
Array1 = ['a'=>1, 'b'=>2, 'c'=>3];
Array1 = ['a'=>1, 'b'=>2, 'c'=>'anaconda'];
$keys = ISTHEREAFUCNTION(array1, array2);
echo ($keys);
// c
Is there a function that compares arrays by value and return keys of array1 values different from values of array2?
I can do it just iterating over both arrays, but maybe there is a more elegant solution?
Thank you!
Using array_diff() and array_keys()
$ cat test.php
<?php
$arr1 = ['a'=>1, 'b'=>2, 'c'=>3];
$arr2 = ['a'=>1, 'b'=>2, 'c'=>'anaconda'];
print_r(array_keys(array_diff($arr1,$arr2)));
?>
$ php test.php
Array
(
[0] => c
)
Use array_keys and array_diff_assoc
array_keys(array_diff_assoc($array1, $array2));
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 an array that contains names of arrays
$names_array[] = ('$array1', '$array2', $array3'....)
The $names_array[] is dynamically updated so it may contain 2 or more different names.
When the script is executed the values of listed arrays in $names_array[] need to be merged.
I think in case of merging it is not a problem
u can merge $result = array_merge($array1, $array2);
http://php.net/manual/en/function.array-merge.php
I think it can be done with variable variables.
$arraymerge = array();
foreach ($names_array as $arrayname)
{
$arraymerge = array_merge($arraymerge, ${$arrayname});
}
Thanks for help... I have went around the problem: if anyone needs to merge dynamically generated arrays, in my case I have six arrays that exist or not, so I need to merge the existing ones. What I did is:
if(!is_array($array1[$i])) $array1[$i]=array();
if(!is_array($array2[$i])) $array2[$i]=array();
if(!is_array($array3[$i])) $array3[$i]=array();
if(!is_array($array4[$i])) $array4[$i]=array();
if(!is_array($array5[$i])) $array5[$i]=array();
if(!is_array($array6[$i])) $array5[$i]=array();
$combineddata[$i]=array_merge($array1[$i], $array2[$i],$array3[$i],$array4[$i], $array5[$i], $array6[$i]);
In case 'array_x[$i]' doesn't exists array_merge doesn't break the script just merges empty array.
Thanks
$names_array = array ('array1', 'array2', 'array3');
$array1 = array ('a','b','c');
$array2 = array ('d','e','f');
$array3 = array ('g','h','i');
$result = array ();
foreach ($names_array as $x) {
$result = array_merge ($result, $$x);
}
print_r ($result);
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));
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