Repopulate a newer array with values from an older array- php - php

Hello Stack Overflow community,
I am trying to update values from array1 with a newly ordered array2 and output the final array as array2. I have been tying to figure this out for days now but cannot seem to get it. I am new to php as you can most likely tell. Can anyone help me?
Here are the two arrays- 'array1' is first and 'array2' follows:
Array (
[0] => Array
(
[id] => bbb
[hammer] => $1,000
)
[1] => Array
(
[id] => ccc
[hammer] => $678
)
[2] => Array
(
[id] => aaa
[hammer] => $222
)
) Array (
[0] => Array
(
[id] => aaa
[hammer] => GBP 135
)
[1] => Array
(
[id] => bbb
[hammer] => GBP 610
)
[2] => Array
(
[id] => ccc
[hammer] => GBP 413
)
)
Now my code tries to update the newly reordered 'id' values in the second array with the 'hammer' values from the first array. Here is my code:
foreach($array2 as $key => $val) {
$a = $array2[$key]['id'];
$hammer_a = $array2[$key]['hammer'];
foreach($array1 as $key => $val) {
$b = $array1[$key]['id'];
if($a===$b){
$hammer_b = $array1[$key]['hammer'];
$array2[$key]['hammer'] = $hammer_b;
}
}
}
However as this code stands I am left with an undesired result below. I would like 'id' 'aaa' to have its original 'hammer' value of $222 and so on for the other 'id's:
Array
(
[0] => Array
(
[id] => aaa
[hammer] => $1,000
)
[1] => Array
(
[id] => bbb
[hammer] => $678
)
[2] => Array
(
[id] => ccc
[hammer] => $222
)
)
Can anyone please tell me what I am doing wrong?

Change the name of $key and $val in the second loop. They are overriding each other.

Don't use $key in your inner foreach loop. You want the key for array2.

There is also this option, with a lot less code:
$items[0] = array('id'=>'bbb','hammer'=>'$1,000');
$items[1] = array('id'=>'ccc','hammer'=>'$678');
$items[2] = array('id'=>'aaa','hammer'=>'$222');
function cmp($a, $b) {
return strcmp($a['id'], $b['id']);
}
echo '<pre>';
var_dump($items);
usort($items, "cmp");
var_dump($items);
echo '</pre>';

Related

php two Multidimensional Array difference

I am working with two multidimensional array difference bellow are my array:
Array1:
Array
(
[0] => Array
(
[F_CONTACT_ID] => 2
[F_CONTACT_FNAME] => name2
[F_CONTACT_NAME] => name22
)
[1] => Array
(
[F_CONTACT_ID] => 3
[F_CONTACT_FNAME] => name3
[F_CONTACT_NAME] => name33
)
)
Array2:
Array
(
[0] => Array
(
[F_CONTACT_ID] => 2
[F_CONTACT_FNAME] => name2
[F_CONTACT_NAME] => name22
)
[1] => Array
(
[F_CONTACT_ID] => 3
[F_CONTACT_FNAME] => name3
[F_CONTACT_NAME] => name33
)
[2] => Array
(
[F_CONTACT_ID] => 5
[F_CONTACT_FNAME] => name5
[F_CONTACT_NAME] => name55
)
)
I just want o compare the difference with 'F_CONTACT_ID' in the array.
My Resulting Array Should be:
Result:
Array
(
[2] => Array
(
[F_CONTACT_ID] => 5
[F_CONTACT_FNAME] => name5
[F_CONTACT_NAME] => name55
)
)
Also If one array is empty: suppose Array2 is empty. My result Array should be:
Array
(
[0] => Array
(
[F_CONTACT_ID] => 2
[F_CONTACT_FNAME] => name2
[F_CONTACT_NAME] => name22
)
[1] => Array
(
[F_CONTACT_ID] => 3
[F_CONTACT_FNAME] => name3
[F_CONTACT_NAME] => name33
)
)
I tried with different solutions but nothing worked for me. I tried to retrieve the F_CONTACT_ID and stored in single-dimensional array and compare but It took lot of time.
Kindly help me in better and fast solution.
Have you tried this?
for($i=0;$i<count($array1);$i++) {
$temp[$array1[$i]['F_CONTACT_ID']] = $array1[$i];
};
for($i=0;$i<count($array2);$i++) {
if($temp[$array2[$i]['F_CONTACT_ID']]) {
unset($temp[$array2[$i]['F_CONTACT_ID']]);
} else {
$temp[$array2[$i]['F_CONTACT_ID']] = $array2[$i];
}
}
echo "<pre>";
print_r($temp);
echo "</pre>";
The result will be some thing like this:
Array
(
[5] => Array
(
[F_CONTACT_ID] => 5
[F_CONTACT_FNAME] => name5
[F_CONTACT_NAME] => name55
)
)
You could try the function array-diff-key() function, which helps you to compare two multidimensional arrays using keys.
You could visit this page for more information: http://php.net/manual/en/function.array-diff-key.php
what about something like this?
$array1;
$array2;
$array3;
foreach ($array1 as $ar1) {
foreach ($array2 as $ar2) {
if ($ar1['F_CONTACT_ID']==$ar2['F_CONTACT_ID']) {
array_push($array3, $ar1);
}
}
}
it's not very optimize, to maximize performance change the second foreach checking if array_push is already append (using a bool condition)

Intersection of two arrays using common key value for comparison

I want to perform an intersection of two arrays that have different structures, but both have one key common (fid). I want a new (filtered second) array after intersection with first array. below is my code and two arrays :
first array:
Array
(
[0] => Array
(
[fid] => 1
)
[1] => Array
(
[fid] => 3
)
)
Second array:
Array
(
[0] => Array
(
[fid] => 9
[functionality] => testing
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[2] => Array
(
[fid] => 2
[functionality] => view functionality category
[funcat_id] => 1
[name] => functionality
)
[3] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
[4] => Array
(
[fid] => 4
[functionality] => edit functionality
[funcat_id] => 1
[name] => functionality
)
)
I want this Output :
Array
(
[0] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
)
I tried this code but I'm not getting the right answer:
$result=array_intersect($array1,$array2);
//Or this also
$result=recursive_array_intersect_key($array1,$array2);
Please let me know, if any one can do this ?
I do not know if a function does exists to do this outright, but alternatively, you can just loop them instead:
$result = array();
foreach($array2 as $val2) {
foreach ($array1 as $val1) {
if($val2['fid'] == $val1['fid']) {
$result[] = $val2;
}
}
}
echo '<pre>';
print_r($result);
Sample Output
Or if you're using PHP 5.5 or greater:
$val1 = array_column($array1, 'fid');
$result = array_filter($array2, function($val2) use($val1) {
return in_array($val2['fid'], $val1);
});
foreach($array2 as $val)
{
$i=0;
foreach($array1 as $val1)
{
if($val['fid']==$val1['fid'])
{
$i++;
}
}
if($i!=0)
{
$a[]=$val;
}
}
print_r($a);

Padding a multidimensional array with array_pad?

$summary=$query->result_array(); //where the original array is created
print_r($summary); //dump contents
Produces this:
Array ( [0] => Array ( [RecordID] => 2 [UserID] => 3 [BookID] => 1 [Title] => FirstBook ) [1] => Array ( [RecordID] => 3 [UserID] => 3 [BookID] => 2 [Title] => Sequel ) )
I would now like to pad the multi dimensional array with a price element so as to create the results of
Array ( [0] => Array ( [RecordID] => 2 [UserID] => 3 [BookID] => 1 [Title] => FirstBook [Price] => 99 ) [1] => Array ( [RecordID] => 3 [UserID] => 3 [BookID] => 2 [Title] => Sequel [Price] => 99) )
The only way I can think of doing this is to break the multidimensional array into one-dimensional arrays, modify them, and then re-assemble them. Doesn't sound terribly efficient though. Any suggestions?
You can update the internal arrays by reference, note the & here:
foreach($summary as &$details){
$details['Price'] = $price; // wherever $price comes from...
}
try to use:
foreach ($summary as $idx => &$arrValue)
$arrValue['Price'] = ###;
If you're fixed on your dimensions, iterate with a reference and modify $summary in place...
<?php
foreach ($summary as &item) {
$item['price'] = 99;
}
If you have particular objections/issues with references:
<?php
foreach ($summary as $key=>item) {
$summary[$key]['price'] = 99;
}

php unique multidimensional array by keeping entry with the highes value from one dimension?

I have another array unique question in the endless list of questions about them.
I can imagine this problem is quite simple to solve but I simply do not come on it.
Just because there are so many questions on this subject i wasn't able to find anything useful in this case.
the array:
Array
(
[0] => Array
(
[0] => blabla values
[1] => 91.181818181818
)
[1] => Array
(
[0] => blabla same values
[1] => 95.333333333333
)
[2] => Array
(
[0] => blabla other values
[1] => 86
)
[3] => Array
(
[0] => blabla other values
[1] => 92.5
)
[4] => Array
(
[0] => blabla same values
[1] => 88.5
)
)
I want to unique the array by the first array dimension and only keep the entry with the highest value from the second.
Maybe in MYSQL this would be no big deal but at the moment i am not able to implement something like that in php.
desired output array would be:
Array
(
[0] => Array
(
[0] => blabla values
[1] => 91.181818181818
)
[1] => Array
(
[0] => blabla same values
[1] => 95.333333333333
)
[2] => Array
(
[0] => blabla other values
[1] => 92.5
)
)
Has anyone a clever idea?
<?php
$list = array(
array('blabla values',91.181818181818),
array('blabla same values', 95.333333333333),
array('blabla other values', 86),
array('blabla other values', 92),
array('blabla same values', 88.5),
);
$result = array();
foreach ($list as $item)
{
$key = $item[0];
$value = $item[1];
if (!isset($result[$key]) || $result[$key][1] < $value)
{
$result[$key] = $item;
}
}
$result = array_values($result);
print_r($result);
the output:
Array
(
[0] => Array
(
[0] => blabla values
[1] => 91.1818181818
)
[1] => Array
(
[0] => blabla same values
[1] => 95.3333333333
)
[2] => Array
(
[0] => blabla other values
[1] => 92
)
)
usort($arr, function ($a, $b){
return $a[1] - $b[1];
});
$out = array();
foreach ($arr as $key => $value){
$out[$value[0]] = $value[1];
}
$arr = array_map(NULL, array_keys($out), $out);
Output:
Array
(
[0] => Array
(
[0] => blabla same values
[1] => 95.333333333333
)
[1] => Array
(
[0] => blabla other values
[1] => 86
)
[2] => Array
(
[0] => blabla values
[1] => 91.181818181818
)
)

Merge two arrays without replacing values

Im a bit lost or mind is not working as it should. I read other questions around but can get mine to work.
I got this array:
Array
(
[89] => Array
(
[0] => 16
[1] => 2
)
)
And i got this :
Array
(
[84] => Array
(
[0] => 2
)
[83] => Array
(
[0] => 2
)
[87] => Array
(
[0] => 2
[1] => 3
)
[88] => Array
(
[0] => 2
)
[89] => Array
(
[0] => 2
)
[90] => Array
(
[0] => 2
)
)
I should get all results but on key 89 i should get the value from first array.
Array
(
[84] => Array
(
[0] => 2
)
[83] => Array
(
[0] => 2
)
[87] => Array
(
[0] => 2
[1] => 3
)
[88] => Array
(
[0] => 2
)
[89] => Array
(
[0] => 16
[1] => 2
)
[90] => Array
(
[0] => 2
)
)
Array merge wont work :( .
Also after i get the result if the first array its :
Array
(
[89] => Array
(
[1] => 2
)
)
The resulting array should update to one record.
Im sure its a 1 min code for you gurus but arrays always been a pain for me.
Thanks
UPDATE : if i use array_merge_recursive it wont keep my keys :
print_r(array_merge_recursive($array1,$array2));
Array
(
[0] => Array
(
[0] => 16
[1] => 2
)
[1] => Array
(
[0] => 2
)
[2] => Array
(
[0] => 2
)
[3] => Array
(
[0] => 2
[1] => 3
)
[4] => Array
(
[0] => 2
)
[5] => Array
(
[0] => 2
)
[6] => Array
(
[0] => 2
)
)
array_merge_recursive should do the job; look at the documentation for more pointers: http://www.php.net/manual/en/function.array-merge-recursive.php
EDIT
The function behaved differently than I initially thought, hereĀ“s a different version of the function to solve your problem:
function array_merge_recursive_distinct(array &$array1, array &$array2) {
$merged = $array1;
foreach($array2 as $key => &$value) {
if(is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = array_merge_recursive_distinct($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}
return $merged;
}
Thanks to the community of php.net http://www.php.net/manual/en/function.array-merge-recursive.php#92195
If I understand it correctly, adding arrays should suffice:
$result_array = $array1 + $array2;
In contrast to array_merge, it won't overwrite values in first array and won't renumber numerical keys.
$newarray=array();
foreach(array_merge($array1,$array2) as $k=>$arr){
$newarray[$k]=array_merge($array1[$k],$array2[$k]);
}
foreach($firstArr as $key=>$val){
if(in_array($key,$secondArray)){
$secondArray[$key] = $firstArr[$key];
}
}
Try this may help you.

Categories