Array 1
Array
(
[0] => Array
(
[prid] => 110
[size_id] => 24
)
[1] => Array
(
[prid] => 117
[size_id] => 24
)
[2] => Array
(
[prid] => 174
[size_id] => 24
)
)
Array 2
Array
(
[0] => Array
(
[prid] => 174
[size_id] => 24
)
[1] => Array
(
[prid] => 174
[size_id] => 25
)
[2] => Array
(
[prid] => 163
[size_id] => 24
)
)
I have 2 multidimensional arrays. I want to compare both arrays and resultant arrays are equal in both.
(i.e) The answer is
[prid] => 174
[size_id] => 24
Note:- i am using array_intersect_assoc(),but not working
How can we retrieve this???
Please tell the answer to this
You can use foreach() and array_column() to compare second array value with first array
$final_array = [];
foreach($array2 as $arr){
if(in_array($arr['prid'],array_column($array1,'prid')) && in_array($arr['size_id'],array_column($array1,'size_id'))){
$final_array[$arr['prid']] = $arr;
}
}
$final_array = array_values($final_array);
print_r($final_array);
Output:- https://eval.in/979112
Another easier solution using array_intersect() and array_column()
$final_array = [];
$common_array = array_intersect (array_column($array1,'prid'),array_column($array2,'prid'));
foreach($common_array as $key=>$val){
$final_array[] = $array1[$key];
}
print_r($final_array);
Output:-https://eval.in/979135
$new_array = array();
foreach($array1 as $value1){
foreach($array2 as $value2){
if($value1['prid'] == $value2['prid'] && $value1['size_id'] == $value2['size_id']){
$new_array[] = $value1;
}
}
}
echo print_r($new_array);
You can get this done by using couple of array function and a foreach loop.
Try like this:-
$array1=[
['prid'=>110,'size_id'=>24],
['prid'=>117,'size_id'=>24],
['prid'=>174,'size_id'=>24],
];
$array2=[
['prid'=>174,'size_id'=>24],
['prid'=>174,'size_id'=>25],
['prid'=>163,'size_id'=>24]
];
$prid = array_intersect(array_column($array1,'prid'),array_column($array2,'prid'));
$size_id = array_intersect(array_column($array1,'size_id'),array_column($array2,'size_id'));
$array3 = array_merge($array1,$array2);
$result=[];
foreach ($array3 as $data){
if(in_array($data['prid'],$prid) && in_array($data['size_id'],$size_id)){
$result = $data;
break;
}
}
or like this
$result=[];
foreach ($array1 as $data1){
foreach ($array2 as $data2){
if($data1['prid']==$data2['prid'] && $data1['size_id']==$data2['size_id']){
$result = $data1;
break;
}
}
}
It will give o/p like this
Related
I have a simple Two array
$ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);
$ages1[] = array("demo"=>22);
When I print this arrays it should be like following:
Array
(
[0] => Array
(
[Peter] => 22
[Clark] => 32
[John] => 28
)
)
Array
(
[0] => Array
(
[demo] => 22
)
)
But I want to create third array which will be show demo kye value into first array like following:
Array
(
[0] => Array
(
[Peter] => 22
[Clark] => 32
[John] => 28
[demo] => 22
)
)
Can we do two array into single array in PHP like Above
Not sure what are you trying to achieve here...little more context would be helpful. But this is how you can do this,
$ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);
$ages1[] = array("demo"=>22);
$result[] = array_merge($ages[0],$ages1[0]);
This would do the job.
<?php
$ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);
$ages1[] = array("demo"=>22);
$output = prepend_array($ages,$ages1);
print_r($output);
// Function to prepend arrays
function prepend_array()
{
$num_args = count(func_get_args());
$new_array = array();
foreach (func_get_args() as $params){
foreach($params as $out_key => $param)
{
foreach($param as $key => $value)
$new_array[$out_key][$key] = $value;
}
}
return $new_array;
}
I have two arrays and I want to replace the second array keys with the first array values if both keys matches.
As an example: Replace A with Code And B with name
How to do this;
<?php
$array = array('A' => 'code', 'B' =>'name');
$replacement_keys = array
(
array("A"=>'sara','B'=>2020),
array("A"=>'ahmed','B'=>1010)
);
foreach($replacement_keys as $key => $value){
foreach($value as $sk => $sv){
foreach($array as $rk => $rv){
if($sk == $rk ){
$sk = $rv;
}
}
}
}
echo "<pre>";
print_r($value);
echo "</pre>";
exit;
I want the result to be like this
array(
[0] => Array
(
[name] => ahmed
[code] => 1020
)
[1] => Array
(
[name] => sara
[code] => 2020
)
)
<?php
$array = array('A' => 'code', 'B' =>'name');
$replacement_keys = array
(
array("A"=>'sara','B'=>2020),
array("A"=>'ahmed','B'=>1010)
);
foreach($replacement_keys as &$value)
{
foreach ($array as $key => $name) {
$value[$name] = $value[$key];
unset($value[$key]);
}
}
var_dump($replacement_keys);
Try this:
<?php
$array = array('A' => 'code', 'B' =>'name');
$replacement_keys = array
(
array("A"=>'sara','B'=>2020),
array("A"=>'ahmed','B'=>1010)
);
$newArray = array();
foreach($replacement_keys as $key => $value)
{
foreach($value as $key2 => $value2)
{
if(isset($array[$key2]))
{
$newArray[$key][$array[$key2]] = $value2;
}
else
{
$newArray[$key][$key2] = $value2;
}
}
}
print_R($newArray);
This should work for you, nice and simple (I'm going to assume that A should be name and B should be code):
(Here I go through each array from $replacement_keys with array_map() and replace the array_keys() with the array_values() of $array. Then I simply get all array values from $replacement_keys and finally I array_combine() the replaced array keys with the corresponding array values)
$result = array_map("array_combine",
array_map(function($v)use($array){
return str_replace(array_keys($array), array_values($array), array_keys($v));
}, $replacement_keys),
$replacement_keys
);
output:
Array ( [0] => Array ( [code] => sara [name] => 2020 ) [1] => Array ( [code] => ahmed [name] => 1010 ) )
array_fill_keys
(PHP 5 >= 5.2.0, PHP 7)
array_fill_keys — Fill an array with values, specifying keys
Description
array array_fill_keys ( array $keys , mixed $value )
Fills an array with the value of the value parameter, using the values of the keys array as keys.
http://php.net/manual/en/function.array-fill-keys.php
When print_r($my_array);
Array
(
[0] => Array
(
[value] => num1
[label] =>
)
[1] => Array
(
[value] => num2
[label] =>
)
[2] => Array
(
[value] => num3
[label] =>
)
)
Now I wonder how to create a new array variable to hold only 1 level with the following structure. e.g:
if print_r($new_array), it will show:
Array
(
[0] => num1
[1] => num2
[2] => num3
)
Try foreach() and store value in new array
foreach($arr as $v) {
$newarr[] = $v['value'];
}
print_r($newarr);
$new = array_map(function($element){ return $element['value'] ; }, $array);
Try with foreach of $my_array and assign value to $new_array variable like
$new_array = array();
foreach($my_array as $array){
$new_array[] = $array['value'];
}
print_r($new_array);
Loop thru the array as follows and push the desired elements into a new array.
$new_array = array();
for($i=0;$i<count($my_array);$i++) {
array_push($new_array, $my_array[$i]['value']);
}
print_r($new_array);
below can work:
foreach($my_array as $k => $v){
$new_array[] = $v['value'];
}
print_r($new_array);
$new_array = array();
foreach ($my_array as $key => $value) {
$new_array[] = $value['value'];
}
print_r($new_array);
I am trying to "subtract" the values of an array in php. I used array_diff but it doesn't seem to work for more than one value.
<?php
$array1 = array(1,3,7,10,7);
$array2 = array(1,7);
$result=array_diff($array1,$array2);
print_r($result);
?>
//Output//
Array ( [1] => 3 [3] => 10 )
What I would like to do is return 3,7,10 instead of excluding all 7's. Thanks in advance!
Try:
$array1 = array(1,3,7,10,7);
$removals = Array(1,7);
foreach( $removals as $remove ) {
foreach( $array1 as $key => $value ) {
if ($value === $remove ) {
unset($array1[ $key ]);
break;
}
}
}
print_r($array1); // Array ( [1] => 3 [3] => 10 [4] => 7 )
sort($array1)
print_r($array1); // Array ( [0] => 3 [1] => 7 [2] => 10 )
based on thelastshadows post but shorter and may faster because only one foreach
$array1 = array(1,3,7,10,7);
$removals = Array(1,7);
foreach( $removals as $remove ) {
unset($array1[array_search($remove,$array1)]);
}
sort($array1);
print_r($array1);
I was wondering if anyone could help me restructure a predefined php array. The output of my current array is:
Array
(
[71-ctns] => 1
[71-units] => 1
[308-units] => 1
[305-ctns] => 1
[306-units] => 2
)
And I would like it to look like:
Array
(
[71] => Array
(
[ctns] => 1
[units] => 1
)
[308] => Array
(
[units] => 1
)
[305] => Array
(
[ctns] => 1
)
[306] => Array
(
[units] => 2
)
)
Is this possible?
This should do it
$merged = array();
foreach($a as $k=>$v){
$t = explode('-',$k);
$id = intval($t[0]);
if(!array_key_exists($id, $merged))
$merged[$id] = array();
$merged[$id][$t[1]] = $v;
}
EDIT:
Sorry you should use explode instead of split.
Yes, but you need to loop (note: array_map can also work, but this example is more explicit):
$fin = array();
foreach( $complex as $item => $val )
{
$pieces = explode('-', $item);
$fin[$pieces[0]] = isset($fin[$pieces[0]])?:array();
$fin[$pieces[0]][$pieces[1]] = $val;
}
Find below code to restructure a predefined php array
<?php
$newArray=array();
$result = array("71-ctns"=>1,"71-units"=>1,"308-ctns"=>1,"308-units"=>1,"305-units"=>1,"306-units"=>2);
if(is_array($result) && count($result)>0) {
foreach($result as $key=>$val) {
$getKeyArray = explode("-",$key);
$newArray[$getKeyArray[0]][$getKeyArray[1]] =$val;
}
}
print"<pre>";
print_r($newArray);
?>