I have 2 arrays:
$array1 = array(1 => "aaa", 4 => "bbb", 5 => "ccc", 8 => "ddd", 9 => "eee", 11 => "fff");
$array2 = array(2 => "", 3 => "", 6 => "", 7 => "", 9 => "", 13 => "");
I want to change the keys of $array1 according to $array2. I'm given the information that an element of the second array has to correspond to another of the first one. For example I know that $array2[6] has to correspond to $array1[4].
So I should have all the keys of $array1 changed according to this rule:
$array1 = array(3 => "aaa", 6 => "bbb", 7 => "ccc", 9 => "ddd", 13 => "eee", 2 => "fff");
I don't know how to solve this. I've tried to split the first array where the element given is but I'm stuck.
You can define a function that maps and constructs a new array.
function transfer_keys($key_array,
$value_array) {
$a = array_map(null, array_keys($key_array),
$value_array);
$result = array();
foreach($a as $kv) {
$result[$kv[0]] = $kv[1];
}
return $result;
}
$array1 = array(1 => "aaa", 4 => "bbb", 5 => "ccc",
8 => "ddd", 9 => "eee", 11 => "fff");
$array2 = array(2 => "", 3 => "", 6 => "", 7 => "",
9 => "", 13 => "");
print_r(transfer_keys($array2, $array1));
if you wanna update all elements of your first array with the values in the array2 in the same order :
$i = 0;
foreach ($array1 as $v) {
while (!isset($array2[$v]))
$i++;
$array2[$i] = $v;
}
But if you want to update according to an order you pre-defined, you have to make something else, like a table defining the rule :
$assoc_array = array(
2 => 4,
3 => 7,
4 => 11,
6 => 5,
5 => 9,
8 => 1);
foreach ($assoc_array as $k => $v) {
$array1[$k] = $array2[$v];
}
Hope this helps.
Aw, you know the difference is always the same !
Then this should be something like :
function updateArray ($array1, $array2, $key_of_array1, $key_associated_of_array2) {
$diff = $key_associated_of_array2 - $key_of_array1;
foreach ($array1 as $k => $v) {
$array2[$k + $diff] = $array1[$k];
}
}
you can get first keys of both arrays using function array_keys();
suppose, $keys1 contains keys of $array1 and $keys2 cotains keys of $array2
Then move into for loop as follows:
for($i=0 ; $i<count($array1) ; $i++)
{
$result[$keys2[$i+1]] = $array1[$i];
}
print_r($result);
Hope this will be helpful to you
foreach($array1 as $item => $value)
if(isset($array2[($item + 2)]) && item != 11)
$temp[$item + 2] = $value;
elseif($item == 11)
$temp[2] = $value;
$array1 = #$temp;
Example code.. but again, you need to tell us the pattern that determines where the first array elements are being inserted into the second array elements. I think it's.. +2? Maybe?
Related
I have an array where I'm looping on an object
foreach ($product->info->details as $details) {
$skuItem[] = $details->dtl1;
$skuItem[] = $details->dtl2;
$skuItem[] = $details->dtl3;
}
The object it loops on is structured this way
"details": {
"1": {
"dtl1": "123",
"dtl2": "TEst",
"dtl3": "123"
},
"2": {
"dtl1": "12",
"dtl2": "Test",
"dtl3": "153"
}
},
The thing is, it can only have up to 2 of those sets but sometimes it has only one.
Is there a way to accomodate in my foreach loop so that if there is only one then I can basically 'dummy up' a second set with all zeroes? I'm mapping this to a file and need to make sure I'm at least always mapping all 6 values
So if the object looks like
"details": {
"1": {
"dtl1": "123",
"dtl2": "TEst",
"dtl3": "123"
}
I would want to create my array like
0 => "123",
1 => "TEst",
2 => "123"
3 => "0",
4 => "0",
5 => "0"
After the foreach, you can pad your array with zero:
foreach ($product->info->details as $details) {
$skuItem[] = $details->dtl1;
$skuItem[] = $details->dtl2;
$skuItem[] = $details->dtl3;
}
Array now contains:
0 => "123"
1 => "TEst"
2 => "123"
Now run:
$skuItem = array_pad($skuItem, 6, 0);
This will add zeros to the end of the array until you get 6 items in it, so the array now contains:
0 => '123'
1 => 'TEst'
2 => '123'
3 => 0
4 => 0
5 => 0
If you want string zero instead, then just pass that as the 3rd arg:
$skuItem = array_pad($skuItem, 6, '0');
Yields:
0 => '123'
1 => 'TEst'
2 => '123'
3 => '0'
4 => '0'
5 => '0'
You can create a template of what you want and replace with what you create in the loop:
$skuItem = array_replace(array_fill(0, 6, 0), $skuItem);
array_pad is probably better for this trivial example, but consider if you have a variety of values:
$temp = array('x', 'y', 'z', 'x', 'y', 'z');
$skuItem = array_replace($temp, $skuItem);
Or:
$temp = array('x', 'y', 'z');
if(count($skuItem) != 6) {
$skuItem = array_merge($skuItem, $temp);
}
I have an array.
$a = array(
0 => 1,
1 => 1,
2 => 2,
3 => 3,
4 => 1
);
How to get unique array like this?
$result = array_My_unique($a);
print_r($result);
Output:
$a = array(
0 => 1,
1 => 2,
2 => 3,
3 => 1
);
Thank!
Assuming you are trying to avoid duplicates that are immediately next to each other:
function array_my_unique($a = array()) {
$out = array();
$curr = false;
foreach ($a as $v) {
if ($curr !== $v) {
$out[] = $v;
}
$curr = $v;
}
return $out;
}
This satisfies the assertion between input/output that you described in the question.
I have 2 PHP arrays, a simple one:
array
0 => int 5
1 => int 6
and an array of objects:
array
0 =>
object(stdClass)[43]
public 'id' => int 1
1 =>
object(stdClass)[46]
public 'id' => int 3
2 =>
object(stdClass)[43]
public 'id' => int 5
3 =>
object(stdClass)[46]
public 'id' => int 6
4 =>
object(stdClass)[46]
public 'id' => int 7
I'd like to make a diff of these 2 arrays to eliminate in the second those present in the first. In this example, i don't want the ids 5 and 6 in the second array. But i need help ;>
Thank you.
fabien
Assuming $objects is your array of objects, and $values is your array of values to remove...
You could use a foreach loop if you want to return objects:
$values = array(5, 6);
$objects = array(
(object) array("id" => 1),
(object) array("id" => 3),
(object) array("id" => 5),
(object) array("id" => 6),
(object) array("id" => 7)
);
foreach($objects as $key => $object) {
if(in_array($object->id,$values)) {
unset($objects[$key]);
}
}
Live demo (0.008 sec)
If you want to use the diff function itself (that's possible but awkward, less readable and will just return an array of values) you can (as Baba suggested) return the id of the object inline:
$values = array(5, 6);
$objects = array(
(object) array("id" => 1),
(object) array("id" => 3),
(object) array("id" => 5),
(object) array("id" => 6),
(object) array("id" => 7)
);
$diff = array_diff(array_map(function ($object) {
return $object->id;
}, $objects), $values);
Live demo (0.008 sec)
You can try :
$diff = array_diff(array_map(function ($v) {
return $v->id;
}, $array2), $array1);
See Live DEMO
Loop over the second array and use in_array method to check for existing values in first
$firstArray = array(5, 6);
foreach ($objects as $key => $object) {
if (in_array($object->id, $firstArray)) {
unset($objects[$key];
}
}
For versions older than 5.3
foreach( $arr_2nd as $key => $val )
{
$arr_2nd[$key] = $val->id;
}
array_diff( $arr_1st, $arr_2nd );
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove integers in array less than X with PHP
$array = array(
"a" => 10,
"b" => 9,
"c" => 8,
"d" => 7,
"e" => 6,
"f" => 5,
"g" => 4,
"h" => 3
);
How can I dele the item if the value is smaller than 6? i.e., how can I get the following array?
$array = array(
"a" => 10,
"b" => 9,
"c" => 8,
"d" => 7,
"e" => 6
);
array_filter is perfect for this:
$new = array_filter($old,function($a) {return $a >= 6;});
A simple iteration would be:
$new_array = array();
foreach($arras as $key => $value){
if($value >= 6)new_array[$key] = $value;
}
foreach($array as $index=>$value) {
if ( $value < 6) {
unset($array[$index]);
}
}
or
foreach($array as $index=>$arr_value) {
if ( $value >= 6) {
$new_array[$index] = $arr_value;
}
}
I want to combine two arrays like this:
1st array:
array( "ATTENDED" => 1,
"TENTATIVE" => 2, //
"REJECTED" => 3,
"OUTSTANDING" => 4,
"ACCEPTED" => 6
);
2nd Array:
array ( 1 => 29,
4 => 30,
6 => 47
);
I want to get the results like this:
array ( 'ATTENDED' => 29,
'OUTSTANDING' => 30,
'ACCEPTED' => 47
);
2nd array is flexible. I can flip keys and values.
or better yet:
array( "ATTENDED" => 29,
"TENTATIVE" => 0, //
"REJECTED" => 0,
"OUTSTANDING" => 30,
"ACCEPTED" => 47
);
I know there must be a simple solution.
Any ideas?
foreach ($arr1 as $k1 => $v1) {
$arr1[$k1] = isset($arr2[$v1]) ? $arr2[$v1] : 0;
}
edit-
This is without an explicit loop, although I don't think this is really better, but maybe cooler though.
$mapped = array_map(function($valFromArr1) use ($arr2) {
return isset($arr2[$valFromArr1]) ? $arr2[$valFromArr1] : 0;
}, $arr1);
I can't think of a sane way to just use pure php functions.
$labels = array(
"ATTENDED" => 1,
"TENTATIVE" => 2,
"REJECTED" => 3,
"OUTSTANDING" => 4,
"ACCEPTED" => 6
);
$values = array(
1 => 29,
4 => 30,
6 => 47
);
$results = array();
foreach ($labels as $label => $id) {
$results[$label] = array_key_exists($id, $values) ? $values[$id] : 0;
}