This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php values of one array to key of another array
I have the sorted array
$A = array(
0=>EUR,
1=>GBP,
2=>USD
);
$B = array(
0=>'0.88',
1=>'0'
);
I want to map to be like this allways put 'EUR'=>'1':
$C = array(
'EUR'=>'1',
'GBP'=>'0.88',
'USD'=>'0'
);
Could anyone tell me please?
$C = array_combine($A, array_merge(array(1), $B));
$C = array();
foreach ($A as $key => $value)
{
$C[$value] = $B[$key];
}
Related
This question already has answers here:
How to cast array elements to strings in PHP?
(7 answers)
Closed last year.
I have a data array like this:
$data = array(1,2,3);
how to change the array like this:
$data = array('1','2','3');
Tanks Before
You have to change type of all array values, just like this
foreach ($data as $key => $val) {
$data[$key] = (string) $val;
}
or
function convert2string($n)
{
return (string) $n;
}
$a = [1, 2, 3];
$data = array_map('convert2string', $a);
This question already has answers here:
Split associative array by key
(6 answers)
Get the first N elements of an array?
(5 answers)
php - get numeric index of associative array
(7 answers)
Closed 2 years ago.
There is an array in php
$sampleArray = array('first' => 'first', 'second' => 'second', 'third' => 'third');
I need to splitting the array based on value $check, if the $check is 'second', then the array need to be split like this
$requiredArray = array('third'=>'third')
$nonReqArray = array('first'=>'first','second'=>'second')
if the $check is 'third' then the $requiredArray will be empty. Please suggest the solution to this?
Tried with this
$afterResult = array_filter($sampleArray, function($key) use (&$requiredArray, &$nonReqArray, &$check) {
if($key > $check){
$requiredArray[$key] = $key;
}else{
$nonReqArray[$key] = $key;
}
return true;
},
ARRAY_FILTER_USE_KEY);
Usually solved with for loops. But we can solve by array_filter array function. The main issue is indexing is not numerical, so comparing is not easy.
$cmp = false;
$afterResult = array_filter($sampleArray, function($key) use (&$requiredArray, &$nonReqArray, &$check, &$cmp) {
if($cmp){
$requiredArray[$key] = $key;
}else{
$nonReqArray[$key] = $key;
}
if($key == $check){
$cmp =true;
}
return true;
},
ARRAY_FILTER_USE_KEY);
This worked for me.
This question already has answers here:
PHP Find All (somewhat) Unique Combinations of an Array
(6 answers)
Closed 8 years ago.
I have list of 100 First Name and 100 Surname.
I want use mix of all the combinations without repetition.
Do you have some math solutions for this?
this is called "cartesian product", php man page on arrays http://php.net/manual/en/ref.array.php shows some implementations (in comments).
and here's yet another one:
function array_cartesian() {
$_ = func_get_args();
if(count($_) == 0)
return array(array());
$a = array_shift($_);
$c = call_user_func_array(__FUNCTION__, $_);
$r = array();
foreach($a as $v)
foreach($c as $p)
$r[] = array_merge(array($v), $p);
return $r;
}
$cross = array_cartesian(
array('A', 'B', 'C'),
array('1', '2')
);
print_r($cross);
This question already has answers here:
Cartesian Product of N arrays
(10 answers)
Closed 9 years ago.
Let's say I have several arrays:
$array1 = array( 'a','b','c');
$array2 = array( '1','2','3');
$array3 = array( '+','-');
As a result I'd like to have a array of all possible mixes of those arrays:
$result = array( 'a1+','a1-','a2+','a2-','b1+','b1-','b2+'...
SQL provides such operation in case of the following request:
SELECT * FROM `letters`,`digits`,`operations`
Ho can I do this in PHP?
$permute= array();
foreach($array1 as $x)
foreach($array2 as $y)
foreach ($array3 as $z)
$permute[]= $x.$y.$z;
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
This is really simple but I need a quick way to do this.
I have three arrays like
$a = array('a','b','c');
$p = array('p','q','r');
$x = array('x','y','z');
How do I combine them to make
array (
[0] => array ('a','p','x');
[1] => array ('b','q','y');
[2] => array ('c','r','z');
);
Wouldn't array_map(null, $a, $p, $x); be better?
See array_mapĀDocs.
<?php
$a = array('a','b','c');
$p = array('p','q','r');
$x = array('x','y','z');
$arr = array();
for($i=0; $i<count($a); $i++){
$arr[$i] = array($a[$i], $p[$i], $x[$i]);
}
?>
array_map is simpler, but for the sake of possibibility, a quickly typed code example to make use of the MultipleIterator to solve the issue:
$it = new MultipleIterator;
foreach(array($a, $p, $x) as $array) {
$it->attachIterator(new ArrayIterator($array));
}
$items = iterator_to_array($it, FALSE);
Might be handy in case it's more than an array.