1

code:

$array[ ( preg_match( '/pdf-\d/', $key ) ? true : false ) + '[' + ( preg_match( '/pdf-total-\d/', $key ) ? true : false ) + ']' ]

output should be:

$array['pdf-1[pdf-total-1]']
5
  • You haven't done yourself any favors cramming that all onto one line. Commented Nov 5, 2014 at 9:22
  • preg_match( '/pdf-\d/', $key ) will return true or false, you dont need the ? true : false, if you use preg_match( '/pdf-(\d)/', $key ) this will return one array with the (\d) found Commented Nov 5, 2014 at 9:31
  • Thanks for reply guys, but I figured out it myself. I just appended a hidden input in my jquery with value and match it in my php. Thanks a lot. Commented Nov 5, 2014 at 12:06
  • @nicearma: oh! preg_match returns 0 when there is no match, 1 when a match is found, false when an error occurs. Commented Dec 13, 2014 at 23:05
  • @nicearma: even if you use a capture group preg_match will not return the matched content! Commented Dec 13, 2014 at 23:13

1 Answer 1

2

Actually, you are using ternary operator:

preg_match( '/pdf-\d/', $key ) ? true : false

This returns true or false but never pdf-1 nor [pdf-total-1]

Maybe you want more something like this:

$array[ 
        ( preg_match( '/pdf-\d/', $key ) ? $key: false ) . 
        '[' . ( preg_match( '/pdf-total-\d/', $key ) ? $key: false ) . ']' 
      ]

But it doesn't have much sense in any case because $key matches the first pattern or it matches the second

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.