I have two arrays:
$a = array([0]=>1 [1]=>2 [2]=>3);
$b = array([0]=>a [1]=>b [2]=>c);
I want to merge them like
$ab=array( [a]=>array([0]=>1 [1]=>2 [2]=>3)
[b]=>array([0]=>a [1]=>b [2]=>c) );
How to do this ?
I tried array_merge() but it does not work as I want it to.
In this case you can just go:
$ab = array('a' => $a, 'b' => $b);
You can use compact() method:
$ab = compact('a', 'b');
where 'a' and 'b' will used as array keys and treated as variable names to assign values, so it will do array('a' => $a, 'b' => $b)
You've pretty much written the answer already.
$ab = array('a' => $a, 'b' => $b);
$ab = array('a' => $a, 'b' => $b);
Will result in:
$ab['a'] = array([0] => 1, [1] => 2, [2] => 3);
$ab['b'] = array([0] => a, [1] => b, [2] => c);
Or you can just do $ab = array($a, $b) if you don't want the keys (it is unclear in your question if a and b are strings or integers).
Related
I have print_r result like this :
Array
(
[0] => A, B, C, D
[1] => 15,20,24,19
)
how to make them like this :
Array
(
[A] => 15
[B] => 20
[C] => 24
[D] => 19
)
Great thanks for help :)
Try this:
$a = array('A', 'B', 'C', 'D');
$b = array(15, 20, 24, 19);
$c = array();
foreach ($a as $index => $value) {
if (isset($b[$index])) {
$c[$value] = $b[$index];
}
}
var_dump($c);
You need explode() and array_combine(). Assuming your initial array, $arr:
$new_arr = array_combine(explode(', ', $arr[0]), explode(',', $arr[1]));
See demo
Try to explode() your array index by comma and combine both array with keys and values using array_combine()
$a = explode(',',$arr[0]);
$b = explode(',',$arr[1]);
$new = array_combine($a,$b);
print_r($new); //Array ( [A] => 15 [ B] => 20 [ C] => 24 [ D] => 19 )
array_combine is the way
<?php
$myArray = array(
array('A', 'B', 'C', 'D'),
array(15, 20, 24, 19)
);
$combinedArray = array_combine($myArray[0], $myArray[1]);
$a = array('x' => 1, 'y' => 2, 'z' => 3, 'a' => 4, 'b' => 4);
$b = array('x', 'z', 'a');
I am looking for a NATIVE PHP function that will take the 2 above arrays and return
array('x' => 1, 'z' => 3, 'a' => 4);
I do not want to use a loop.
Any help would be appreciated.
This can be solved using array_intersect_key (http://uk3.php.net/manual/en/function.array-intersect-key.php) and array_flip (http://uk3.php.net/manual/en/function.array-flip.php);
array_intersect_key($a, array_flip($b));
See this example; https://eval.in/158360
array_intersect_key($a, array_fill_keys($b, 1));
There is native function array_intersect_key which computes the intersection of several arrays using their keys as base. But your second array is a list of values which we need to be keys. So we can use function array_fill_keys which take first argument as list of keys and create an array filled with value, specified by second parameter ( we will take integer 1, for instance ):
$b = array('x', 'z', 'a');
$bValsToKeys = array_fill_keys($b, 1);
$bValsToKeys will be
Array ( [x] => 1 [z] => 1 [a] => 1 )
And then we can intersect it with $a:
$result = array_intersect_key($a, $bValsToKeys);
print_r($result);
Out:
Array ( [x] => 1 [z] => 3 [a] => 4 )
I have 2 array
a=array(a=>1,b=>2,c=>2,d=>2,e=>2,f=>2)
and
b=array(a,b,d)
I want to make function compare_plus(array a, array b) like if array a have key== array b val then in increase value of array a at this key by 1.
Example with above array a and b:
c=compare_plus(a,b) =>> c=(a=>2,b=>3,c=>2,d=>3,f=>2)
$a = array('a' => 1, 'b' => 2, 'c' => 2, 'd' => 2, 'e' => 2, 'f' => 2);
$b = array('a', 'b', 'd');
$c = compare_plus($a, $b);
print_r($c);
function compare_plus($arr, $plusarr){
foreach($plusarr as $key)
$arr[$key]++;
return $arr;
}
Codepad Demo
If you want to add only to existing keys and not create additional ones, you'll need something like this:
$a = array("a" => 1, "b" => 2, "c" => 2, "d" => 2, "e" => 2, "f" => 2);
$b = array("a", "b", "d", "g", "apple");
$c = compare_plus($a, $b);
print_r($c);
function compare_plus($arr, $plusarr){
foreach($plusarr as $key)
if (array_key_exists($key, $arr))
$arr[$key]++;
return $arr;
}
/* // Output:
Array
(
[a] => 2
[b] => 3
[c] => 2
[d] => 3
[e] => 2
[f] => 2
)
*/
To add the additional keys from $b to $c, simply remove if (array_key_exists($key, $arr)).
http://codepad.org/aquc5DKA
Let say I have an array like the following:
$list[] = array('a' => 1, 'b' => 4);
$list[] = array('a' => 2, 'b' => 5);
$list[] = array('a' => 3, 'b' => 6);
Is it possible to add a new Key & Value like c => null to all the array elements, but WITHOUT looping through all records or using map like functions?
The end result should be something similar to this:
$list[] = array('a' => 1, 'b' => 4, 'c' => null);
$list[] = array('a' => 2, 'b' => 5, 'c' => null);
$list[] = array('a' => 3, 'b' => 6, 'c' => null);
Actually, I'm thinking of making the second array on-the-fly and combine it to the original one, or simply looping through the array and extend it, however, just wondering if there is any better alternative way for doing that.
Shamelessly:
array_walk($list, function(&$v){ $v['c'] = null; });
Okay further to comments
If you are worried about the structure of your array and wish certain keys to be present even if they are not explicitly defined then you are trying to describe something specific - in which case you are better defining that object in a class - from both a readability and itegrity standpoint.
For example:
class MyObject {
public $a;
public $b;
public $c;
function __construct(){
// this line is lazy but it's just a demo
list($a,$b,$c) = func_get_args();
$this->a = $a;
$this->b = $b;
$this->c = $c;
}
}
$list[] = new MyObject(1, 4);
$list[] = new MyObject(2, 5);
$list[] = new MyObject(3, 6);
print_r($list);
run code
In this instance - if we do not specify "c" in the constructor - it is still implicitly set as null. As a consequence you do not need to loop through the $list array again afterwards. Though for completeness I should probably point out here that there is possibly a speed-memory trade-off.
Output:
Array
(
[0] => MyObject Object
(
[a] => 1
[b] => 4
[c] =>
)
[1] => MyObject Object
(
[a] => 2
[b] => 5
[c] =>
)
[2] => MyObject Object
(
[a] => 3
[b] => 6
[c] =>
)
)
No, you can't do this without walking through the array, either explicitly through a for loop, or implicitly using array_walk or similar
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
+ operator for array in PHP?
If $a and $b are both arrays, what is the result of $a + $b?
http://www.php.net/manual/en/language.operators.array.php
Union of $a and $b.
The + operator appends elements of
remaining keys from the right handed
array to the left handed, whereas
duplicated keys are NOT overwritten.
<?php
$a = array(1, 2, 3);
$b = array(4, 5, 6);
$c = $a + $b;
print_r($c);
results in this for me:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
BUT:
<?php
$a = array('a' => 1, 'b' => 2, 'c' => 3);
$b = array('d' => 4, 'e' => 5, 'f' => 6);
$c = $a + $b;
print_r($c);
results in:
Array
(
[a] => 1
[b] => 2
[c] => 3
[d] => 4
[e] => 5
[f] => 6
)
So it would appear that the answer here depends on how your arrays are keyed.
My test
$ar1 = array('1', '2');
$ar2 = array('3', '4');
$test = $ar1 + $ar2;
print_r($test);
Array
(
[0] => 1
[1] => 2
)
Now try this experiment
$a = array( 0 => 1,
1 => 2,
4 => 3
);
$b = array( 2 => 4,
4 => 5,
6 => 6
);
$c = $a + $b;
var_dump($c);
If you do something like $result = $a + $b; then $result will be assigned to the first argument, in this case $a.