I have a array which structure is
<?php
$a = [1,2,3,4,5,6];
$b = [];
?>
I want to add indexes of variable $a one by one to variable $b.
$a = [1,2,3,4,5,6];
$b = array_keys($a);
$b = array_values($a);
or you can do as follows :
foreach ($a as $v){
array_push($b, $v);
}
I guess you are looking for something like this,
$a = [1,2,3,4,5,6];
foreach ($a as $key => $value){
$b[] = $key;
}
<?php
foreach (array(1, 2, 3, 4) as &$value) {
$value = $value * 2;
}
?>
From what I understand from your question
<?php
$a = [1,2,3,4,5,6];
$b = array();
for ($i=0; $i < count($a) ; $i++) {
array_push($b, $i);
}
print_r($b);
?>
foreach ($a as $v){
$b[] = $v;
}
Or if you just want to copy the array, you can use array_merge instead:
$b = array_merge(array(), $a);
I misunderstood the question a bit. If you want to copy the keys and not the value you could do like this with foreach-loop:
foreach ($a as $k=>$v){
$b[] = $k;
}
Other suggestion like array_keys would work as well.
Related
I had a array of list like this :
A = (a.11, b.12, c.dd)
I want to store the above array values in two different arrays like
B = (a, b, c)
C = (11,12,dd)
I tried a lot but all in vain. i am bit new to php. please help me out in this regard. Your prompt response is highly appreciated
Thanx
Hope this will help you:
$a = array("a.11,b.12,c.dd");
$b = array();
$d = array();
foreach ($a as $val)
{
$c =explode(',', $val);
foreach ($c as $v)
{
$e =explode('.', $v);
array_push($b,$e[0]);
array_push($d,$e[1]);
}
}
print_r($b);
print_r($d);
Working demo
foreach($A as $v) {
$v = explode('.', $v);
$B[] = $v[0];
$C[] = $v[1]
}
Try this,
$C = [];
$B = array_map(function($v) use(&$C){$arr = explode('.', $v); $C[] = $v[1]; return $v[0];}, $A);
Sum the individual columns of 3 different arrays
I need to sum the arrays of individual columns
$a = [1,2,3,4];
$b = [5,6,8,7];
$c = [10,11,4,70];
the output will be like that..
$d = [(1+5+10),(2+6+11),(3+8+4),(4+7+70)];
then $d will be
$d = [16,19,15,81];
Here is the code:
$a = [1,2,3,4];
$b = [5,6,8,7];
$c = [10,11,4,70];
$limit = count($a);
$d = array();
for($i=0;$i<$limit;$i++){
$d[] = $a[$i]+$b[$i]+$c[$i];
}
var_dump($d);//array(16,19,15,81)
I hope it helps
$a = [1,2,3,4];
$b = [5,6,8,7];
$c = [10,11,4,70];
$res = [];
for($i=0;$i<sizeof($a);$i++)
{
$res[$i]=$a[$i]+$b[$i]+$c[$i];
}
You can try like this
$a = [1,2,3,4];
$b = [5,6,8,7];
$c = [10,11,4,70];
$myArray = array($a, $b, $c);
$sumArray = array();
foreach ($myArray as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
print_r($sumArray);
So i'm working on a small php applications that combines four arrays.
Now there is a possibility that some of the possible arrays will be null.
I tried the following solution to merge the four arrays uniquely.
<?php
$a = [1,2,3,4,5];
$b = null;
$c = [5,4,3,2,1];
$d = [1,2];
$new_array;
if(is_array($a) && is_array($b) && is_array($c) && is_array($d))
{
$new_array = array_unique(array_merge($a,$b,$c,$d));
}else if(is_array($a) && is_array($b) && is_array($c))
{
$new_array = array_unique(array_merge($a,$b,$c));
}else if(is_array($a) && is_array($b))
{
$new_array = array_unique(array_merge($a,$b));
}else{
$new_array = $a;
}
print_r($new_array);
?>
I soon realized my code was highly dysfunctional in that it does not cater for all possible combinations of arrays while excluding the null variables.
How do I solve this. Ensuring that all the variables that are arrays are merged a nd those that are not are discarded.
Thanks
how about this? putting all the array's in an array, so you can loop through them all easily, and use a custom in_array() function to check if they are already existing?
The good thing about this way is that it doesn't limit the script to just four array's, as it will loop through all the array's you get together.
$a = [1,2,3,4,5];
$b = null;
$c = [5,4,3,2,1];
$d = [1,2];
$array_stack = array($a, $b, $c, $d);
$new_array = array();
foreach($array_stack as $stack){
if(!is_array($stack)) continue;
foreach($stack as $value){
if(!in_array($value, $new_array)) $new_array[] = $value;
}
}
print_r($new_array);
maybe something like this :
<?php
$a = [1,2,3,4,5];
$b = null;
$c = [5,4,3,2,1];
$d = [1,2];
$new_array;
if(is_array($a)) $new_array = $a;
if(is_array($b)) $new_array = array_unique(array_merge($new_array,$b));
if(is_array($c)) $new_array = array_unique(array_merge($new_array,$c));
if(is_array($d)) $new_array = array_unique(array_merge($new_array,$d));
?>
Old question, but going to give my input anyways. A more universal approach:
function multiple_array_merge() {
$args = func_get_args();
$array = [];
foreach ($args as $i) {
if (is_array($i)) $array = array_merge($array, $i);
}
return array_unique($array);
}
$a = [1, 2, 3, 4, 5];
$b = null;
$c = [5, 4, 3, 2, 1];
$d = [1, 2];
$merged = multiple_array_merge($a, $b, $c, $d);
I have a php array as follows,
<?php
$arr = array('op'=>'pqr', 'ab'=>'xyz', 'mn'=>'abcd');
?>
How to set xyz value as first element with minimum loop,if the value exist.
Expected Result
<?php
$arr = array('ab'=>'xyz', 'op'=>'pqr','mn'=>'abcd');
?>
$ab = $array['ab'];
unset($array['ab']);
$array = array('ab' => $ab) + $array;
If the key itself is unknown, find it first:
$key = array_search('xyz', $array);
$tmp = $array[$key];
unset($array[$key]);
$array = array($key => $tmp) + $array;
Or go with a sort:
uasort($array, function ($a, $b) {
if ($a == 'xyz') return -1;
if ($b == 'xyz') return 1;
return 0;
});
<?php
$arr = array('op'=>'pqr', 'ab'=>'xyz', 'mn'=>'abcd');
ksort($arr);
echo '<pre>';
print_r($arr);
This question already has answers here:
How to filter an associative array comparing keys with values in an indexed array?
(12 answers)
Closed last year.
I have an associative arrays and an array of keys.
$A = array('a'=>'book', 'b'=>'pencil', 'c'=>'pen');
$B = array('a', 'b');
How I build an associative array from all element of $A where the key is in $B?
For the example above, the answer should be
$C = array('a'=>'book', 'b'=>'pencil');
$keys = array_flip($B);
$C = array_intersect_key($A,$keys);
array_intersect_key($A,array_combine($B,$B))
or better: array_intersect_key($my_array, array_flip($allowed))
from the question: PHP: How to use array_filter() to filter array keys?
Here's a simple solution which checks that the key exists in $A before appending it to $C
$A = array('a'=>'book', 'b'=>'pencil', 'c'=>'pen');
$B = array('a', 'b');
$C = array();
foreach ($B as $bval) {
// If the $B key exists in $A, add it to $C
if (isset($A[$bval])) $C[$bval] = $A[$bval];
}
var_dump($C);
// Prints:
array(2) {
["a"]=>
string(4) "book"
["b"]=>
string(6) "pencil"
}
$keys = array_keys($B);
$C = array();
foreach ($A as $key => $value)
{
if (in_array($key, $keys))
{
$C[$key] = $value;
}
}
To my immense surprise, the foreach loop method is faster.
The following quick benchmark script gives me results:
array_intersect_key: 0.76424908638
foreach loop: 0.6393928527832
$A = array('a'=>'book', 'b'=>'pencil', 'c'=>'pen');
$B = array('a', 'b');
$start = microtime(true);
for ($i = 0 ; $i < 1000000; $i++) {
$c = array_intersect_key($A,array_flip($B));
}
$t1 = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$C = array();
foreach ($B as $bval) {
// If the $B key exists in $A, add it to $C
if (isset($A[$bval])) $C[$bval] = $A[$bval];
}
}
$t2 = microtime(true);
echo "array_intersect_key: " . ($t1 - $start), "\n";
echo "foreach loop: " . ($t2 - $t1), "\n";