0

i have array

Array('0284'=>array('name'='XX',
                    'inputs'=array(
                                array('sysname'=>'KEY_TO_MATCH1',....);
                                array('sysname'=>'KEY_TO_MATCH2',....);
                                  ),
      '0287'=>array('name'='YYY',
                    'inputs'=array(
                                array('sysname'=>'KEY_TO_MATCH3',....);
                                array('sysname'=>'KEY_TO_MATCH4',....);
                                )
      );

what i want is to search $_POST if the key sysname exist then return array 'name'..

example if $_POST['KEY_TO_MATCH1'] exist return XX

EDIT

array structure explanation:

array is result of fetching db table

plugins(id,name,user_id)

plugins_inputs(id,plugin_id(FK),sysname,label,value,extra_attrs)

what i do is to fetch all plugins and its inputs, then i separate them by pluigns_id

Array('plugins.id'=>array('name'='plugins.name',
                        'inputs'=array(//all rows from plugins_inputs where plugin_id=plugins.id
                                    array('sysname'=>'KEY_TO_MATCH1',....);
                                    array('sysname'=>'KEY_TO_MATCH2',....);
                                      )

plugins.id as key: to avoid duplicates as i cann't trust form.name

plugins.name : points to function name that handles these inputs

plugins_inputs : contain multiple rows of inputs

now i want to listen to $_POST case any of these inputs where submited then redirect it to that form specific function that handle it. $this->${plugins.name}->backend();

currently using

foreach($array as $id=>$val){
 foreach($val['inputs'] as $input)
  //$this->input->post is codeigniter help to handle $_POST[] if it doesnt exist it return false
   if($this->input->post([$input['sysname']])runbackend($val['name'],$id);
 }

but i was wondering if there is smarter way to do it.. using array_filter or array_map or something... }

2
  • Have you used in_array() Commented Feb 14, 2013 at 11:49
  • Why are 0287 and 0284 in 2 different arrays. If they are both keys with values and are unique put them in a single array (as key's with their array value pairs) to enable looping. Commented Feb 14, 2013 at 11:54

5 Answers 5

1

Try this one ...

foreach($data as $key => $d) 
{

  foreach($d as $key => $dat) 
  {

       if(in_array("$_POST['KEY_TO_MATCH1']",$dat['inputs'])
       {
         echo $d['name'];
       }
       else {
       echo "Not found";

       }
 }
}
Sign up to request clarification or add additional context in comments.

Comments

1

$child is the value that you want to match and $stack is the array to search.

function getParentStackComplete($child, $stack) {
    $return = array();
    foreach ($stack as $k => $v) {
        if (is_array($v)) {
            // If the current element of the array is an array, recurse it 
            // and capture the return stack
            $stack = getParentStackComplete($child, $v);

            // If the return stack is an array, add it to the return
            if (is_array($stack) && !empty($stack)) {
                $return[$k] = $stack;
            }
        } else {
            // Since we are not on an array, compare directly
            if ($v == $child) {
                // And if we match, stack it and return it
                $return[$k] = $child;
            }
        }
    }

    // Return the stack
    return empty($return) ? false: $return;
}

EXAMPLE:

$array = array(
    'balloon' => array(
        'red' => array(1 => 'Love', 'Valentine', 'Heart',),
        'green' => array(1 => 'Summertime', 'Hope',),
    ),
    'ribbon' => array(
        'yellow' => array(2 => 'Welcome',),
        'red' => array(3 => 'Love', 'Love',),
    ),
);
$c = getParentStackComplete('Love', $array);

RESULT

array
  'balloon' => 
    array
      'red' => 
        array
          1 => string 'Love' (length=4)
  'ribbon' => 
    array
      'red' => 
        array
          3 => string 'Love' (length=4)
          4 => string 'Love' (length=4)

Comments

1

First of all, sort your array structure out. Is this how it's supposed to look. I've also changed some of your = to =>, and ; to , in your code, as they are syntax errors in arrays.

$outerarray = array(
   '0284'=>array('name' => 'XX',
                 'inputs' => array(
                            array('sysname'=>'KEY_TO_MATCH1',....),
                            array('sysname'=>'KEY_TO_MATCH2',....),
                  )
           ),
   '0287'=>array('name' => 'YYY',
                 'inputs' => array(
                   array('sysname'=>'KEY_TO_MATCH3',....),
                   array('sysname'=>'KEY_TO_MATCH4',....),
                  )
           )); 
foreach ($outerarray as $outerkey=>$outervalue) {
  foreach ($outervalue[]['inputs'] as $searcharray) {
    foreach($searcharray as $key=>$value) {
      if ($value == $_POST['KEY_TO_MATCH1']) return $outerkey;
    }
  }
}

3 Comments

this won't work as $_POST['KEY_TO_MATCH1'] does not exist. His architecture is strange that is what matters I think.
His architecture is strange, but the $_POST variable will exist if someone has posted values to the PHP page (i.e. through a form/ajax)
thank u for correcting array, i can do this compound loop but was wondering if there is a short cut for it using array_filter or something. +1
0

First try to make 1 array out of all of your values. To do this you need to get the inputs and couple them with their array key value.

If you have done this, take the value of your post and do a foreach through the array you just created. If you have a match, return the array key.

Comments

0

your architecture is very complex. I don't know your usecase but I'm sure you can simplify it by getting rid of the double depth in input.

If you keep (or have to keep) this architecture, you have to process a search :

foreach($baseArray as $key=>$value){
    foreach($value['inputs'] as $subArray){
       if(subArray['sysname'] === $keyToMatch{
          return $key;
       }
    }
}
return '';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.