Jump to content

Automatically searching an array inside an array?


Gibbs

Recommended Posts

I haven't got any experience with most array functions so I'm finding it difficult to be creative. Anyway... I have an array that has multiple arrays inside it. I'm trying to find the value of each array. The problem is that I also need to know the name of the array that is in use. Heres an example of what I'm doing:

 

if ($arrayData[sect][TEX][0][AAA] == "A") { $ret_a = $ret_a."AAA "; }
if ($arrayData[sect][TEX][0][GGG] == "A") { $ret_a = $ret_a."GGG "; }
if ($arrayData[sect][TEX][0][bBB] == "A") { $ret_a = $ret_a."BBB "; }
if ($arrayData[sect][TEX][0][MMM] == "A") { $ret_a = $ret_a."MMM "; }
if ($arrayData[sect][TEX][0][CCC] == "A") { $ret_a = $ret_a."CCC "; }
if ($arrayData[sect][TEX][0][EEE] == "A") { $ret_a = $ret_a."EEE "; }
if ($arrayData[sect][TEX][0][VVV] == "A") { $ret_a = $ret_a."VVV "; }

 

That's a lot of code especially as I have to find out B, C and search another 7 of these. I'm probably not making any sense whatsoever but it's difficult to explain.

 

Basically I need to find out all the arrays inside $arrayData[sect][TEX][0], find it's value AND return the name of that array. This is being done from a live feed so I can't change the XML file unfortunately.

 

I can do this but 147 lines is a waste of time...

 

Any help appreciated!

Link to comment
Share on other sites

Array ( 
[sect] => Array (
[LLL] => Array ( 
[0] => Array 
( [CCC] => A [FFF] => A [VVV] => A [GGG] => A [EEE] => A [MMM] => E [bBB] => E ) )
[FFF] => Array (
[0] => Array ( 
[LLL] => A [GGG] => A [bBB] => N [MMM] => E [CCC] => N [EEE] => A [VVV] => A) )

 

That's an example of the array. I'm guessing its four-dimension?

 

Using the first part for LLL I need to echo the value of each (if it equals A) arrays value (like CCC) as well as the array name CCC.

 

The output would need be something like:

A CCC
A FFF
A VVV
A GGG
A EEE

Link to comment
Share on other sites

yeah, Im guessing its more like a 2d associative array, or 3d assoc.

 

here's a function I made alittle while back that you could modify to select data from your array.

<?php
// Select 1 field from an Array
function select_array($array,$id,$field) {

     return $array[$id][$field]; 

}

 

Regards ACE

Link to comment
Share on other sites

Thanks for the help. Managed to come up with this. Still long but cuts the code down.

 

<?php		
for ($x = 0; $x < 8; $x++)
{
$sectSel = "LLL";
switch($x)
{
	case 0: $partSearch = "LLL"; break;
	case 1: $partSearch = "FFF"; break;
	case 2: $partSearch = "GGG"; break;
	case 3: $partSearch = "BBB"; break;
	case 4: $partSearch = "MMM"; break;
	case 5: $partSearch = "CCC"; break;
	case 6: $partSearch = "EEE"; break;
	case 7: $partSearch = "VVV"; break;
}

if ($arrayData[sect][$sectSel][0][$partSearch] == "E")
{
	$eString[$sectSel] = $eString[$sectSel]." ".$partSearch; 
}
}
echo $eString[$sectSel];
?>

Link to comment
Share on other sites

This is as good as I can come up with based on what you've posted so far.  It looks to me like you have a list of associations like:

  ASSOC=>VALUE

  LLL => E

  TEX => A

 

Using those you want to search each $arrayData['sect'][ASSOC][0][name] to see if it's equal to VALUE.  In the following code I set up those associations in $arrSearch.  Then I just use a couple of foreach loops to find what you're looking for.  I store the results in $arrFound and just dump its contents as the last line.

 

<?php

  /**
   * Set up an association of which array after ['sect'] we want to search and
   * what value in that array we want to search for.
   */
  $arrSearch = Array(
                'TEX' => 'A',
                'LLL' => 'E'
               );
  $arrFound = Array(); // values that we found, empty for now

  // Now loop over each section that we want to search
  foreach($arrSearch as $sect => $search){
    $tmp = $arrayData['sect'][$sect][0]; // just a shorthand, makes it easier
                                         // to use
    foreach($tmp as $name => $value){
      if($value == $search){ // we found it
        $arrFound[] = Array(
                        'name' => $name, 'value' => $value
                      );
        break;
      }
    }
  }

  echo '<pre style="text-align: left;">' . print_r($arrFound, true) . '</pre>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.