简体   繁体   中英

get value of associative array without using the key name

I am experimenting with JSON and PHP

I create an (nested) object in Javascript like

var obj2={user:{name:"will"}, age:27, country:"UK"};

I turn it to JSON and send it via AJAX/POST to an php file

hr.send("firstname="+JSON.stringify(obj2));

There, I decode it,

$a=$_POST['firstname']; $b=json_decode($a,true);

so its like Array ( [user] => Array ( [name] => will ) [age] => 27 [country] => UK )

Now, if I do

echo $b[user][name];

I will get "will"

How can I still get the same result, but without knowing the names of the keys? Do something like echo $b[0][0]; and still output "will"?

Thanks

I'm not sure why you'd want to do this. The whole point of associative arrays in PHP is to easily "associate" the indexes to their values. The key names gives a hint on what that particular index contains. By converting the array into a numeric indexed one, you will lose the information.

If that's not an issue, you can re-index the array first in order to be able to access the array values based on their numeric offsets. Use array_values() to re-index the array and then access the values based on numeric offsets (as opposed to keys):

$array = array_values($b['user']);
echo $array[0]; // => 'Will'

You can use array_keys for that, it returns a new array with all the keys indexed by integers

$keys = array_keys($b);
$user = $b[$keys[0]];

$keys2 = array_keys($user);
$name  = $user[$keys2[0]]; // "will";

Do you mean this?

$b = array( 'user' => array ( 'name' => 'will' ), 'age' => 27);

echo current(current($b)); // 'will'

This assumes the internal "array pointer" hasn't been moved before. See current for details.

A more flexible would be a function that picks the n-th element:

function nth($ary, $position) {
    return array_values($ary)[$position];
}

Example:

$b = array( 'user' => array ( 'name' => 'will' , 'lastname' => 'smith'), 'age' => 27);
print nth(nth($b, 0), 1); // smith

You should not go via JSON or by associative arrays. Theoretically associative arrays do not guarantee that the order will be preserved.

This approach will only be useful if you can assume a standard order of fieldnames for each object.

Create a standard list of fieldnames that will be used when sending and receiving.

$objectsTypes = array('user','task')
$fieldsUser = array('firstname','lastname','age');

Then send the data in the correct order. Example using GET (POST could also work the same way):

?data[0][0]=Maxwell&data[0][1]=Smart&data[0][2]=35&data[1]=AnswerShoePhone

When processing the data you know which position to expect each variable. You could hard code the positions, or detect them dynamically.

echo $_GET['data'][0][0];//Maxwell
echo $_GET['data'][0][1];//Smart
echo $_GET['data'][1];//AnswerShoePhone

$indexOfUser = array_search('user',$objectsTypes);
$indexOfLastname = array_search('lastname',$fieldsUser);
echo $_GET['data'][ $indexOfUser ][ $indexOfLastname ];//Smart

If you want to support a collection of objects (eg multiple users) you can nest everything one level deeper. eg

?data[0][0][0]=Maxwell
&data[0][0][1]=Smart
&data[0][1][0]=Agent
&data[0][1][1]=99

$users = $_GET['data'][ $indexOfUser ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM