开发者

How to retrieve an array from Multidimensional Array

So I have a multi-dimensional array looks like this.

$config = array(
  "First Name" => array(
          "user"  => $_POST['firstname'],
          "limit" => 35,
        ),
  "Last Name" => array(
          "user"  => $_POST['lastname'],
          "limit" => 40,
        ),
);

I want use the array that's within the config array, so my approach is to use a foreach loop.

foreach($config as $field => $data) {

}

Now I know that $data will be my array, but it seems I can't use it outside of the foreach statement because I only get half of whats already there. Using print_r you can see what it shows outside the loop:

 Array
(
    [user] => lastname
    [limit] => 40
)

But when inside the loop and I use print_r here is my result:

Array
(
    [user] => firstname
    [limit] => 35
)
Array
(
    [user] => lastname
    [limit] => 40
)

I imagine it has to do something with it being with the foreach loop. I've tried to run a foreach on the $data array to populate another array, but that didn't work as well.

Is开发者_Python百科 there a way to use this outside of a foreach loop?

Sorry if this a dumb question, I'm sure there is a quite a simple answer to this, but I'm just stumped, and can't think of a way to do this. Thanks.


$config['First Name'] will return the first array, $config['Last Name'] the second.

E.g.

$first_name_config = $config['First Name'];

If you looped over the array, then $data will point to the last element of the array when the looped finished.

Is this what you want? If not, please clarify your question.

In any way, you might want to read about Arrays in PHP.


You can access the two subarrays as $config['First Name'] and $config['Last Name'].

Is that what you want to do?


The trick is to understand that this isn't really a multidimensional array, i.e. it's not stored as a grid or a matrix (unlike true multidimensional arrays in e.g. C). Rather it's just arrays stored within an array.

To answer your headline question, it rather depends on what you mean by "retrieve"!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜