1

I have an field name that is passed to me via an ajax function which i need to use to lookup values in an existing array. e.g.

$myarray = array(
    'name' => array(
        '0' => array(
            'first' => 'Steve'
        )
    ),
);


$incoming = 'name[0][first]';

So using the $incoming variable i wanted to get the corresponding data out of the array but cant figure out how to convert the string into the array indicies lookup.

Im trying to come up with a method that will work regardless of the level of nesting in the array.

2

4 Answers 4

1

Suggestion #1

Change the jQuery to send the request as JSON, which can instantly be converted into a PHP array with json_decode.

Suggestion #2

If you can't switch to JSON, use preg_split to split the incoming field and loop over them to grab the correct array element. Something along the lines of:

$incoming = 'name[0][first]';

$parts = preg_split('/(\[|\])/', $incoming, null, PREG_SPLIT_NO_EMPTY);

$el = $myarray;
foreach ($parts as $part) {
    $el = $el[$part];
}

echo $el; // Steve
Sign up to request clarification or add additional context in comments.

Comments

0

you can use the eval function... evenhough this is extremely not recommended - especially when you get the string for the client (if I understood write)

 $incoming = 'name[0]["first"]';
 eval('$value = $'+$incoming+';');
 echo $value;

this script should print the data of the specific cell in the array...

again - this is not recommended unless you make sure that the input is in the format that you are expecting...

3 Comments

I don't think this should have been proposed, given the OP's level of knowledge of the subject. Even with the disclaimers.
With the information i gave the suggestion of running eval() against an argument passed via ajax would be a security disaster
I'm glad you understand that :)
0

This will work assuming there will always be 3 keys OR LESS. You could add "$keyX" vars and "if($numkeys == X)" for however big the array gets, but I could not find a more flexible fix

$incoming = 'name[0][first]';

$numkeys =  preg_match_all('/[\[]/', $incoming, $matches) + 1;

$split = preg_split('/[[\]]/', $incoming);
$key1 = $split[0];
$key2 = $split[1];
$key3 = $split[3];

if($numkeys == 1) {
echo $myarray[$key1];
} elseif ($numkeys == 2) {
echo $myarray[$key1][$key2];
} elseif ($numkeys == 3) {
echo $myarray[$key1][$key2][$key3];
}

Comments

0

try using explode function here, in your example - name[0][first] will give give an array - of (name, 0] , first] ) as list/array after you explode on '['. Discard the first element and from the rest, take the ']' part out. Later you may convert them to respective formats - integers, strings etc.

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.