Parse arrays in array by their key values

Discussion in 'PHP' started by trecords, Mar 2, 2012.

  1. #1
    Hi,

    I want to get value from this array, I only need to get words which has value N. This is array:
    $myarray = array (
      'feeler gauge' => 
      array (
        0 => 'h',
      ),
      'feeler' => 
      array (
        0 => 'N',
      ),
      'feeless' => 
      array (
        0 => 'A',
      ),
      'feelingful' => 
      array (
        0 => 'A',
      ),
      'feelinglessly' => 
      array (
        0 => 'v',
      ),
      'feelingless' => 
      array (
        0 => 'A',
      ),
      'feelingly' => 
      array (
        0 => 'v',
      ),
      'feelingness' => 
      array (
        0 => 'N',
      ),
      'feeling' => 
      array (
        0 => 'N',
        1 => 'A',
      ),
      'feel' => 
      array (
        0 => 'V',
        1 => 't',
        2 => 'i',
        3 => 'N',
      ),
    );
    PHP:
    Lets say I want to get 'feeler' which is second array and first value which contains N value in its array. Let me know plz how can I get that value.
     
    Solved! View solution.
    trecords, Mar 2, 2012 IP
  2. wsoulrc

    wsoulrc Member

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #2
    why not use a foreach and checks

    is array | if
     
    wsoulrc, Mar 2, 2012 IP
  3. trecords

    trecords Well-Known Member

    Messages:
    145
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #3
    I did something like this, but I can`t get array key. As you see this array contains values and for each value array with key and values. So i need to match last value for each array and I did it but can`t extract value name:
    foreach ($myarray as $array) {
    	$key = array_search('N', $array);
    	echo $key;
    }
    PHP:
     
    trecords, Mar 2, 2012 IP
  4. wsoulrc

    wsoulrc Member

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    sorry i am spanish and.. my inglish... jajajaja

     
    wsoulrc, Mar 2, 2012 IP
  5. #5
    I'm going to give you a sample idea of how parsing arrays within arrays works:

    foreach($myarray as $key => $value)
    {
        // $key is now 'feeler' or 'feeless' or etc
        // $value is now the array for 'feeler' or 'feeless' or etc (the $key)
        foreach($value as $inside_key => $inside_value)
        {
             // $inside_key is now 0, 1, 2, 3, etc 
             // $inside_value is now 'h', 'N', 'A', etc
        }
    }
    PHP:
    I'm sure you'll figure out the rest from here ;)
     
    CIScripts, Mar 2, 2012 IP