Match Array values and merge if the values matches in PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have two array i want to match a key of first array with another array and if the values of both key matches then add the value of second array to array 1
Array 1
[{"currency":1,"amount":23},{"currency":1,"amount":30},{"currency":2,"amount":40},]
Array 2
[{"currency_id": 1,"currency_symbol":$},{"currency_id":2,"currency_symbol":€}]
Desired output is:
[{"currency":$,"amount":23},{"currency":$,"amount":30},{"currency":€,"amount":40}]
The Code i am using is:
foreach($a1 as $key) {
foreach($a2 as $cKey){
if($a1['currency']==$a2['currency_id']){
$a1['currency_symbol'] = $a2['currency_symbol'];
echo $a1['currency_symbol'];
}
}
}

foreach($arr1 as $k=>$key) {
foreach($arr2 as $cKey){
if($key['currency']==$cKey['currency_id']){
$arr3[$k]['currency'] = $cKey['currency_symbol'];
$arr3[$k]['amount'] = $key['amount'];
}
}
}
var_export($arr3);
You only need to assign variable and print outside loop.

Related

PHP multidimensional array loop through second dimension [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
how do i loop this kind of array?
$arr = array (
"aa"=>array("apple","orange"),
"bb"=>array("373","22"),
"cc"=>array("t0","h0"),
"dd"=>array("1","0")
);
I want to loop through the column of each item.
e.g.: i want to display ('apple','373','t0','1') at the first loop and ('orange','22','h0','0') at the last loop. thanks
We are assuming that all the arrays inside the main array are the same size in this example.
$arr = array (
"aa"=>array("apple","orange"),
"bb"=>array("373","22"),
"cc"=>array("t0","h0"),
"dd"=>array("1","0")
);
for($i = 0; $i<sizeof($arr["aa"]); $i++)
{
foreach($arr as $key=>$item)
{
echo($item[$i]);
}
echo ' - ';
}
Output: (obviously you can do any necessary formatting you need such as new lines or commas)
apple373t01 - orange22h00 -

How to return array unique value? in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need an array like this in PHP?
array = [mani , mani, nithi, nithi, basto]
return in
array = [basto]
not for other elements
Does anyone know how to solve this problem?
<?php
$items = ['mani' , 'mani', 'nithi', 'nithi', 'basto'];
$counts = array_count_values($items);
// Remove elements that occur more than once.
$filtered = array_filter($items, function ($item) use ($counts) {
return $counts[$item] === 1;
});
var_export($filtered);
Output:
array (
4 => 'basto',
)
You may want to specify your question so users here can provide better assistance.
To your issue:
array_unique($array);
https://www.php.net/manual/en/function.array-unique.php
EDIT: you want to search all items by name, to do that you need this function: https://www.php.net/manual/en/function.array-search.php
with your example:
$array = ['mani' , 'mani', 'nithi', 'nithi', 'basto'];
$basto = array_search('basto', $array);
Best,
Sebo

PHP auto compute multidimensional arrays against one another [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a multidimensional array.
$parent_array($child1('lat','long'),$child2('lat','long')...)
The amount of child arrays can be of any value per request starting from 2.
The parent array is then called into a function that has a google distance calculator between to or more points.
function distance($parent_array)
{
foreach ( $parent_array as $child){
$distance = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins='.$child1['lat'].','.$child1['long'].'&destinations='.$child2['lat'].','.$child2['long'].'&key=********************************');
$trim = trim($this->objectToArray($distance)['rows'][0]['elements'][0]['distance']['text'],' km');
}
return $trim;
}
I need to get this function to calculate the distance between all posted child arrays as illustrated on the above function using
$child1 and $child2
As I understand you, what you need is a cartesian product. This is as simple as 2 foreach cycles:
foreach ($points as $point1) {
foreach ($points as $point2) {
computeDistance($point1, $point2);
}
}

Php combine an array of values into an array with double combinations string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How i can combine ad array of values into an array with double combination string without duplications ?
For example, if i have something like:
array('one','two','tre','four','five');
I want to obtain an array of combinations like this ('one / two') just one time and not also ('two / one').
In this way i wanto to get something similar to:
array('one/two', 'one/tre', 'one/four', 'one/five', 'two/tree','two/four' .......
Suggestions ?
You can do it with this code. It won't show two/one, three/two, etc (that's how I understood it):
<?php
$array = array('one','two','tre','four','five');
$newArray = [];
foreach ($array as $el) {
foreach ($array as $el2) {
if ($el === $el2) continue;
$newArray[] = $el."/".$el2;
}
array_shift($array); // remove the element we just went through
}
print_r($newArray);
Demo

For each for these numbers make a loop on PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
From a mySQL row I get the below from column coop as appeared (with commas)
1,2,6,27
My question is how can I have something like
for
as numbers as the column
do the loop {
{
Assuming you have the values stored in a string, let's call it $dbValue, you can split that string into an array:
$values = explode(",", $dbValue);
Then just loop over that array:
foreach ($values as $value) {
// do something with each value
}
As an aside... Storing delimited values in a single database field is very often a bad idea. Each database field should contain one discrete value. If multiple values are needed, you'd create a separate table with a many-to-one relationship.
seems foreach
$tmp = explode(',', $yourvalue) // $yourvalue = '1,2,6,27'
foreach ( $tmp as $key => $value ) {
echo $value;
}

Categories