How do I remove an array elements from another array? - php

I have this array. How do I remove all those elements which are present in another array i.e. $remove and re-index the final array starting from 1 not 0?
$info = array(
'1' => array('name' => 'abc', 'marks' => '56'),
'2' => array('name' => 'def', 'marks' => '85'),
'3' => array('name' => 'ghi', 'marks' => '99'),
'4' => array('name' => 'jkl', 'marks' => '73'),
'5' => array('name' => 'mno', 'marks' => '59')
);
$remove = array(1,3);
Desired Output:
$info = array(
'1' => array('name' => 'def', 'marks' => '85'),
'2' => array('name' => 'jkl', 'marks' => '73'),
'3' => array('name' => 'mno', 'marks' => '59')
);
So far I've tried these two methods. Nothing worked for me.
if (($key = array_search(remove[0], $info))) {
unset($info[$key]);
$info = array_values($info);
}
And
$result = array_diff($info, $remove);

Something like this will work:
$result = array_diff_key( $info, array_flip( $remove));
This array_flip()s your $remove array so the keys become the values and the values becomes the keys. Then, we do a difference against the keys with array_diff_key() of both arrays, to get this result:
Array
(
[2] => Array
(
[name] => def
[marks] => 85
)
[4] => Array
(
[name] => jkl
[marks] => 73
)
[5] => Array
(
[name] => mno
[marks] => 59
)
)
Finally, to yield your exact output, you can reindex your array by passing it through array_values(), but this will yield sequential indexes starting at zero, not one:
$result = array_values( array_diff_key( $info, array_flip( $remove)));
If you really need indexes to start at one, you will need a combination of array_combine() and range():
$result = array_diff_key( $info, array_flip( $remove));
$result = array_combine( range( 1, count( $result)), $result);

What about using array_diff function?
Example
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
this will output
Array
(
[1] => blue
)

Related

How to merge two multidimensional arrays

I have two array:
$arr1 = array(
'attributes' => array(
'fruit' => 'banana',
),
);
$arr2 = array(
'attributes' => array(
'color' => 'red',
),
);
$result = array_merge($arr1, $arr2);
The result is:
Array ( [attributes] => Array ( [color] => red ) )
But my expected result:
Array ( [attributes] => Array ( [color] => red [fruit] => banana ) )
What I am doing wrong? Should I use array_merge
or maybe will be better and easier just to use array_push and use only ('color' => 'red') ?
array_merge_recursive() is a great fit here.
$resultArray = array_merge_recursive($arr1, $arr2);
try this:
$result = array('attributes' => array_merge($arr1['attributes'], $arr2['attributes']));
print_r($result);

Merge two arrays, replacing values of same key

I have two arrays
First Array
(
[0] => Array
(
[352] => 1
[128] =>
[64] =>
[70] => 2
)
)
Second array is like this :
Array
(
[0] => Array
(
[128] => 1.5
)
)
I want to make final array like this.(i want to store the matching into the main array in this example it is 128 -> 1.5) how can i do it.?
Array
(
[0] => Array
(
[352] => 1
[128] => 1.5
[64] =>
[70] => 2
)
)
here is my array variables:
print_r($listskilssresult);
print_r($listskilssresultmatching);
You need to use array_replace_recursive
$listskilssresult = [
[
352 => 1,
128 => '',
64 => '',
70 => 2
]
];
$listskilssresultmatching = [
[
128 => 1.5
]
];
print_r(array_replace_recursive($listskilssresult, $listskilssresultmatching));
Prints :
Array
(
[0] => Array
(
[352] => 1
[128] => 1.5
[64] =>
[70] => 2
)
)
Know the difference between array_replace_recursive and array_merge_recursive here
This is specific to your question. If you want to make something more automated, you can create a function. But this will do what you want:
<?php
$array1 = [[352 => 1, 128 => null, 64 => null, 70 => 2]];
$array2 = [[128 => 1.5]];
$keys1 = array_keys($array1[0]);
$keys2 = array_keys($array2[0]);
foreach ($keys1 as $key => $value) {
if (in_array($value, $keys2)) {
$array1[0][$value] = $array2[0][$value];
unset($array2[0][$value]);
}
}
if (!empty($array2[0])) {
foreach ($array2[0] as $key => $value) {
$array1[0][$key] = $value;
unset($array2[0][$key]);
}
}
print_r($array1[0]);
?>
The last if statement will add key + value from the 2nd array to the first if no match was found for them (from the foreach statement). You can just delete that condition if you just want to add only matching keys.
For this solution, you have to use array_merge() built-in php function.
Syntax:
$finalArray = array_merge($array1, $array2);
print_r($finalArray)
Example:
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
Output:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
Reference : http://php.net/manual/en/function.array-merge.php
array_replace_recursive is the best solution!
$old_array = Array('my_index1' => Array('1' => 'A', '2' => 'B'), 'my_index2' => Array('1' => 'C', '2' => 'D'));
$new_array = Array('my_index2' => Array('2' => 'Z'));
$result = array_replace_recursive($old_array, $new_array);
//Result : Array('my_index1' => Array('1' => 'A', '2' => 'B'), 'my_index2' => Array('1' => 'C', '2' => 'Z'));
array_merge would do the job for you:
array_merge($listskilssresult,$listskilssresultmatching)

How to merge multiple two dimensional array based on first dimension key?

My problem statement is like follows:
Suppose I have 2 two dimensional array. The arrays are:
$array1 = Array
(
[8] => Array
(
[branch_code] => DG-52484
[total_desg] => 11
)
);
$array2 = Array
(
[8] => Array
(
[total_dak] => 0
[total_dak_decision] => 0
)
);
After combining the two array my required out put will be:
Array
(
[8] => Array
(
[branch_code] => DG-52484
[total_desg] => 11
[total_dak] => 0
[total_dak_decision] => 0
)
);
Is there any php function for this type of task. Please note that i am not interested to use foreach or while in my situation.
Thanks in advance.
It will work with array_replace_recursive:
$array1 = Array(
8 => Array(
'branch_code' => 'DG-52484',
'total_desg' => '11',
)
);
$array2 = Array
(
8 => Array(
'total_dak' => 0,
'total_dak_decision' => 0,
)
);
var_dump(array_replace_recursive($array1, $array2));
Output
array (size=1)
8 =>
array (size=4)
'branch_code' => string 'DG-52484' (length=8)
'total_desg' => string '11' (length=2)
'total_dak' => int 0
'total_dak_decision' => int 0
try to use
$array = array(
8 => array_merge($array1[8],$array2[8]);
);
You can use array_merge
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
Output
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
For more info http://php.net/manual/tr/function.array-merge.php

Include an array in another array

Was wondering how to add the values of one array into another to save me typing the values of one array over and over:
$array_main = array(
'[1]' => '1',
'[2]' => '2',
'[3]' => '3',
'[4]' => '4'
);
$array_1 = array( $array_main, '[5]' => '5' );
This deduces:
$array_1 = array(
array(
'[1]' => '1',
'[2]' => '2',
'[3]' => '3',
'[4]' => '4'
),
'[5]' => '5'
);
But I wanted:
$array_1 = array(
'[1]' => '1',
'[2]' => '2',
'[3]' => '3',
'[4]' => '4',
'[5]' => '5'
);
So is there anything that can turn an array into a string? I've tried implode and array_shift but I need the whole array() not just the values..
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
The above example will output:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
http://php.net/manual/en/function.array-merge.php
Fastest way is simply use single array like following,
$array_main = array(
'[1]' => '1',
'[2]' => '2',
'[3]' => '3',
'[4]' => '4'
);
$array1 = $array_main;
$array1['[5]'] = '5';
Though if specific requirement for new array use array_merge,
$array1 = array_merge($array_main,array('[5]' => '5'));
You can use merge array (don't just want to add an extra value) to merge two arrays:
<?php
$array1 = array("0" => "0", "1" => "1");
$array2 = array("a" => "a", "b" => "b");
print_r( array_merge($array1, $array2 );
?>
Prints:
Array
(
[0] => 0
[1] => 1
[a] => a
[b] => b
)
Use array_merge()
$array_1 = array_merge($array_main, array('[5]' => '5'));

Array within another array

I have an array like:
Array ( [id] => 1 [code] => FAC876 )
How do I push it into another array using PHP, such that the result is like:
Array ( [0] => Array ( [id] => 1 [code] => FAC876 )
[1] => Array ( [id] => 2 [code] => GEO980 )
)
Simply threat the array as any other variable.
So if this is what you got:
$array = array();
$array1 = array( "id"=>1, "code"=>"FAC876" );
$array2 = array( "id"=>2, "code"=>"GEO980" );
You could do either
$array[] = $array1;
$array[] = $array2;
or
$array[0] = $array1;
$array[1] = $array2;
or
$array = array($array1, $array2);
or
array_push($array, $array1);
array_push($array, $array2);
Any of those four possibilites will give you what you want.
You almost posted the answer yourself:
Array (
0 => Array ( 'id' => 1, 'code' => 'FAC876' ),
1 => Array ( 'id' => 2, 'code' => 'GEO980' )
)
$arr1 = array(
'id' => 1,
'code' => 'FAC876',
);
$arr2 = array(
$arr1,
array(
'id' => 2,
'code' => 'GEO980',
),
);
or
$arr1 = array(
'id' => 1,
'code' => 'FAC876',
);
$arr2 = array(
'id' => 2,
'code' => 'GEO980',
);
$arr3 = array($arr1, $arr2);
or lots of other ways to achieve that.
$ar=array();
$ar[]=array("no"=>10,"name"=>"abc");
$ar[]=array("no"=>20,"name"=>"pqr");
$arrays = array();
$array1 = array("id" => 1, "code" => "ABC");
$array2 = array("id" => 2, "code" => "DEF");
array_push($arrays, $array1, $array2);
$array = array(array( "id"=>1, "code"=>"FAC876" ) , array( "id"=>2, "code"=>"GEO980" ));
or
$array = array();
$array[] = array( "id"=>1, "code"=>"FAC876" );
$array[] = array( "id"=>2, "code"=>"GEO980" );
or
$array = array();
array_push($array, array( "id"=>1, "code"=>"FAC876" ));
array_push($array, array( "id"=>2, "code"=>"GEO980" ));

Categories