0

I have a form and I serialised it to send it to PHP (AJAX) :

var dataString = $('#form_filtre').serializeArray();

I would like to extract in PHP value where names is "ou" :

    array(1) {
  ["form_serialize"]=>
  array(6) {
    [0]=>
    array(2) {
      ["name"]=>
      string(3) "ctr"
      ["value"]=>
      string(6) "maison"
    }
    [1]=>
    array(2) {
      ["name"]=>
      string(6) "action"
      ["value"]=>
      string(17) "readHomesLocation"
    }
    [2]=>
    array(2) {
      ["name"]=>
      string(2) "ou"
      ["value"]=>
      string(1) "8"
    }
    [3]=>
    array(2) {
      ["name"]=>
      string(2) "ou"
      ["value"]=>
      string(1) "6"
    }
    [4]=>
    array(2) {
      ["name"]=>
      string(5) "quand"
      ["value"]=>
      string(0) ""
    }
    [5]=>
    array(2) {
      ["name"]=>
      string(3) "max"
      ["value"]=>
      string(3) "500"
    }
  }
}

I would like to extrat 6 and 8.

The problem, is that I don't know in advance how many "ou" I will have.

It can be from 0 to n

3
  • 1
    not hard to research how to filter an array or iterate array and use conditionals to do what is needed. Not clear what you plan to do with this data Commented Jan 2, 2016 at 17:14
  • the target is to use the value of "ou" in a sql request. Commented Jan 2, 2016 at 17:28
  • That doesn't explain much but going back to first statement you can use php array filtering or simply iterate array and check that value and react accordingly Commented Jan 2, 2016 at 17:30

1 Answer 1

2

Seems like an easy approach would be to have PHP unserialize the array, then iterate over it with a foreach loop like this:

foreach($array as $index => $subArray) {
    foreach($subArray as $key => $val) {
        if ($key == "ou") {
            $ouArray[$index] = $val;
        }
    }
} 

(where $array, obviously, is your unserialized array)

You could also just use $ouArray[] = $val, if you don't care which element the ou belonged to.

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

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.