I have received array like this
Array
(
[hash] => 9761d3233f9cb256c0992be
[total] => 2736712601
[received] => 2017-01-13T21:43:32.047Z
[income] => Array
(
[0] => Array
(
[value] => 647262
[addresses] => Array
(
[0] => Address_1
)
)
[1] => Array
(
[value] => 17200000
[addresses] => Array
(
[0] => Address_2
)
)
[2] => Array
(
[value] => 3729034
[addresses] => Array
(
[0] => Address_3
)
)
[3] => Array
(
[value] => 2414997500
[addresses] => Array
(
[0] => Address_4
)
)
[4] => Array
(
[value] => 10856454
[addresses] => Array
(
[0] => Address_5
)
)
)
)
So in my database I store the hash (9761d3233f9cb256c0992be). The I hash and the address. Then based on them I match the correct array from [income]. When I found correct address in income I take the [value] and showing it on the page. Here is how I've made it
$url=get_curl_content("https://example.com/".$order->hash);
$totala =json_decode($url,true);
....
$match = true;
foreach ($totala['income'] as $data) {
if ($data['addresses'] == $order->address) {
$match = $data;
break;
}
}
$price = $data['value'];
The problem is that I'm expecting Address_2 because in database i have saved Address_2 I've got Address_3 instead.
When I var_dump($data['addresses']) i got Address_3. What can be the problem?
Try like this. It will search and match in array using in_array function
$match = true;
foreach ($totala['income'] as $data) {
if (in_array($order->address, $data['addresses'])) {
$match = $data;
break;
}
}
$price = $match['value'];
Related
I have two arrays, one called fetched_services and one called fetched_companies, they look like this:
fetched_services
(
[1] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 153
)
[1] => Array
(
[id] => 137
)
[2] => Array
(
[id] => 138
)
)
)
)
fetched_companies
(
[0] => stdClass Object
(
[services] => Array
(
[0] => 25
[1] => 102
)
)
)
What i want to achieve is to end up with an array like fetched_services but only having child_services with id of fetched_companies["services"].
What i have tried is this:
$services = [];
$isFound = false;
foreach ($fetched_services as $fetched_service) {
foreach ($fetched_service["child_services"] as $fetched_child_service) {
$fetched_service["child_services"] = [];
foreach ($fetched_companies as $fetched_company) {
if( (in_array($fetched_child_service["id"],$fetched_company->services)) ) {
$fetched_service["child_services"][] = $fetched_child_service;
$isFound = true;
}
}
if($isFound) {
$services[] = $fetched_service;
$isFound = false;
}
}
}
This outputs this:
Array
(
[0] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 116
)
)
)
[1] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 117
)
)
)
)
As you can see the resulting array have two arrays containing same id but the child_services are different.
What i want to end up with is this:
Array
(
[0] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 116
)
)
(
[0] => Array
(
[id] => 117
)
)
)
)
What am i doing wrong here? Thanks!
Your question is a bit unclear (I'll edit/delete this if/when you clarify, see my comment above), but you could probably make use of array_filter by only keeping services that are present in said list (via in_array):
$result = array_filter(
$fetched_services['child_services'],
fn(array $child_service): bool => in_array($child_service['id'], $fetched_companies[0]->services, true)
);
Demo
data
Array
(
[0] => Array
(
[id] => 9962
[value] => Amart
)
[1] => Array
(
[id] => attrval_9962
[value] => k
)
[2] => Array
(
[id] => 9952
[value] => Denim
)
[3] => Array
(
[id] => attrval_9952
[value] => l
)
[4] => Array
(
[id] => 5788
[value] => Grey
)
[5] => Array
(
[id] => 21307
[value] => Long Sleeve
)
)
Above is the data that store in array, anyone know how to unset the data if the attrval_ exist so that i want to unset the data id 9962. so the data will look like below.
Array
(
[0] => Array
(
[id] => attrval_9962
[value] => k
)
[1] => Array
(
[id] => attrval_9952
[value] => l
)
[2] => Array
(
[id] => 5788
[value] => Grey
)
[3] => Array
(
[id] => 21307
[value] => Long Sleeve
)
)
Which mean that I only want to store the data which is attrval_ exists else will store back the default value.
First get an Array which contain
$filtered = array_filter($data, function($element){
return strpos($element['id'], 'attrval_') !== false;
});
Then find the difference of both array.
$result = array_udiff($data, $filtered, function($a, $b) {
return intval($a != $b);
});
You can do it with
$result = [];
foreach(data as $value){
if(strpos($value['id'],'attrval_) !== false){
$result[] = $value
}
}
$filtered = array_filter($array, function ($data) {
return strpos($data['id'], 'attrval_') !== false;
});
I have the following Two array results coming from MySQL query result.
Array One (Orignal-Data):
Array
(
[w_a] => Array
(
[0] => Array
(
[cod] => CRR
[pr] => LL
[aid] => VM2254
[gender] => m
[title] =>
)
)
[w_a_ml] => Array
(
)
[w_a_ol] => Array
(
)
[w_a_rl] => Array
(
[0] => Array
(
[rol] => 1
)
)
)
Array Two (Changed-Data)
Array
(
[w_a] => Array
(
[0] => Array
(
[cod] => CRR
[pr] => LL
[aid] => VM2254
[gender] => f
[title] => Mr
)
)
[w_a_ml] => Array
(
[0] => Array
(
[wl] => 255
[care] => Sahan
[heigh] =>
[adam] =>
[instance] => Look
)
)
[w_a_ol] => Array
(
)
[w_a_rl] => Array
(
[0] => Array
(
[rol] => 1
)
)
)
What I wan to achieve from the above two array is to compare and show the result in table format or Inside Form To each Field (Original and New-Change) like below.
FiledsN Original New Change
title Empty Mr
Wl Empty 255
Care Empty Sahan
gender m f
instance Empty Look
I've tried making the array a single array using this function:
function array_single($arr) {
if (!is_array($arr)) {
return FALSE;
}
$res = array();
foreach ($arr as $keys => $values) {
if (is_array($values)) {
$res = array_merge($res, array_single($values));
} else {
$res[$keys] = $values;
}
}
return $res;
}
And then comparing using this:
function diff($new,$old) {
$del=array_diff_assoc($old,$new);
$add=array_diff_assoc($new,$old);
return $diff=array("old"=>$del, "new"=>$add);
}
The following code is converting XML to array. I want to get a sting by the value of [name]. So for example: $..[offerid].. should give 8b5f7fd3806a42ccb0ade9f8309c5587
Who can help me?? :)
$xmldata = file_get_contents($XMLURL);
$arr= xml2ary($xmldata);
foreach($arr['m4n']['_c']['data']['_c']['record'] as $result){
echo "<pre>"; print_r($result);
}
The echo result is:
Array
(
[recordHash] => Array
(
[_v] => -652572603
)
[column] => Array
(
[0] => Array
(
[_a] => Array
(
[name] => url
)
[_v] => http://ad.dd.com/ppc/?20910868C187884459&zpar6=DF_1&ULP=[[http%3A%2F%2F]]
)
[1] => Array
(
[_a] => Array
(
[name] => title
)
[_v] => This is the title
)
[2] => Array
(
[_a] => Array
(
[name] => description
)
[_v] => Aanbod
)
[3] => Array
(
[_a] => Array
(
[name] => offerid
)
[_v] => 8b5f7fd3806a42ccb0ade9f8309c5587
)
)
)
Try looping through all of the items using a php foreach loop.
$name_to_find = 'offerid';
$value = FALSE;
foreach ($result['column'] as $item) {
if ($item['_a']['name'] == $name_to_find) {
$value = $item['_v'];
break;
}
}
// Now $value has the value of the entry with the name $name_to_find
If you end up finding an entry with the given $name_to_find, then $value will not be FALSE (granted that, you don't have any FALSE values :D).
I have following array pattern.
Array
(
[0] => Array
(
[label] => 2011
[subtable] => Array
(
[0] => Array
(
[label] => 05
[subtable] => Array
(
[0] => Array
(
[label] => /hello-world.html
[url] => http://example.com/2011/05/hello-world.html
)
[1] => Array
(
[label] => /test-test-test.html
[url] => http://example.com/2011/05/test-test-test.html
)
)
)
[1] => Array
(
[label] => 04
[subtable] => Array
(
[0] => Array
(
[label] => /i-have-some-problem-with-gfc-updates.html
[url] => http://example.com/2011/04/i-have-some-problem-with-gfc-updates.html
)
[1] => Array
(
[label] => /testing-for-google-frineds-update.html
[url] => http://example.com/2011/04/testing-for-google-frineds-update.html
)
)
)
[2] => Array
(
[label] => /05
[url] => http://example.com/2011/05
)
[3] => Array
(
[label] => /04
[url] => http://example.com/2011/04
)
)
)
[1] => Array
(
[label] => /index
[url] => http://example.com/
)
)
I want to get "label" and "url" into another array like
array(
[0] = array([label] => a [url] => http://example.com/a/)
[1] = array([label] => b [url] => http://example.com/b/)
)
How can I achieve it using recursion, or by passing it to a function?
You may use array_walk_recursive for this:
$parsed_array = array();
function parse_array($item, $key) {
global $parsed_array;
if(is_array($item) && isset($item['url'])) {
$parsed_array[] = $item;
}
}
array_walk_recursive($your_array, 'parse_array');
Or you may implement it using a recursive function:
function parse_array($array) {
$result = array();
foreach($array as $item) {
if(isset($item['subtable'])) {
$result = array_merge($result, parse_array($item['subtable']));
}else{
$result[] = $item;
}
}
return $result;
}
$parsed_array = parse_array($your_array);