remove array by matching two array. One is simple array and other is multidimensional array. I want to remove elements of second array if value not exist in 1st array. So there are total 20 element in 1st array. So there will be only 20 element in second array.
Final result that I want, where value should be match with Ist array. no element in second array whose value does not match in 1st array. So only the elements of second array whose value match with 1st array.
Array
(
[0] => Array
(
[label] => Afrikaans
[value] => af
)
[1] => Array
(
[label] => Albanian
[value] => sq
)
[2] => Array
(
[label] => Arabic
[value] => ar
)
[3] => Array
(
[label] => Armenian
[value] => hy
)
[4] => Array
(
[label] => Azerbaijani
[value] => az
)
[5] => Array
(
[label] => Basque
[value] => eu
)
[6] => Array
(
[label] => Belarusian
[value] => be
)
[7] => Array
(
[label] => Bengali
[value] => bn
)
[8] => Array
(
[label] => Bosnian
[value] => bs
)
[9] => Array
(
[label] => Bulgarian
[value] => bg
)
[10] => Array
(
[label] => Catalan
[value] => ca
)
[11] => Array
(
[label] => Cebuano
[value] => ceb
)
[12] => Array
(
[label] => Chinese
[value] => zh-CN
)
[13] => Array
(
[label] => Chinese (Traditional)
[value] => zh-TW
)
[14] => Array
(
[label] => Croatian
[value] => hr
)
[15] => Array
(
[label] => Czech
[value] => cs
)
[16] => Array
(
[label] => Danish
[value] => da
)
[17] => Array
(
[label] => Dutch
[value] => nl
)
[18] => Array
(
[label] => English
[value] => en
)
[19] => Array
(
[label] => Esperanto
[value] => eo
)
[20] => Array
(
[label] => Estonian
[value] => et
)
1st array
Array
(
[0] => ar
[1] => bn
[2] => zh-CN
[3] => en
[4] => fr
[5] => de
[6] => el
[7] => gu
[8] => hi
[9] => it
[10] => ja
[11] => kn
[12] => ko
[13] => la
[14] => mr
[15] => pa
[16] => ru
[17] => es
[18] => ta
[19] => te
[20] => ur
)
2nd array
Array
(
[0] => Array
(
[label] => Afrikaans
[value] => af
)
[1] => Array
(
[label] => Albanian
[value] => sq
)
[2] => Array
(
[label] => Arabic
[value] => ar
)
[3] => Array
(
[label] => Armenian
[value] => hy
)
[4] => Array
(
[label] => Azerbaijani
[value] => az
)
[5] => Array
(
[label] => Basque
[value] => eu
)
[6] => Array
(
[label] => Belarusian
[value] => be
)
[7] => Array
(
[label] => Bengali
[value] => bn
)
[8] => Array
(
[label] => Bosnian
[value] => bs
)
[9] => Array
(
[label] => Bulgarian
[value] => bg
)
[10] => Array
(
[label] => Catalan
[value] => ca
)
[11] => Array
(
[label] => Cebuano
[value] => ceb
)
[12] => Array
(
[label] => Chinese
[value] => zh-CN
)
[13] => Array
(
[label] => Chinese (Traditional)
[value] => zh-TW
)
[14] => Array
(
[label] => Croatian
[value] => hr
)
[15] => Array
(
[label] => Czech
[value] => cs
)
[16] => Array
(
[label] => Danish
[value] => da
)
[17] => Array
(
[label] => Dutch
[value] => nl
)
[18] => Array
(
[label] => English
[value] => en
)
[19] => Array
(
[label] => Esperanto
[value] => eo
)
[20] => Array
(
[label] => Estonian
[value] => et
)
[21] => Array
(
[label] => Filipino
[value] => tl
)
[22] => Array
(
[label] => Finnish
[value] => fi
)
[23] => Array
(
[label] => French
[value] => fr
)
[24] => Array
(
[label] => Galician
[value] => gl
)
[25] => Array
(
[label] => Georgian
[value] => ka
)
[26] => Array
(
[label] => German
[value] => de
)
[27] => Array
(
[label] => Greek
[value] => el
)
[28] => Array
(
[label] => Gujarati
[value] => gu
)
[29] => Array
(
[label] => Haitian
[value] => ht
)
[30] => Array
(
[label] => Hausa
[value] => ha
)
[31] => Array
(
[label] => Hebrew
[value] => iw
)
[32] => Array
(
[label] => Hindi
[value] => hi
)
[33] => Array
(
[label] => Hmong
[value] => hmn
)
[34] => Array
(
[label] => Hungarian
[value] => hu
)
[35] => Array
(
[label] => Icelandic
[value] => is
)
[36] => Array
(
[label] => Igbo
[value] => ig
)
[37] => Array
(
[label] => Indonesian
[value] => id
)
[38] => Array
(
[label] => Irish
[value] => ga
)
[39] => Array
(
[label] => Italian
[value] => it
)
[40] => Array
(
[label] => Japanese
[value] => ja
)
[41] => Array
(
[label] => Javanese
[value] => jv
)
[42] => Array
(
[label] => Kannada
[value] => kn
)
[43] => Array
(
[label] => Khmer
[value] => km
)
[44] => Array
(
[label] => Korean
[value] => ko
)
[45] => Array
(
[label] => Lao
[value] => lo
)
[46] => Array
(
[label] => Latin
[value] => la
)
[47] => Array
(
[label] => Latvian
[value] => lv
)
[48] => Array
(
[label] => Lithuanian
[value] => lt
)
[49] => Array
(
[label] => Macedonian
[value] => mk
)
[50] => Array
(
[label] => Malay
[value] => ms
)
[51] => Array
(
[label] => Maltese
[value] => mt
)
[52] => Array
(
[label] => Maori
[value] => mi
)
[53] => Array
(
[label] => Marathi
[value] => mr
)
[54] => Array
(
[label] => Norwegian
[value] => no
)
[55] => Array
(
[label] => Persian
[value] => fa
)
[56] => Array
(
[label] => Polish
[value] => pl
)
[57] => Array
(
[label] => Portuguese
[value] => pt
)
[58] => Array
(
[label] => Punjabi
[value] => pa
)
[59] => Array
(
[label] => Romanian
[value] => ro
)
[60] => Array
(
[label] => Russian
[value] => ru
)
[61] => Array
(
[label] => Serbian
[value] => sr
)
[62] => Array
(
[label] => Slovak
[value] => sk
)
[63] => Array
(
[label] => Slovenian
[value] => sl
)
[64] => Array
(
[label] => Somali
[value] => so
)
[65] => Array
(
[label] => Spanish
[value] => es
)
[66] => Array
(
[label] => Swahili
[value] => sw
)
[67] => Array
(
[label] => Swedish
[value] => sv
)
[68] => Array
(
[label] => Tamil
[value] => ta
)
[69] => Array
(
[label] => Telugu
[value] => te
)
[70] => Array
(
[label] => Thai
[value] => th
)
[71] => Array
(
[label] => Turkish
[value] => tr
)
[72] => Array
(
[label] => Ukrainian
[value] => uk
)
[73] => Array
(
[label] => Urdu
[value] => ur
)
[74] => Array
(
[label] => Vietnamese
[value] => vi
)
[75] => Array
(
[label] => Welsh
[value] => cy
)
[76] => Array
(
[label] => Yiddish
[value] => yi
)
[77] => Array
(
[label] => Yoruba
[value] => yo
)
[78] => Array
(
[label] => Zulu
[value] => zu
)
)
Example
$a1 = your first array;
$a2 = second array;
foreach($a2 as $k => $v)
{
if(!in_array($v['value'],$a1)
{
unset($a2[$k]);
}
}
So you will have $a2 with the only remaining values which will be present in $a1.
Assuming you don't need to preserve the indexes in the second array, you can flip the first array and assign temporary keys to the second and call array_intersect_key().
Though this appears to make several function calls, none of them are done in a loop. Using in_array() inside of a loop, will mean (in your case) that php will be doing 59 full array scans and 20 partial scans -- This will not be efficient.
Code: (Demo)
var_export( // print to screen
array_values( // this is optional. This just reindexes the array
array_intersect_key( // retain elements where key exists in both arrays
array_column($fulllist,null,'value'), // make new keys for 2nd array
array_flip($keep) // swap values and indexes of 1st array
)
)
);
Output:
array (
0 =>
array (
'label' => 'Arabic',
'value' => 'ar',
),
1 =>
array (
'label' => 'Bengali',
'value' => 'bn',
),
2 =>
array (
'label' => 'Chinese',
'value' => 'zh-CN',
),
3 =>
array (
'label' => 'English',
'value' => 'en',
),
4 =>
array (
'label' => 'French',
'value' => 'fr',
),
5 =>
array (
'label' => 'German',
'value' => 'de',
),
6 =>
array (
'label' => 'Greek',
'value' => 'el',
),
7 =>
array (
'label' => 'Gujarati',
'value' => 'gu',
),
8 =>
array (
'label' => 'Hindi',
'value' => 'hi',
),
9 =>
array (
'label' => 'Italian',
'value' => 'it',
),
10 =>
array (
'label' => 'Japanese',
'value' => 'ja',
),
11 =>
array (
'label' => 'Kannada',
'value' => 'kn',
),
12 =>
array (
'label' => 'Korean',
'value' => 'ko',
),
13 =>
array (
'label' => 'Latin',
'value' => 'la',
),
14 =>
array (
'label' => 'Marathi',
'value' => 'mr',
),
15 =>
array (
'label' => 'Punjabi',
'value' => 'pa',
),
16 =>
array (
'label' => 'Russian',
'value' => 'ru',
),
17 =>
array (
'label' => 'Spanish',
'value' => 'es',
),
18 =>
array (
'label' => 'Tamil',
'value' => 'ta',
),
19 =>
array (
'label' => 'Telugu',
'value' => 'te',
),
20 =>
array (
'label' => 'Urdu',
'value' => 'ur',
),
)
Easy (if i managed to understand...)
let newArray = languages.filter(lang => return codes.indexof(lang.value) !== -1);
UPDATE
Oh my :-) wrong language, sorry
$final = array_filter($languages, function($language) use($codes) { return in_array($language['value'], $codes)});
UPDATE 2
Lets brake it down:
array_filter can be used to filter an array :-), by default array_filter will remove "falsy" values. With a callback as second parameter you can define how to filter the input array. The first parameter is the input array.
I use array_filter instead of unsetting the original values, because its cleaner and more robust.
PHP knows anonymous functions - a filter operation is a perfect usecase for that. But because PHP is not the best programming language out there we have to use a special syntax to simulate some kind of scoping, thats why you see this part:
use($codes)
if i would define the filter like:
$final = array_filter($languages, function($language) {
return in_array($language['value'], $codes)
});
PHP would throw an exception because $codes is not defined within the function scope.
Anonymous functions support the use() block, that way we can "expose" the codes-array to our function.
$final = array_filter($languages, function($language) use($codes) {
return in_array($language['value'], $codes)
});
finally we check if the language is valid, by searching for the language-value inside our $codes array (which will hold the allowed languages). If the code does not exist in $codes in_array will return false and we return that to array_filter, so this element is not valid.
If the code of the checked language exists in codes, in_array will return true, we return it to array_filter and the item is valid.
I am trying to get the value of bank_name in php. Can't get it to display.
stdClass Object ( [account_id] => 43726384 [name] => Account Name [state] => action_required [description] => [owner_user_id] =>[type] => personal [create_time] => 1477684534 [disablement_time] => [country] => US [currencies] => Array ( [0] => USD ) [action_reasons] => Array ( [0] => kyc [1] => bank_account ) [disabled_reasons] => Array ( ) [image_uri] => [supported_card_types] => Array ( [0] => visa [1] => mastercard [2] => american_express [3] => discover [4] => jcb [5] => diners_club ) [gaq_domains] => Array ( [0] => ) [balances] => Array ( [0] => stdClass Object ( [balance] => 0 [currency] => USD [disputed_amount] => 0 [incoming_pending_amount] => 0 [outgoing_pending_amount] => 0 [reserved_amount] => 0 [bank_name] => Bank Of America [withdrawal_next_time] => [withdrawal_period] => [withdrawal_type] => ) ) [statuses] => Array ( [0] => stdClass Object ( [currency] => USD [incoming_payments_status] => ok [outgoing_payments_status] => paused [account_review_status] => not_requested ) ) )
It's not very clear if you display data that way, but I think this would be the way to access to bank_name:
$bankName = $object->balances[0]->bank_name;
(If it doesn't work, please show the XML before parsing it to an object)
This is the original main array:
Array
(
[0] => Array
(
[subtotal] => 0.6000
[taxes] => 0.0720
[charged_amount] => 0.6720
[total_discount] => 0.0000
[provinceName] => BC
[store_key] => 1
[store_id] => 5834
[categories] => Array
(
[2] => 0.6000
[4] => 0
[3] => 0
)
)
[1] => Array
(
[subtotal] => 29.8500
[taxes] => 2.3270
[charged_amount] => 20.2370
[total_discount] => 11.9400
[provinceName] => MB
[store_key] => 9
[store_id] => 1022
[categories] => Array
(
[2] => 0
[4] => 29.8500
[3] => 0
)
)
[2] => Array
(
[subtotal] => 0.3000
[taxes] => 0.0390
[charged_amount] => 0.3390
[total_discount] => 0.0000
[provinceName] => NB
[store_key] => 8
[store_id] => 1013
[categories] => Array
(
[2] => 0.3000
[4] => 0
[3] => 0
)
)
[3] => Array
(
[subtotal] => 24.3100
[taxes] => 1.1830
[charged_amount] => 10.2830
[total_discount] => 15.2100
[provinceName] => NL
[store_key] => 4
[store_id] => 3033
[categories] => Array
(
[2] => 24.3100
[4] => 0
[3] => 0
)
)
[4] => Array
(
[subtotal] => 1116.3400
[taxes] => 127.6960
[charged_amount] => 1110.0060
[total_discount] => 134.0300
[provinceName] => ON
[store_key] => 2
[store_id] => 1139
[categories] => Array
(
[2] => 85.7300
[4] => 143.2800
[3] => 887.3300
)
)
[5] => Array
(
[subtotal] => 10.8500
[taxes] => 1.4100
[charged_amount] => 12.2600
[total_discount] => 0.0000
[provinceName] => ON
[store_key] => 5
[store_id] => 1116
[categories] => Array
(
[2] => 10.8500
[4] => 0
[3] => 0
)
)
)
I just need to add the values of the array [categories] with same keys and use it further to print the total, but not getting correct output, can someone help me out to get the desired result:
Desired result
An array with same keys but total of individual array values
Array ( [2] => 0.9000 [4] => 29.8500 [3] => 1.5 )
NOTE: Initial array is dynamic can have n number of key value pair
Thanks
The first thing that you need to do is iterate through the outer array. Then, for each row in the outer array, you and to iterate through each entry in the category element. So this means that we have two foreach loops. Inside the inner foreach, we simply set the value for the current index to be the value of the same index on a 'sum' array (if it doesn't already exist), or increment the value of that index if it already exists in the 'sum' array.
<?php
$sumArray = array();
foreach($outerArray as $row)
{
foreach($row["categories"] as $index => $value)
{
$sumArray[$index] = (isset($sumArray[$index]) ? $sumArray[$index] + $value : $value);
}
}
?>
Demo using your example array
I'm writing this query in yii2 -
$senderInfo[] = Customers::find()->where(['cust_id' => $resShipment['ship_shipper_id'], 'cust_country_code' => Yii::$app->user->identity->country_code])->distinct()->all();
and getting this as a result -
Array ( [0] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( [0] => common\models\Customers Object ( [_attributes:yii\db\BaseActiveRecord:private] => Array ( [cust_id] => 79 [cust_user_id] => 2 [cust_designation] => Mr [cust_fname] => abcd [cust_mname] => xyz [cust_lname] => xyz [cust_country_code] => 91 [cust_mobile] => 8888888888 [cust_email] => abcdxyz#gmail.com [cust_id_proof] => [cust_country] => [cust_state] => [cust_city] => [cust_location] => [cust_street] => [cust_address] => delhi [cust_pobox] => 02881 [cust_additional_detail] => [cust_picture] => [verified] => Yes [created] => 2016-04-20 14:45:42 [updated] => 0000-00-00 00:00:00 [deleted] => No ) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ( [cust_id] => 79 [cust_user_id] => 2 [cust_designation] => Mr [cust_fname] => abcd [cust_mname] => xyz [cust_lname] => xyz [cust_country_code] => 91 [cust_mobile] => 8888888888 [cust_email] => abcdxyz#gmail.com [cust_id_proof] => [cust_country] => [cust_state] => [cust_city] => [cust_location] => [cust_street] => [cust_address] => delhi [cust_pobox] => 413714 [cust_additional_detail] => [cust_picture] => [verified] => Yes [created] => 2016-04-20 14:45:42 [updated] => 0000-00-00 00:00:00 [deleted] => No ) [_related:yii\db\BaseActiveRecord:private] => Array ( ) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => Array ( ) ) ) )
But, because of the empty arrays in the start of this array, I can't access the elements. I've to populate it in a for(strictly) loop, after it gets accessible.
If you want the result in array you can use ActiveRecord function toArray()
$myModel = Customers::find()->
where(['cust_id' => $resShipment['ship_shipper_id'],
'cust_country_code' => Yii::$app->user->identity->country_code])->distinct()->all();
$senderInfo = myModel->toArray();
I am trying to get values from a complex associative array it is obtained from an xml file.Here is the actual output of array (https://www.dropbox.com/s/k6lxrx6h9x3x83u/xml-first.txt?dl=0).
Here is a sample
Array
(
[drawserialnumber] => 48285
[lotteryname] => Akshaya
[shortname] => AK
[drawdate] => 2015-06-17
[drawheld] => Array
(
)
[drawnumber] => 194
[drawvenew] => SREE CHITHRA HOME AUDITORIUM, PAZHAVANGADI, EAST FORT, THIRUVANANTHAPURAM
[approvedby] => A.Jayakumar
[approverdesig] => Deputy Director
[approveroffice] => Directorate Of State Lotteries , Vikas Bhavan,tvm
[prizes] => Array
(
[prizedetails] => Array
(
[0] => Array
(
[prizeno] => 1
[prizedesc] => Rs :6,500,000/-
[totalamt] => 6500000
[conslation] => f
[prizeticket] => Array
(
[seriesname] => AK
[digit] => 279045
[district] => THRISSUR
)
)
[1] => Array
(
[prizeno] => 1
[prizedesc] => Rs :10,000/-
[totalamt] => 10000
[conslation] => t
[prizeticket] => Array
(
[0] => Array
(
[seriesname] => AJ
[digit] => 279045
[district] => NIL
)
[1] => Array
(
[seriesname] => AR
[digit] => 279045
[district] => NIL
)
[2] => Array
(
[seriesname] => AM
[digit] => 279045
[district] => NIL
)
[3] => Array
(
[seriesname] => AO
[digit] => 279045
[district] => NIL
)
[4] => Array
(
[seriesname] => AP
[digit] => 279045
[district] => NIL
)
[5] => Array
(
[seriesname] => AL
[digit] => 279045
[district] => NIL
)
)
)
[2] => Array
(
[prizeno] => 2
[prizedesc] => Rs :200,000/-
[totalamt] => 200000
[conslation] => f
[prizeticket] => Array
(
[0] => Array
(
[seriesname] => AR
[digit] => 420734
[district] => KANNUR
)
[1] => Array
(
[seriesname] => AJ
[digit] => 221136
[district] => ALAPPUZHA
)
[2] => Array
(
[seriesname] => AK
[digit] => 825429
[district] => WAYANAD
)
[3] => Array
(
[seriesname] => AL
[digit] => 171621
[district] => THIRUVANANTHAPURAM
)
[4] => Array
(
[seriesname] => AM
[digit] => 786170
[district] => ERNAKULAM
)
[5] => Array
(
[seriesname] => AO
[digit] => 668158
[district] => PALAKKAD
)
[6] => Array
(
[seriesname] => AP
[digit] => 244326
[district] => KANNUR
)
)
)
[3] => Array
(
[prizeno] => 3
[prizedesc] => Rs :10,000/-
[totalamt] => 10000
[conslation] => f
[prizeticket] => Array
(
[seriesname] => Array
(
)
[digit] => 25426
[district] => Array
(
)
)
)
[4] => Array
(
[prizeno] => 4
[prizedesc] => Rs :5,000/-
[totalamt] => 5000
[conslation] => f
[prizeticket] => Array
(
[0] => Array
(
[seriesname] => Array
(
)
[digit] => 6989
[district] => Array
(
)
)
[1] => Array
(
[seriesname] => Array
(
)
[digit] => 3242
[district] => Array
(
)
)
and so on for may occurances
Now i want to get the details to an html table ,its almost done but the problem is
pricedetails array.
Here is the code for getting pricedetails
foreach ($data['prizes'] as $prizes) {
foreach($prizes as $prize){
echo '<pre>';
print_r($prize);
echo '</pre>';
$temp=$prize['prizeticket'];
foreach($temp as $ticket){
print_r($ticket);
}
}
}
Here is the two iterations of Print_r($prize)
Array
(
[prizeno] => 1
[prizedesc] => Rs :6,500,000/-
[totalamt] => 6500000
[conslation] => f
[prizeticket] => Array
(
[seriesname] => AK
[digit] => 279045
[district] => THRISSUR
)
)
Array
(
[prizeno] => 1
[prizedesc] => Rs :10,000/-
[totalamt] => 10000
[conslation] => t
[prizeticket] => Array
(
[0] => Array
(
[seriesname] => AJ
[digit] => 279045
[district] => NIL
)
[1] => Array
(
[seriesname] => AR
[digit] => 279045
[district] => NIL
)
[2] => Array
(
[seriesname] => AM
[digit] => 279045
[district] => NIL
)
[3] => Array
(
[seriesname] => AO
[digit] => 279045
[district] => NIL
)
[4] => Array
(
[seriesname] => AP
[digit] => 279045
[district] => NIL
)
[5] => Array
(
[seriesname] => AL
[digit] => 279045
[district] => NIL
)
)
)
As you see the first iteration contains only one sub array under prizedetails and the second iterations contains more than one sub arrays under prizedetails.
The problem i can't get the values with both key-value pairs.
print_r($ticket) output
AK
279045
THRISSUR
Array
(
[seriesname] => AJ
[digit] => 279045
[district] => NIL
)
Array
(
[seriesname] => AR
[digit] => 279045
[district] => NIL
)
Notice that first output AK
279045
THRISSUR
printed without any key and its like a text and the rest is outputted correctly,but i need to print all of with the array keys.I know this is because the print_r($prize) first iteration contains only one sub array under prizedetails.How can i solve it?
UPDATE
Code for parsing xml
$data=json_decode(json_encode((array)simplexml_load_string($lottery)),1);
Your code is behaving correctly. You need to alter the code such that "IF" prizeticket has one item, it does one thing, "ELSE" it does something else.
if(!isset($prize['prizeticket'][0])) // It has one item
print_r($prize['prizeticket']);
else foreach($prize['prizeticket'] as $ticket)
print_r($ticket);
This is a good place to use a function. Suppose it was named `handle_a_ticket($ticket)', you would replace print_r with handle_a_ticket.
My first reaction is that the XML has a typo.
[prizeticket] => Array
(
[seriesname] => AK
[digit] => 279045
[district] => THRISSUR
)
Should instead look like:
[prizeticket] => Array
(
[0] => Array
(
[seriesname] => AK
[digit] => 279045
[district] => THRISSUR
)
If you have some requirement that you absolutely must leave the XML the way it is, I'd suggest checking for the existence of index 0 like this:
foreach($temp as $tickets) {
if(!isset($tickets[0]) {
//do something with $ticket
}
else {
foreach($tickets as $ticket) {
//do something with $ticket
}
}
Finally i found the solution based on #RiggsFolly's comments.
$xml=simplexml_load_file('AK-194.xml');
foreach($xml->prizes as $prize){
foreach($prize->prizedetails as $details){
foreach($details->prizeticket as $tickets){
print_r($tickets);
}
}
}