$array = array(array("a"=>1),array("a"=>2));
I need to push data to sub array element in $array,
End result must be as follow,
Array ( [0] => Array ( [a] => 1 [b] => 2 ) [1] => Array ( [a] => 2 [b] => 2 ) )
I used following ways .
foreach($array as &$a){ $a['b']=2;}
$result = array_map("pushdata",$array);
function pushdata($a){
$a['b']=2;
}
what is the most suitable and performance high way when $array consists of more than 1000 records ?
Here is an example using array_walk() to add a new key b to each sub-array:
$array = array(array('a' => 1), array('a' => 2));
array_walk($array, function(&$item, $key) {
$item['b'] = 2;
});
print_r($array);
/* outputs:
Array
(
[0] => Array
(
[a] => 1
[b] => 2
)
[1] => Array
(
[a] => 2
[b] => 2
)
)
*/
Here is an example for 5 items.
<?php
for($i = 1 ; $i<5 ; $i++){
$array[] = array("a"=>$i,"b"=>2);
}
print_r($array);
?>
See online
Use array_walk , to iterate over the array and array_push to push the element to each iteration.
Related
I need to merge 2 arrays and i have found lot of examples here on stackoverflow, but nothing has worked for me, in my case, so i explain my case:
Arrays (can be one, two, or three, or more...):
Array ( [0] => name-file_icon_001_00.png-
[1] => name-file_icon_002_00.png-
[2] => name-file_icon_003_00.png- )
Array ( [0] => rel
[1] => rel
[2] => rel )
Or can be two:
Array ( [0] => name-file_icon_001_00.png-
[1] => name-file_icon_002_00.png- )
Array ( [0] => rel
[1] => rel )
Or one:
Array ( [0] => name-file_icon_001_00.png- )
Array ( [0] => rel )
Need to insert the relative value "[0] => rel" with "[0] => name-file_icon_001_00.png-"
Expected result (merged):
Array ( [0] => name-file_icon_001_00.png-rel
[1] => name-file_icon_002_00.png-rel
[2] => name-file_icon_003_00.png-rel )
Reading around the web, seems that not exist a native function for make this.
Please, hope in your help :)
A simple foreach loop using the index and value parameters will do it in no time
Example
$a1 = ['name-file_icon_001_00.png-',
'name-file_icon_002_00.png-',
'name-file_icon_003_00.png-'
];
$a2 = ['rel1','rel2','rel3'];
foreach ($a1 as $i => $v){
$new[] = $v . $a2[$i];
}
print_r($new);
RESULT
Array
(
[0] => name-file_icon_001_00.png-rel1
[1] => name-file_icon_002_00.png-rel2
[2] => name-file_icon_003_00.png-rel3
)
You can map each array to a function that concatenates them:
$result = array_map(function($a, $b) { return $a.$b; }, $one, $two);
If you define one array with subarrays then you can unpack that array ...:
$array = [$one, $two];
$result = array_map(function($a, $b) { return $a.$b; }, ...$array);
Or for fun, you can extract each column from the subarrays and implode them:
$array = [$one, $two];
for($i=0; $a=array_column($array, $i); $i++) {
$result[] = implode($a);
}
I have a array $a and some key arrays e.g. $keys1, $keys2,....
$a = array('a'=>array('b'=>array('c'=>array('d'=>123,'s'=>4),'r'=>3),'q'=>2),'p'=>1);
$keys1 = array('a','b','c');
$keys2 = array('a','b');
$a = Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => 123
[s] => 4
)
[r] => 3
)
[q] => 2
)
[p] => 1
)
whenever $keys1 is used, output should be
Array
(
[d] => 123
[s] => 4
)
or whenever $keys2 is used, output should be
Array
(
[c] => Array
(
[d] => 123
[s] => 4
)
)
this is very simple I can achieve result by using $a[a][b][c] in first case and by using $a[a][b] in second case
Problem: these $keys are provided in form of array at run time,
Is there any function in php to get the result?
Try this.
Comments are in code.
It will loop through the keys and return the searched value.
$a = array('a'=>array('b'=>array('c'=>array('d'=>123,'s'=>4),'r'=>3),'q'=>2),'p'=>1);
$keys1 = array('a','b','c');
$keys2 = array('a','b');
$keys = $keys1; // set this as "search value"
$value = $a; // create a copy of original array. Maybe not needed?
Foreach($keys as $key){ // loop keys in search value
$value = $value[$key]; // overwrite $value with subarray of $value[$key]
}
Var_dump($value); // dump the search return.
https://3v4l.org/ZHBMa
I have two associative arrays.
array ( [apple]=>1 [banana]=>2 cocunet => 3)
and other array is
array ( [apple]=>2 [banana]=>3 cocunet => 4)
now i want to merge my array like that
array ( [apple]=>1,2 [banana]=>2,3 cocunet => 3,4)
There is no such array in PHP. The thing you want can only be done by creating multidimentional arrays.
$a1 = array( 'apple'=>1,'banana'=>2,'coconut'=> 3);
$a2 = array( 'apple'=>2,'banana'=>3,'coconut'=> 4);
echo "<pre>";
print_r(array_merge_recursive($a1,$a2));
echo "</pre>";
For this you can use the array_merge_recursive() function.
PHPFiddle: http://3v4l.org/5OCKI
If you want the result to be a string then this should work:
foreach( $array_1 as $fruit => $num) {
if(array_key_exists($fruit, $array_2)){ //check if key from array_1 exists in array_2
$final_array[] = array($fruit => $array_1[$fruit].','.$array_2[$fruit]); //concatenate values from shared key
}
}
print_r($final_array) will return:
Array
(
[0] => Array
(
[apple] => 1,2
)
[1] => Array
(
[banana] => 2,3
)
[2] => Array
(
[coconut] => 3,4
)
)
<?php
$array1 = array("apple" => 5, "banana" => 1);
$array2 = array("banana" => 4, "coconut" => 6);
print_r( array_merge_recursive( $array1, $array2 ) );
?>
Returns:
Array ( [apple] => 5 [banana] => Array ( [0] => 1 [1] => 4 ) [coconut] => 6 )
I only used two elements in each of the primary arrays to demonstrate the output prior to having any groups existing, i.e. non-array value.
i have an array:
Array
(
[0] => Array
(
[setid] => 2
[income] => 100
)
[1] => Array
(
[setid] => 2
[income] => 120
)
[2] => Array
(
[setid] => 3
[income] => 700
)
)
i need to find entrys with the same setid, sum their income up and delete duplicate entrys - in the end it should look like this:
Array
(
[0] => Array
(
[setid] => 2
[income] => 220
)
[1] => Array
(
[setid] => 3
[income] => 700
)
)
does someone know a sophosticated solution for my problem or do i have to take the long road and do every step manually?
thanks and greetings
Just create a new array which you make fast adressable by using the setid as key. And reindex the array at the end.
$result = array();
foreach ($array as $val) {
if (!isset($result[$val['setid']]))
$result[$val['setid']] = $val;
else
$result[$val['setid']]['income'] += $val['income'];
}
$result = array_values($result); // reindex array
This should work:
$values = array();
foreach($array as $val) {
if(isset($values[$val['setid']])) {
$values[$val['setid']] += $val['income'];
} else {
$values[$val['setid']] = $val['income'];
}
}
//all values are now in $values array, keys are setid and values are income
Write your own function, that's no long road at all.
$result = array_reduce($originalArray, function($memo, $item){
isset($memo[$item['setid']])
? $memo[$item['setid']] = $item['income']
: $memo[$item['setid']] += $item['income'];
return $memo;
}, []);
You should use the array_unique function that php's official site offers.
An example:
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
Output:
Array
(
[a] => green
[0] => red
[1] => blue
)
To sum the duplicated values you can use the array_count_values function:
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
Output would be:
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
I am looking for a function, if any, to preserve or merge values into similar key, but no luck so far.
array_combine simply removes the value:
array_combine(array('a','a','b'), array(1,2,3));
Returns:
Array
(
[a] => 2
[b] => 3
)
Expected:
Array
(
[a] => 1,2
[b] => 3
)
Any hint is very much appreciated.
Thanks
UPDATE: I didn't know, and didn't articulate better about the merged values (1,2), but then I had better accepted Jeroen array of values for easier breakdowns of possible array of values.
Thanks to everyone who has kindly helped.
This:
function array_combine_custom($arr1, $arr2) {
$out = array();
$arr1 = array_values($arr1);
$arr2 = array_values($arr2);
foreach($arr1 as $key1 => $value1) {
$out[(string)$value1] [] = $arr2[$key1];
}
return $out;
}
Returns:
Array
(
[a] => Array
(
[0] => 1
[1] => 2
)
[b] => Array
(
[0] => 3
)
)
$out = array();
foreach ($arr1 as $v) {
$out[$v] .= (strlen($out[$v]) ? ',' : '').array_shift($arr2);
}
$out now:
Array
(
[a] => 1,2
[b] => 3
)