-2

I have these 2 arrays $fonts['google'] and $data['value'] with the following content:

var_dump ($fonts['google']) outputs
array(4) {
    [0]=> array(3) { ["family"]=> string(7) "ABeeZee" ["variants"]=> array(2) { [0]=> string(7) "regular" [1]=> string(6) "italic" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } } 
    [1]=> array(3) { ["family"]=> string(4) "Abel" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } } 
    [2]=> array(3) { ["family"]=> string(13) "Abril Fatface" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(2) { [0]=> string(5) "latin" [1]=> string(9) "latin-ext" } } 
    [3]=> array(3) { ["family"]=> string(8) "Aclonica" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } }
}


var_dump ($data['value']) outputs
array(4) {
    ["size"]=>  int(17) 
    ["family"]=>  string(3) "Exo"
    ["style"]=>  string(3) "200"
    ["subsets"]=>  string(5) "latin"
}       

Now I get the $data['value']['family'] = 'Abel' from my database.

Questions:

  • How can I get the ['variants'] for the given $data['value']['family'] value?
  • How can I get the index in $fonts['google'] for the sub-array where the $data['value']['family'] value is?
thednp
  • 4,401
  • 4
  • 33
  • 45
  • possible duplicate of [PHP - Accessing Multidimensional Array Values](http://stackoverflow.com/questions/17139453/php-accessing-multidimensional-array-values) – Rizier123 Feb 21 '15 at 15:37
  • You are talking about 2 different arrays! (`$fonts` , `$data`) – Rizier123 Feb 21 '15 at 15:39
  • OK. I will edit the question, please don't rate me down, I am learning things, I'm a total n00b. Thanks – thednp Feb 21 '15 at 15:45
  • @Rizier123 I updated the question. Please check and provide a solution, then rate up my question :) – thednp Feb 21 '15 at 16:56
  • While it's possible to answer your question as asked (using a loop), the fact that it's not super simple is a hint that your data structure is not optimal. If your $fonts['google'] array used the family names as indexes, rather than using numeric indexes, this would better match how you need to access the data! – RobP Feb 21 '15 at 17:32
  • @RobP go for it, I will give you the bounty :) – thednp Feb 21 '15 at 17:55

1 Answers1

1

PHP supports Associative Arrays which let you use a (string) key rather than a numeric index for each element. These arrays are akin to javascript objects, Objective-C dictionaries, java HashMaps, etc. That makes scenarios like this easy. Do you have control over building the original data array? If you can refactor your storage, set up the arrays like this:

$fonts['google'] = [
    ["ABeeZee"] => [
        ["variants"]=>["regular", "italic"],
        ["subsets"]=>["latin"]
    ],
    ["Abel"] => [
        ["variants"]=>["regular"],
        ["subsets"]=>["latin"]
    ],
    ["Abril Fatface"] => [
        ["variants"]=>["regular"],
        ["subsets"]=>["latin", "latin-ext"]
    ],
    ["Aclonica"] => [
        ["variants"]=>["regular"],
        ["subsets"]=>["latin"]
    ]
]

extra credit: if you have the original data as in the post, you could convert it:

$newArray = array(); // or just [] in PHP >= 5.3 I believe
foreach($fonts['google'] as $index=>$fontArray) {
    $newArray[$fontArray['family']] = $fontArray;
    // this leaves a redundant copy of the family name in the subarray
    unset $newArray[$fontArray['family']]['family']; // if you want to remove the extra copy
}

Then it becomes trivial. Given a font family name, you just access $fonts['google'][$fontFamilyName] (or $newArray[$fontFamilyName]) using the family name as the array index.

RobP
  • 9,144
  • 3
  • 20
  • 33
  • Thanks @RobP but you didn't answer my question. You seem to be talking to your computer and not to me :) – thednp Feb 21 '15 at 18:35
  • Can you explain how it doesn't answer your question? I'm proposing a solution that reorganizes the data so you can access the sub-array just using an array index. Would you rather see a solution that loops through the array as you have it stored every time you access it? This is why I asked if you receive the data from somewhere else or you have control over how the data's organized. – RobP Feb 21 '15 at 18:38
  • I don't understand the code until I see what it does. Thanks @RobP, this saved my day :) – thednp Feb 21 '15 at 18:48