Wednesday 15 September 2010

list specific key/values in multidimensional array using php -


I have only read-only multidimensional companion array that lets me remove specific values ​​to produce output with some values is required.

The array looks like this:

  array ('dogs' => array (0 = & gt; array ('name' = & gt; 'Big' Array ('0'> 'Dogs',' 0 '' Array (1)> Array ('name' => '3 legged dogs',' Recoverys' => Array (0 = & '= & Gt;' spots', 'id' = & gt; '12 ',' owner '= & gt; sally', 'IsaBiter' =>, incorrect),),  < / Pre> 

etc ..

and nested forward loop array which can print all the values ​​of the array as follows:

  echo "& lt; ul & Gt; "; foreach ($ myArray $ arr1 => $ val1) {foreach ($ val1 as $ arr2 = & gt; $ Val2) {foreach ($ val2 as val3) {resonant "lift; ltd;" $ val3. "& Lt; ul & gt;"; foreach ($ val3 = $ val4 = $ val4 as ) {Foreign currency ($ val5 $ arr5 = & gt; $ val5) {echo "
  • ". $ Val5; foreign currency ($ arr5 = $ 6 = $ val6) {// echo $ val6; Foreign currency ($ val7 $ arr7 = & gt; $ Val7) {echo $ val7 ""} // dump details)} echo "";}}} Echo "& lt; / Ul & gt; "; echo" & lt; / Li & gt; "; I would ideally want to be 1) I do not want to exclude values ​​that I do not want to show (id, some array level names etc, currently it shows all) and 2 ) Display specific (i.e. name, owner, IsaBiter) so I can cleaner the results, such formats: Dog Dogs Retrievers Big Dogs Name: Fido Owner: Billy IsaBiter: True Name : Spot owner: Sally IsaBiter: false

    Here is the start of a recursive function that comes close to match you want.

      function displayArray ($ array, $ level = 0) { // This filter determines which level will be displayed $ visible = in_array ($ level, array (0,2,4, 6)); if ($ view) {echo ' ul & gt;'; Foreign currency ($ array = $ gt; $ value as the $ key) {if (is_array ($ value)) echo ' li & gt; copy; $ copy; displayArray ($ value, $ level + 1); echo & lt; / li & gt; ';} Else {// defines this filter Which non-array key $ visible = $ level == 6 and in_array ($ key, array ('name', 'owner', 'IsaBiter') is displayed); If ($ visible) {if (is_bool ($ value)) is $ value = $ value? "right wrong"; Echo & lt; Li & gt; '; Echo "$ key: $ value"; Echo & lt; / Li & gt; '; }}} Echo & lt; / Ul & gt; '; } Else {// If no level is visible, then we still try Arrayers foreach ($ array = $ as $ value) and if (is_array ($ value)) displayArray ( $ Value, $ level + 1); }}   

    I have shown how you can filter specific levels and specific keys, but you will probably need to get the exact output you showed.

    First of all, in your example you have only shown "retiovisers", although "FIOS" and "Spot" are actually two separate "retivisers" araaz (4 Legged Retivisers and 3 Legs Retievers) Are from I'm not sure if this is deliberate, and if so, then how well that rule should be.

    Secondly, you have the "Big dogs" displayed as it is the key of the top-level array, when in fact it is the "name" value of one of the children of objects in that array is. Then, if he is deliberately, then I am not sure what the rules are for deciding what you want to display and how.

    If this answer is not enough for you to understand yourself, you will need to provide more sample data and sample output so that I can determine what you are trying to achieve .

  • No comments:

    Post a Comment