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.
Related
Array ( [make] => Array ( [id] => 200000201 [name] => Nissan [niceName] => nissan ) [model] => Array ( [id] => Nissan_Pathfinder [name] => Pathfinder [niceName] => pathfinder ) [engine] => Array ( [equipmentType] => ENGINE [availability] => USED [cylinder] => 6 [size] => 3.5 [configuration] => V [fuelType] => regular unleaded [horsepower] => 260 [type] => gas [code] => VQ35DE [rpm] => Array ( [horsepower] => 6400 [torque] => 4400 ) [valve] => Array ( [gear] => double overhead camshaft ) ) [transmission] => Array ( [id] => 200405221 [name] => continuously variableA [equipmentType] => TRANSMISSION [availability] => STANDARD [automaticType] => Continuously variable [transmissionType] => AUTOMATIC [numberOfSpeeds] => continuously variable ) [drivenWheels] => front wheel drive [numOfDoors] => 4 [options] => Array ( ) [colors] => Array ( [0] => Array ( [category] => Interior [options] => Array ( [0] => Array ( [id] => 200439557 [name] => Almond Leather [equipmentType] => COLOR [availability] => USED ) ) ) [1] => Array ( [category] => Exterior [options] => Array ( [0] => Array ( [id] => 200439553 [name] => Cayenne Red Metallic [equipmentType] => COLOR [availability] => USED ) ) ) ) [manufacturerCode] => 25513 [price] => Array ( [baseMSRP] => 34850 [baseInvoice] => 31770 [deliveryCharges] => 845 [usedTmvRetail] => 22236 [usedPrivateParty] => 20769 [usedTradeIn] => 19116 [estimateTmv] => ) [categories] => Array ( [market] => Crossover [EPAClass] => Sport Utility Vehicles [vehicleSize] => Large [crossover] => Car [primaryBodyType] => SUV [vehicleStyle] => 4dr SUV [vehicleType] => SUV ) [vin] => 5N1AR2MNXDC676161 [squishVin] => 5N1AR2MNDC [years] => Array ( [0] => Array ( [id] => 100539157 [year] => 2013 [styles] => Array ( [0] => Array ( [id] => 200439517 [name] => SL 4dr SUV (3.5L 6cyl CVT) [submodel] => Array ( [body] => SUV [modelName] => Pathfinder SUV [niceName] => suv ) [trim] => SL ) ) ) ) [matchingType] => SQUISHVIN [MPG] => Array ( [highway] => 26 [city] => 20 ) ) NissanPathfinder2013
from this array I get make, model, and year like this:
$ed_json=file_get_contents($ed_url);
$ed_array= json_decode($ed_json, true);
print_r($ed_array);
$make=$ed_array['make']['name'];
$model=$ed_array['model']['name'];
$year=$ed_array['years']['0']['year'];
echo $make;
echo $model;
echo $year;
now I am doing the same thing to get style id but I cannot get it.
this is the code for style id:
$id=$ed_array['styles']['0']['id'];
echo $id;
Array for years :
[years] => Array (
[0] => Array (
[id] => 100539157
[year] => 2013
[styles] => Array (
[0] => Array (
[id] => 200439517
[name] => SL 4dr SUV (3.5L 6cyl CVT)
[submodel] => Array (
[body] => SUV
[modelName] => Pathfinder SUV
[niceName] => suv
)
[trim] => SL
)
)
)
)
As you can see [styles][0][id] is in [years][0].
So, to get style id you can do :
$style = $ed_array['years'][0]['styles'][0]['id'];
In your case this should be $ed_array['years'][0]['styles'][0]['id'].
I have a working PHP app running in Bluemix that I want to extend to call a RESTful service (Insights for Twitter). I've been able to call the service, retrieve the json body, and use json_decode as follows to create an array:
$insightList = json_decode($guzzleResponse ->getBody(), true);
However, I can't figure out how to get access to the field I'm interested in. I've searched for solution on the web and tried a few approaches that looked promising, but when I tried to integrate them, I couldn't get them to work. I'm a bit of a PHP novice, so if something doesn't work I'm not sure how to proceed.
The json structure is quite complex, with three top-level arrays - search, tweets, and next. I'm interested in the second of these, tweets. It's a pretty complex array - there is one entry per tweet. The field I want right now is tweets.cde.message.body. You can find the full schema here: https://cdeservice.eu-gb.mybluemix.net/rest-api/#!/messages/getTweets
This is the code I have so far:
foreach($insightList as $cde) {
foreach($cde as $message) {
$insight = $message['body'];
if(strlen($insight) > 60) {
$posts[] = array(
'id' => 99999999,
//Temp; remove links from the text
'text' => $insight,
'category' => $insightCategory,
'image' => 'false'
);
}
}
}
Here's a print_r of $insightList:
[search] => Array (
[results] => 28
[current] => 28
)
[tweets] => Array (
[0] => Array (
[cde] => Array (
[author] => Array (
[gender] => male
[parenthood] => Array (
[isParent] => unknown
)
[location] => Array (
[country] =>
)
[maritalStatus] => Array (
[isMarried] => unknown
)
)
[content] => Array (
[sentiment] => Array (
[evidence] => Array ( )
[polarity] => NEUTRAL
)
)
)
[message] => Array (
[postedTime] => 2015-01-13T09:42:16.000Z
[verb] => share
[link] => http://twitter.com/zWDOM/statuses/554936456477933569
[generator] => Array (
[displayName] => Twitter Web Client
[link] => http://twitter.com
)
[body] => RT #VisualSuccess: "Mainframe & Cloud" Magazine wurde soeben publiziert! http://www.twitter.com #zWDOM #Rocket #JohnKnutson_IBM
[favoritesCount] => 0
[objectType] => activity
[actor] => Array (
[summary] => Seit über 25 Jahren im Mainframebereich, derzeit als Senior Consultant und IT Architekt für zEnterprise und Projektmanager beim IBM BP Cancom in Köln
[image] => https://pbs.twimg.com/profile_images/424202233463308288/XQquUcnh_normal.jpeg
[statusesCount] => 2309
[utcOffset] => 3600
[languages] => Array (
[0] => de
)
[preferredUsername] => zWDOM
[displayName] => Willi Domroese
[postedTime] => 2009-12-17T01:39:25.000Z
[link] => http://www.twitter.com/zWDOM
[verified] =>
)
[provider] => Array (
[displayName] => Twitter
[link] => http://www.twitter.com
[objectType] => service
)
[twitter_filter_level] => medium
[twitter_entities] => Array (
[urls] => Array (
[0] => Array (
[display_url] => ln.is/paper.li/visua…
[indices] => Array (
[0] => 77
[1] => 99
)
[expanded_url] => http://ln.is/paper.li/visualsucce/7zYNk
[url] => http://www.twitter.com
)
)
[hashtags] => Array ( )
[user_mentions] => Array (
[0] => Array (
[indices] => Array (
[0] => 3
[1] => 17
)
[screen_name] => VisualSuccess
[id_str] => 213337792
[name] => Predrag Gasic
[id] => 213337792
)
[1] => Array (
[indices] => Array (
[0] => 101
[1] => 107
)
[screen_name] => zWDOM
[id_str] => 97334013
[name] => Willi Domroese
[id] => 97334013
)
[2] => Array (
[indices] => Array (
[0] => 108
[1] => 115
)
[screen_name] => Rocket
[id_str] => 870584947
[name] => Rocket Software
[id] => 870584947
)
[3] => Array (
[indices] => Array (
[0] => 116
[1] => 132
)
[screen_name] => JohnKnutson_IBM
[id_str] => 16452310
[name] => John Knutson
[id] => 16452310
)
)
[trends] => Array ( )
[symbols] => Array ( )
)
[twitter_lang] => de
[id] => tag:search.twitter.com,2005:554936456477933569
[retweetCount] => 1
[gnip] => Array (
[urls] => Array (
[0] => Array (
[expanded_url] => http://linkis.com/paper.li/visualsucce/7zYNk
[expanded_status] => 200
[url] => http://www.twitter.com
)
)
[language] => Array (
[value] => de
)
)
[object] => Array (
[postedTime] => 2015-01-13T08:04:48.000Z
[verb] => post
[link] => http://twitter.com/VisualSuccess/statuses/554911928527888384
[generator] => Array (
[displayName] => Linkis.com
[link] => http://linkis.com
)
[body] => "Mainframe & Cloud" Magazine wurde soeben publiziert! http://www.twitter.com ://www.twitter.com
[objectType] => activity
[actor] => Array (
[summary] => Ù† (N), Wirtschaftsinformatiker | SAP Consultant Logistics #bigdata #appdevelopment #webdesign #eCommerce #SocialMedia #contentmarketing #SmartHome #Journal
[image] => https://pbs.twimg.com/profile_images/2841607223/959b0d23646b1f24bd7b70deac160e2f_normal.jpeg
[statusesCount] => 14185
[utcOffset] => 3600
[languages] => Array (
[0] => de
)
[preferredUsername] => VisualSuccess
[displayName] => Predrag Gasic
[postedTime] => 2010-11-08T17:19:27.000Z
[link] => http://www.twitter.com/VisualSuccess
[verified] =>
)
[provider] => Array (
[displayName] => Twitter
[link] => http://www.twitter.com
[objectType] => service
)
[twitter_filter_level] => low
[twitter_entities] => Array (
[urls] => Array (
[0] => Array (
[display_url] => ln.is/paper.li/visua…
[indices] => Array (
[0] => 58
[1] => 80
)
[expanded_url] => http://ln.is/paper.li/visualsucce/7zYNk
[url] => http://www.twitter.com
)
)
[hashtags] => Array ( )
[user_mentions] => Array (
[0] => Array (
[indices] => Array (
[0] => 82
[1] => 88
)
[screen_name] => zWDOM
[id_str] => 97334013
[name] => Willi Domroese
[id] => 97334013
)
[1] => Array (
[indices] => Array (
[0] => 89
[1] => 96
)
[screen_name] => Rocket
[id_str] => 870584947
[name] => Rocket Software
[id] => 870584947
)
[2] => Array (
[indices] => Array (
[0] => 97
[1] => 113
)
[screen_name] => JohnKnutson_IBM
[id_str] => 16452310
[name] => John Knutson
[id] => 16452310
)
)
[trends] => Array ( )
[symbols] => Array (
)
)
)
)
)
Answers to this question would be greatly appreciated
Iterating $insightList will not get you to the cde level. And you don't actually want the cde; you want the message.
Just iterate the tweets. You don't need a foreach to get keyed info from a PHP array.
foreach($insightList['tweets'] as $tweet) {
$insight = $tweet['message']['body'];
if(strlen($insight) > 60) {
$posts[] = array(
'id' => 99999999,
//Temp; remove links from the text
'text' => $insight,
'category' => $insightCategory,
'image' => 'false'
);
}
}
It looks to me from your print_r that what you want is at:
echo $insightList[0]['message']['body'];
When there's a section like this [some_key], then 'some_key' is an associative array key, and you can access those elements be they additional arrays or a value, using the key name, as I illustrated here.
I know this is considered a somewhat basic PHP Array question but i'm running on 35 hours no sleep and I really just need to finish this up as quickly as posibble so I can get to sleep...sorry just being honest!
In PHP I have this variable $design_values
If I print_r($design_values); this ARRAY it spits out what is show below. It is "Desing" database records.
In this case there is 2 Design records which make up the first 2 Array keyys the 0 and 1
In the application there can be any number of Designs from 0 up to any number.
Now under the 2 Design Records are 24 more Array keys for each of the 2 Design Arrays.
These 24 Array keys are numbered 0 to 23.
Now under each of the 24 Array keys, is 2 keys. One named name and the other named value.
I need to take the Array $design_values and create a new Array of that array. The new Array should be formatted much better amd easy to work with.
So the name and value keys should make up a key => value. The New array should look more like this....
The reason the current Array is a complete nightmare is because that is the Format I get it in from an existing library which returns this Data from an API call.
If someone can help me to manipulate this Array into the desired Array I will be grateful! I have been messing with it for 2 hours with no luck.
Desired New Array Format :
Array
(
[0] => Array
(
['assigned_user_name'] => 'Jason Administrator',
['modified_by_name'] => 'Jason Administrator',
['created_by_name'] => 'Jason Administrator',
['id'] => '4c5c3c08-2b14-9f9c-6cee-542c56cac7b1',
['date_entered'] => '2014-10-01 19:29:32',
....continued for all 24 record items
),
[1] => Array
(
['assigned_user_name'] => 'Jason Administrator',
['modified_by_name'] => 'Jason Administrator',
['created_by_name'] => 'Jason Administrator',
['id'] => '4c5c3c08-2b14-9f9c-6cee-542c56cac7b1',
['date_entered'] => '2014-10-01 19:29:32',
....continued for all 24 record items
)
)
Current Array Format :
Array
(
[0] => Array
(
[0] => Array
(
[name] => assigned_user_name
[value] => Jason Administrator
)
[1] => Array
(
[name] => modified_by_name
[value] => Jason Administrator
)
[2] => Array
(
[name] => created_by_name
[value] => Jason Administrator
)
[3] => Array
(
[name] => id
[value] => 4c5c3c08-2b14-9f9c-6cee-542c56cac7b1
)
[4] => Array
(
[name] => name
[value] => test
)
[5] => Array
(
[name] => date_entered
[value] => 2014-10-01 19:29:32
)
[6] => Array
(
[name] => date_modified
[value] => 2014-10-01 19:29:32
)
[7] => Array
(
[name] => modified_user_id
[value] => 1
)
[8] => Array
(
[name] => created_by
[value] => 1
)
[9] => Array
(
[name] => description
[value] =>
)
[10] => Array
(
[name] => deleted
[value] => 0
)
[11] => Array
(
[name] => assigned_user_id
[value] => 1
)
[12] => Array
(
[name] => chann_channelqms_id_c
[value] =>
)
[13] => Array
(
[name] => channelqms
[value] =>
)
[14] => Array
(
[name] => design_name
[value] =>
)
[15] => Array
(
[name] => design_number
[value] =>
)
[16] => Array
(
[name] => overall_height
[value] =>
)
[17] => Array
(
[name] => overall_width
[value] =>
)
[18] => Array
(
[name] => show_to_customer
[value] => 1
)
[19] => Array
(
[name] => uploadfile
[value] => 2014-09-29_21-57-50.png
)
[20] => Array
(
[name] => nam_channelletterqms_nam_channelletterqms_designs_name
[value] => Test
)
[21] => Array
(
[name] => price_c
[value] =>
)
[22] => Array
(
[name] => shipping_c
[value] =>
)
[23] => Array
(
[name] => totalprice_c
[value] =>
)
)
[1] => Array
(
[0] => Array
(
[name] => assigned_user_name
[value] => Jason Administrator
)
[1] => Array
(
[name] => modified_by_name
[value] => Jason Administrator
)
[2] => Array
(
[name] => created_by_name
[value] => Jason Administrator
)
[3] => Array
(
[name] => id
[value] => 86f21f44-4b21-1826-3592-542c59e4be66
)
[4] => Array
(
[name] => name
[value] => fdtgrfdhg
)
[5] => Array
(
[name] => date_entered
[value] => 2014-10-01 19:41:54
)
[6] => Array
(
[name] => date_modified
[value] => 2014-10-19 19:30:45
)
[7] => Array
(
[name] => modified_user_id
[value] => 1
)
[8] => Array
(
[name] => created_by
[value] => 1
)
[9] => Array
(
[name] => description
[value] =>
)
[10] => Array
(
[name] => deleted
[value] => 0
)
[11] => Array
(
[name] => assigned_user_id
[value] => 1
)
[12] => Array
(
[name] => chann_channelqms_id_c
[value] =>
)
[13] => Array
(
[name] => channelqms
[value] =>
)
[14] => Array
(
[name] => design_name
[value] => design name
)
[15] => Array
(
[name] => design_number
[value] => 313
)
[16] => Array
(
[name] => overall_height
[value] => 22
)
[17] => Array
(
[name] => overall_width
[value] => 22
)
[18] => Array
(
[name] => show_to_customer
[value] => 1
)
[19] => Array
(
[name] => uploadfile
[value] => 2014-09-29_21-57-50.png
)
[20] => Array
(
[name] => nam_channelletterqms_nam_channelletterqms_designs_name
[value] => Test
)
[21] => Array
(
[name] => price_c
[value] =>
)
[22] => Array
(
[name] => shipping_c
[value] =>
)
[23] => Array
(
[name] => totalprice_c
[value] =>
)
)
)
If you can't change it at the source then here is one way (PHP >= 5.5.0 needed for array_column):
foreach($design_values as $key => $values) {
$result[$key] = array_combine(
array_column($values, 'name'),
array_column($values, 'value'));
}
Or possibly, and easier:
foreach($design_values as $key => $values) {
$result[$key] = array_column($values, 'value', 'name');
}
Our use the PHP Implementation of array_column
You should probably create the array you want when making the first array, but if you don't have control over that and just want to conver then something like this should work:
$newArray = array();
foreach($oldArray as $row){
$tmp = array();
foreach($row as $values){
$tmp[$values['name']] = $values['value'];
}
$newArray[] = $tmp;
}
print_r($newArray);
Here is my multidim. array:
Array
(
[0] => Array
(
[id] => 1
[language] => English
)
[1] => Array
(
[id] => 1
[language] => English
)
[2] => Array
(
[id] => 1
[language] => English
)
[3] => Array
(
[id] => 1
[language] => English
)
[4] => Array
(
[id] => 1
[language] => English
)
[5] => Array
(
[id] => 1
[language] => English
)
[6] => Array
(
[id] => 1
[language] => English
)
[7] => Array
(
[id] => 1
[language] => English
)
[8] => Array
(
[id] => 1
[language] => English
)
[9] => Array
(
[id] => 1
[language] => English
)
[10] => Array
(
[id] => 1
[language] => English
)
[11] => Array
(
[id] => 1
[language] => English
)
[12] => Array
(
[id] => 1
[language] => English
)
[13] => Array
(
[id] => 1
[language] => English
)
[14] => Array
(
[id] => 1
[language] => English
)
[15] => Array
(
[id] => 1
[language] => English
)
[16] => Array
(
[id] => 1
[language] => English
)
[17] => Array
(
[id] => 1
[language] => English
)
[18] => Array
(
[id] => 1
[language] => English
)
[19] => Array
(
[id] => 1
[language] => English
)
[20] => Array
(
[id] => 1
[language] => English
)
[21] => Array
(
[id] => 1
[language] => English
)
[22] => Array
(
[id] => 1
[language] => English
)
[23] => Array
(
[id] => 1
[language] => English
)
[24] => Array
(
[id] => 2
[language] => Italian
)
[25] => Array
(
[id] => 1
[language] => German
)
[26] => Array
(
[id] => 1
[language] => German
)
[27] => Array
(
[id] => 1
[language] => German
)
[28] => Array
(
[id] => 1
[language] => German
)
[29] => Array
(
[id] => 1
[language] => German
)
[30] => Array
(
[id] => 1
[language] => German
)
[31] => Array
(
[id] => 1
[language] => German
)
[32] => Array
(
[id] => 1
[language] => German
)
[33] => Array
(
[id] => 1
[language] => German
)
[34] => Array
(
[id] => 1
[language] => German
)
[35] => Array
(
[id] => 1
[language] => German
)
[36] => Array
(
[id] => 1
[language] => German
)
)
I know how to loop through the array and how to count values but this time I also need to check whether the [id] is in an array with multiple languages and it has to work fully dynamic for different queries.
The new array shall look like this:
Array (
[1] => Array(
"English" => 24,
"German" => 12
),
[2] => Array(
"Italian" => 1
)
)
$sorted_array = array("English" => 0, "German" => 0);
foreach($old_array as $a) {
if(isset($a['language']) && strlen($a['language']) >= 1) {
switch($a['language']) {
case 'English' :
$sorted_array['English']++;
break;
case 'German' :
$sorted_array['German']++;
break;
}
}
}
Just to get you going... And if you're real clever you can use array_keys to automatically populate your sorted_array() for you... (google it)
I need to get the data for each language in it's field as an array.
Probably the best approach in CakePHP will be Set::combine but can't get it working.
I can do it manually with foreach but I don't think that will be the best way.
Here is the example:
Array
(
[Article] => Array
(
[id] => 131
[title] => TEST
)
[titleTranslation] => Array
(
[0] => Array
(
[id] => 62
[locale] => eng
[model] => Article
[foreign_key] => 131
[field] => title
[content] => TEST
)
[1] => Array
(
[id] => 63
[locale] => fre
[model] => Article
[foreign_key] => 131
[field] => title
[content] => Salva
)
[2] => Array
(
[id] => 64
[locale] => rus
[model] => Article
[foreign_key] => 131
[field] => title
[content] => Пвет
)
)
)
into this array:
Array
(
[Article] => Array
(
[id] => 131
[title] => Array
(
[eng] => TEST
[fre] => Salva
[rus] => Пвет
)
)
.... the rest is not important
)
Solved-----
$translatedData = Set::combine($this->data['titleTranslation'], '{n}.locale', '{n}.content', '{n}.field');
$this->data['Article'] = array_merge($this->data['Article'], $translatedData);