0

I want to create a function where i pass as parameter an array "position", something like this:

function f($pos){};
f($people[all][person][name]);

Then i want to retrieve the parent dimension of the array, so I want to have $people[all]. This way I could do a foreach cycle on all person.

My idea is to do a function where I pass the position and the function sort the "parent" dimension by the position given.

Common case:

$people[men][person][name]=>value
f($people[men][person][name])

will sort all men by their name.

How could I do that?

Anders
  • 8,307
  • 9
  • 56
  • 88
Francesco
  • 555
  • 4
  • 23
  • 2
    you're not passing a "position" with that notation. you'd be passing in a single value. you'd have to pass in the individual keys for every level you're dealing with, as separate parameters. – Marc B Oct 02 '15 at 20:22
  • yeah i m passing the value, but my idea (what i want to do) is to pass a pointer (reference maybe) to that position, retrive the parent position and sort the array, all with one parameter... can i do that? – Francesco Oct 02 '15 at 20:24
  • I'm not sure I understand what you mean. You want to pass a reference to what? – Anders Oct 02 '15 at 20:44
  • To sort based on sub array, plese see this question: http://stackoverflow.com/questions/2477496/php-sort-array-by-subarray-value – Anders Oct 02 '15 at 20:47
  • yeah i think, i don't need the value, my function should work on the array like do the native php sort functions... – Francesco Oct 02 '15 at 20:47
  • @anders i already saw that thread, but i don't want to create ad hoc functions, i want to create a function that given the position automatically retrive the parent dimension and sort by the position given, like the example i wrote... – Francesco Oct 02 '15 at 20:54
  • 1
    passing a value is risky. what if you've got duplicate values in the array? You'd find multiple parents. – Marc B Oct 02 '15 at 21:00
  • i don't want to pass the value, i want to pass a pointer to that position and then traverse the array... – Francesco Oct 03 '15 at 12:14

1 Answers1

1

How about this

function f($pos){
foreach ($people[all][person] as $name){
echo $name;
}

}
f($people);

this will you name

Exception
  • 789
  • 4
  • 18
  • $people[all]ecc. was just an example, i don't know the array i get... i m building a "multi purpose" function, that will work with different arrays... – Francesco Oct 02 '15 at 20:26
  • 1
    well there is situation ,why are you passing position as a parameter .if you want to get the value of that element and you know its static(here I mean position) then just send that array to that function and get then value using foreach() function . that's it – Exception Oct 02 '15 at 20:29
  • Having the "child" how can i get the parent to pass at foreach? havinge f(&$pos){}; -> f($arr[pos1][pos2]) -> what i pass to the foreach? now i have only $pos and i don't know his structure... – Francesco Oct 02 '15 at 20:31