2

My array:

$array = array('name'=>'test','server'=>'zangarmarsh','fields'=>'items,stats');
$type = 'character';
$r = $client->fetch($type,$array);
echo '<pre>';
print_r($r);
echo '</pre>';

The output which I get out of the array:

 Array
    (
        [result] => Array
            (
                [lastModified] => 1464923915000

                [items] => Array
                    (
                        [averageItemLevel] => 710

                        [head] => Array
                            (
                                [id] => 125899

                                [tooltipParams] => Array
                                    (
                                        [transmogItem] => 71356

                                    )

                                [bonusLists] => Array
                                    (
                                    )        
                            )

                        [neck] => Array
                            (
                                [id] => 127976

                                [tooltipParams] => Array
                                    (

                                        [upgrade] => Array
                                            (

                                                [itemLevelIncrement] => 0
                                            )

                                        [timewalkerLevel] => 100
                                    )

I know i need to use foreach, but i´m a bit overtaxed. If I use this i get the output of the [result] array, but how can i get the informations out of the other ones.

    foreach ($r as $v1) {
        foreach ($v1 as $v2) {
            echo "$v2\n";
        }
    }
Hertus
  • 147
  • 2
  • 11
  • The most dynamic way to extract what you want from a deep array is probably by using a recursive function / array iterator which will traverse an array and you just set up a mechanism to extract the part you want. – Rasclatt Jun 03 '16 at 20:24
  • 1
    You have arrays nested at least 5 layers deep, which means you'd need at least 5 foreach loops to handle them all. and since the nesting is arbitrary, you'd need extra logic to detect if the `$v1`, `$v2`, etc.. are actually arrays that can be iterated. – Marc B Jun 03 '16 at 20:26
  • @Hertus From my understanding, you are just not sure how to easily extract portions of the array? Is that the issue? – Rasclatt Jun 03 '16 at 20:36
  • Yes, Rasclatt i need specific output of them. – Hertus Jun 03 '16 at 20:37
  • Yeah @miken32 has the answer there in his link, which is what I was saying. It's a recursive array iterator (of the non-object type). – Rasclatt Jun 03 '16 at 20:38

2 Answers2

2

It depends on what you want to achieve. For example if you always have the same result structure: with a entry "result" and inside "result" a "modified" entry and any number of items, and you want to access the items you can do something like this:

$items = $r['result']['items'];
echo 'averageItemLevel: '.$items['averageItemLevel'].'\n';
echo 'head_id: '.$items['head']['id'].'\n';
foreach($items['head']['tooltipParams'] as $key => $value){
    echo 'head_'.$key.': '.$value.'\n';
}
user3190433
  • 375
  • 3
  • 7
  • First of all thanks. I tried your code to understand the concept, but i get confused output, which looks like that: `averageItemLevel: \nhead_id: \naverageItemLevel: \nhead_id: \naverageItemLevel: \nhead_id: \naverageItemLevel: \nhead_id:` – Hertus Jun 03 '16 at 20:47
  • @Hertus, how do you want the array to look? We are not clear what exactly you are after to. – Jose Manuel Abarca Rodríguez Jun 03 '16 at 20:48
  • i just want the specific output of the multidimensional arrays. For example i need the [id] out of the [head] array – Hertus Jun 03 '16 at 20:52
  • 1
    then use $r['result']['items']['head']['id']; updated my code above a little, – user3190433 Jun 03 '16 at 20:55
  • 1
    @Hertus, user3190433 is right, if you know the array and the key, why don't you just print it directly? As you can see, we are still confused by your question. – Jose Manuel Abarca Rodríguez Jun 03 '16 at 20:57
  • The code works now thanks user3190433. @ Jose Manuel Abarca Rodríguez if there is a simplier solution i would appreciate it, if you would give it to me. What do you mean with print it directly? – Hertus Jun 03 '16 at 21:01
0

Another solution. Next recursive function (search_keys) searches for two keys in a multidimensional array: array-key and item-key, if both found, displays the value :

<?php
$arr = Array
       ( "result" => Array
          ( "lastModified" => 1464923915000,
            "items" => Array
             ( "averageItemLevel" => 710,
               "head" => Array
                ( "id" => 125899,
                  "tooltipParams" => Array
                   ( "transmogItem" => 71356
                   ),
                  "bonusLists" => Array
                   (
                   )        
                ),
               "neck" => Array
                ( "id" => 127976,
                  "tooltipParams" => Array
                   ( "upgrade" => Array
                      ( "itemLevelIncrement" => 0
                      ),
                     "timewalkerLevel" => 100
                   )
                )
             )
          )
    );

search_keys( $arr,"head","id" );
search_keys( $arr,"tooltipParams","transmogItem" );
search_keys( $arr,"tooltipParams","timewalkerLevel" );

function search_keys ( $arr,$array_key,$item_key )
{ $keys = array_keys( $arr ); // GET ALL KEYS FROM CURRENT ARRAY.
  foreach ( $keys as $key )
    if ( gettype( $arr[ $key ] ) == "array" ) // IF CURRENT KEY IS ARRAY
       if ( ( $key == $array_key ) && // IF CURRENT KEY IS ARRAY_KEY, AND
            ( array_key_exists( $item_key,$arr[ $key ] ) ) ) // ARRAY CONTAINS ITEM
         echo $arr[ $key ][ $item_key ] . "<br/>"; // DISPLAY VALUE.
    else search_keys( $arr[ $key ],$array_key,$item_key ); // ENTER SUB-ARRAY.
}
?>