Count values in multidimensional array and save the result as new array - php

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)

Related

remove array by matching two array

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.

Convert php array to select box options

I have an input category array with childs as follows
I want to convert it using a recursive function to another array like
OUTPUT NEEDED
['1'=>'fashion', '10' => 'fashion > women','23'=> 'fashion > women > clothing','29'=> 'fashion > women > clothing > dresses' ... and so on ]
My purpose is to use output array to populate select box options to adda category.
INPUT ARRAY
Array
(
[1] => Array
(
[id] => 1
[name] => fashion
[parent_id] => 0
[childs] => Array
(
[10] => Array
(
[id] => 10
[name] => women
[parent_id] => 1
[childs] => Array
(
[23] => Array
(
[id] => 23
[name] => clothing
[parent_id] => 10
[childs] => Array
(
[29] => Array
(
[id] => 29
[name] => dresses
[parent_id] => 23
[childs] => Array
(
)
)
[30] => Array
(
[id] => 30
[name] => jumpsuits
[parent_id] => 23
[childs] => Array
(
)
)
)
)
[24] => Array
(
[id] => 24
[name] => bags & accessories
[parent_id] => 10
[childs] => Array
(
)
)
[25] => Array
(
[id] => 25
[name] => shoes
[parent_id] => 10
[childs] => Array
(
)
)
[26] => Array
(
[id] => 26
[name] => watches
[parent_id] => 10
[childs] => Array
(
)
)
[27] => Array
(
[id] => 27
[name] => jewelery
[parent_id] => 10
[childs] => Array
(
)
)
[28] => Array
(
[id] => 28
[name] => eye-wear
[parent_id] => 10
[childs] => Array
(
)
)
)
)
[11] => Array
(
[id] => 11
[name] => men
[parent_id] => 1
[childs] => Array
(
)
)
[12] => Array
(
[id] => 12
[name] => kids
[parent_id] => 1
[childs] => Array
(
)
)
[13] => Array
(
[id] => 13
[name] => sports
[parent_id] => 1
[childs] => Array
(
)
)
[14] => Array
(
[id] => 14
[name] => bags
[parent_id] => 1
[childs] => Array
(
)
)
[15] => Array
(
[id] => 15
[name] => eyewear
[parent_id] => 1
[childs] => Array
(
)
)
[16] => Array
(
[id] => 16
[name] => watches & jewelery
[parent_id] => 1
[childs] => Array
(
)
)
)
)
[2] => Array
(
[id] => 2
[name] => supermarket
[parent_id] => 0
[childs] => Array
(
[17] => Array
(
[id] => 17
[name] => food & beverages
[parent_id] => 2
[childs] => Array
(
[31] => Array
(
[id] => 31
[name] => breakfast
[parent_id] => 17
[childs] => Array
(
)
)
[32] => Array
(
[id] => 32
[name] => snacks
[parent_id] => 17
[childs] => Array
(
)
)
)
)
[18] => Array
(
[id] => 18
[name] => dairy products
[parent_id] => 2
[childs] => Array
(
)
)
[19] => Array
(
[id] => 19
[name] => beauty
[parent_id] => 2
[childs] => Array
(
)
)
[20] => Array
(
[id] => 20
[name] => homecare
[parent_id] => 2
[childs] => Array
(
)
)
[21] => Array
(
[id] => 21
[name] => baby world
[parent_id] => 2
[childs] => Array
(
)
)
[22] => Array
(
[id] => 22
[name] => pet world
[parent_id] => 2
[childs] => Array
(
)
)
)
)
[3] => Array
(
[id] => 3
[name] => electronics
[parent_id] => 0
[childs] => Array
(
[33] => Array
(
[id] => 33
[name] => laptops
[parent_id] => 3
[childs] => Array
(
)
)
[34] => Array
(
[id] => 34
[name] => television
[parent_id] => 3
[childs] => Array
(
)
)
)
)
[4] => Array
(
[id] => 4
[name] => mobiles & tablets
[parent_id] => 0
[childs] => Array
(
[35] => Array
(
[id] => 35
[name] => mobiles
[parent_id] => 4
[childs] => Array
(
)
)
[36] => Array
(
[id] => 36
[name] => tablets
[parent_id] => 4
[childs] => Array
(
)
)
)
)
[5] => Array
(
[id] => 5
[name] => baby & toys
[parent_id] => 0
[childs] => Array
(
)
)
[6] => Array
(
[id] => 6
[name] => home
[parent_id] => 0
[childs] => Array
(
)
)
[7] => Array
(
[id] => 7
[name] => perfumes & beauty
[parent_id] => 0
[childs] => Array
(
)
)
[8] => Array
(
[id] => 8
[name] => sports & fitness
[parent_id] => 0
[childs] => Array
(
)
)
[9] => Array
(
[id] => 9
[name] => automotive
[parent_id] => 0
[childs] => Array
(
)
)
)
I need an output array with key,the category id
You can create array by this way:
$array = array(
'category_1' => 'value',
'category_2' => array(
'subcategory_1' => 'value',
'subcategory_2' => 'value',
),
);

having difficulty to get style id from edmunds api

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'].

Manipulate a Multi-Multi-Dimensional PHP Array into a nice Multi-Dimensional Array

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);

CakePHP and Set::combine

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);

Categories