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
)
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);
}
$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.
I have two arrays with the following values,
First Array:
Array
(
[Strongly Agree] => 100
)
Second Array:
Array
(
[0] => Strongly Agree
[1] => Agree
[2] => Neither Agree or Disagree
[3] => Strongly Disagree
)
I need the output should like this,
Array (
[0] => 100
[1] => 0
[2] => 0
[3] => 0
)
Try like
foreach($array2 as $key => $value) {
$temp = array_key_exists($value, $array2) ? $array1[$value] : 0;
$newArr[$key] = $temp;
}
array key exists won't trigger notices
$sample = array('Strongly Agree' => 100);
$alternatives = array( 'Strongly Agree', 'Agree', 'Neither Agree or Disagree', 'Strongly Disagree');
$output=array();
foreach($alternatives as $alternative) {
$output[$alternative] = array_key_exists($alternative, $sample)? $sample[$alternative]:0;
}
print_r($output);
Try
$arr2 = array_merge(array_fill_keys($arr2, 0), $arr1);
See demo here
Below is the display of my array $arr[0]. Could you please tell me how to take the values of inner array?
Here I need to take only the value for the ID 656 which is 'John'.
Array
(
[0] => xxxxxxxxx
(
[Users] => Array
(
[0] => Array
(
[name] => 656
[value] => John
)
[1] => Array
(
[name] => 657
[value] =>Peter
)
[2] => Array
(
[name] => 658
[value] => Louie
)
[3] => Array
(
[name] => 659
[value] => Jim
)
)
)
Thanks in advance.
try running a:
foreach($arr as $key=>$value){
var_dump($value);
}
And you'll probably be able to work out what to do from there. Hope that helps?
EDIT: if
$arr = array(
0=>array(
'Users'=>array(
0=>array('name'=>656, 'value'=>'John'),
1=>array('name'=>656, 'value'=>'John'),
2=>array('name'=>658, 'value'=>'Louie')
)
)
);
Then you can use:
foreach($arr as $Users){
foreach($Users as $k=>$v){
var_dump($v[0]['value']);
}
}
To get 'John'. Does that help?
If this isn't just a one-off, you could use a recursive array search function. If your data is in $arr, in the format you described:
$arr = array(array("Users"=>array(array("name"=>656,"value"=>"John"),array("name"=>657,"value"=>"Peter"))));
It might look like this:
print in_array_multi("656",$arr);
// ^-- This prints John
print in_array_multi("657",$arr);
// ^-- This prints Peter
function in_array_multi($item, $arr) {
foreach ($arr as $key => $value) {
if ($value==$item){
return $arr['value'];
} else if (is_array($value)){
if($ret = in_array_multi($item, $value))
return $ret;
}
}
return "";
}
for example:
i supose you have 3 arrays to arrive where you wanna ok?
array_1[0].array_2[0].array_3[0]---> here you the information you need.
so you have 2 arrays inside array_1 at position 0.
for(){//to watch the first array all positions
for(){//top watch the second array idem
for(){//to watch the 3 idem...
here at teh position 0 take away the values...
}
}
}
it hope it helps.
Actual array as below is basically the array of $_POST.
One can understand what is coming from the form. three controls with same name different value. What i need is below this array.
Array
(
[mytext] => Array
(
[0] => aaaa
[1] => bbbb
[2] => ddd
)
[mysel] => Array
(
[0] => one
[1] => two
[2] => two
)
[submit] => Submit
)
I need the array in row format below but be dynamic based of $_POST data.
Array
(
[0] => Array
(
[0] => aaaa
[1] => one
)
[1] => Array
(
[0] => bbbb
[1] => two
)
[2] => Array
(
[0] => dddd
[1] => two
)
)
Try this:
$out = Array();
foreach($_POST['mytext'] as $k=>$v) {
$out[$k] = Array($v,$_POST['mysel'][$k]);
}
var_dump($out);
// Code To Get controls value in row wise
$count=0;
foreach($_POST as $key=>$val){
foreach($_POST[$key] as $val2){
$row[$count][]=$val2;
$count++;
}
$count=0;
}
print_r($row);
Loop through one of the fields, and then pull the corresponding value from the other field.
$result = array();
foreach($_POST['mytext'] as $k=>$v){
$result[] = array($v, $_POST['mysel'][$k]);
}
You can also use array_map to do this:
// PHP 5.3+
$result = array_map(function($a, $b){
return array($a, $b);
}, $_POST['mytext'], $_POST['mysel']);
// PHP <= 5.2
$result = array_map(create_function('$a,$b', 'return array($a,$b);'), $_POST['mytext'], $_POST['mysel']);