3

I have an array with the fields/path to a value i want to use (for usort). Below you can find how i can get the value hardcoded. I Just can not figure out how to create this programatically. The count of the $arrOrderBy is variable. So i probably have to use a foreach?

This is my order by array:

$arrOrderBy = array(
  0 => 'GameObject',
  1 => 'Question',
  2 => '0',
  3 => 'title'
)

This value:

$a[$arrOrderBy[0]][$arrOrderBy[1]][$arrOrderBy[2]][$arrOrderBy[3]];

Has to result to:

$a['GameObject']['Question'][0]['title']

Regards,

2
  • 1
    Just saying, that $array[0] is not the same as $array['0']. Commented Jun 4, 2013 at 8:25
  • PHP does not seem to care :). Commented Jun 4, 2013 at 8:30

2 Answers 2

1

Try this:

$value = $a;

foreach ($arrOrderBy as $orderBy)
{
    $value = $value[$orderBy];
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can also change the value of the field using the following pattern :

$arrOrderBy = array(
  0 => 'GameObject',
  1 => 'Question',
  2 => '0',
  3 => 'title'
);

$a = array(
     'foo' => array (
         'bar' => array (
               'baz' => null
          )
     )
);

$v =& $a;
foreach ($arrOrderBy as $k => $breadcrumb)
{
     $v =& $v[$breadcrumb];
}
$v = 'anything you want';

print_r ($a);

outputs:

Array
(
    [foo] => Array
        (
            [bar] => Array
                (
                    [baz] => "anything you want"
                )

        )

)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.