Merge 3 arrays in to one
Merge 3 arrays in to one
I have a array like following Array i want to merge that array with another 2 arrays. i have tried with array merge function and it failed can you suggest any other way.
(
[0] => stdClass Object
(
[field_name] => mem_goup_name
[table_name] => membership_group
[field_id] => 15
)
[1] => stdClass Object
(
[field_name] => mem_group_description
[table_name] => membership_group
[field_id] => 17
)
)
And I want to set these 2 arrays to that former array
Array
(
[fiter_15] => 5
[fiter_17] => 2
)
Array
(
[operator_15] => 3
[operator_17] => 1
)
which means like this, (the key with the number must equal to the field_id).
[0] => stdClass Object
(
[field_name] => mem_goup_name
[table_name] => membership_group
[field_id] => 15,
[fiter_15] => 5,
[operator_15] => 3
)
[1] => stdClass Object
(
[field_name] => mem_group_description
[table_name] => membership_group
[field_id] => 17,
[fiter_17] => 2,
[operator_17] => 1
)
This is what I have tried so far.
$operator = array();
$filter = array();
$field_ids = array();
foreach ($fitered_values as $key => $value)
if (strpos($key, 'operator_') !== false)
$operator[$key] = $value;
if (strpos($key, 'fiter_') !== false)
$filter[$key] = $value;
if (strpos($key, 'field_ids_') !== false)
$field_ids[$key] = $value;
@RinsadAhmed $operator = array(); $filter = array(); $field_ids = array(); foreach ($fitered_values as $key => $value) if (strpos($key, 'operator_') !== false) $operator[$key] = $value; if (strpos($key, 'fiter_') !== false) $filter[$key] = $value; if (strpos($key, 'field_ids_') !== false) $field_ids[$key] = $value;
– user8911972
Aug 24 at 4:03
Please edit your question with that.
– Rinsad Ahmed
Aug 24 at 4:03
2 Answers
2
You can loop the first array and use preg_grep to find the keys.
I grep the array_keys of array2 and the output is intersected to get the array items.
Then I merge it in to the main array1.
$arr2 = array_merge($filter, $operator);
foreach($arr1 as $key => $item)
$temp = preg_grep("/.*_" . $item['field_id'] . "/", Array_keys($arr2));
$arr1[$key] = array_merge($arr1[$key] , array_intersect_key($arr2, array_flip($temp)));
var_dump($arr1);
https://3v4l.org/3Inbb
Also, why do you have two accounts?
filter array by string and separate in to 2 arrays
Please check below and let me know if it works for you.
$arr1 = array(
0 => (object)array
(
'field_name' => 'mem_goup_name',
'table_name' => 'membership_group',
'field_id' => 15
),
1 => (object)array
(
'field_name' => 'mem_group_description',
'table_name' => 'membership_group',
'field_id' => 17
)
);
$arr2 = array
(
'fiter_15' => 5,
'fiter_17' => 2
);
$arr3 = array
(
'operator_15' => 3,
'operator_17' => 1
);
foreach( $arr1 as $key => $value )
$key1= 'filter_'.$value->field_id;
$key2 = 'operator_'.$value->field_id;
$arr1[$key]->$key1 = $arr2['fiter_'.$value->field_id];
$arr1[$key]->$key2 = $arr3['operator_'.$value->field_id];
print_r($arr1);
Preeti answers on SO should include what the answer does and not only "try this".
– Andreas
Aug 24 at 5:35
This code may give an notice that an array item does not exist if 'field_id' from the first array does not exist in the other two.
– Andreas
Aug 24 at 5:38
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Have you tried any solutions?
– Rinsad Ahmed
Aug 24 at 4:01