在PHP中处理关联数组路径列表

时间:2012-01-26 00:29:20

标签: php associative-array

我有一个数据blob,当反序列化时看起来类似于以下内容:

Array ( 
    [profile] => Array ( 
        [name] => Array ( 
            [firstName] => Slug 
            [midName] => Foo 
            [lastName] => Bar 
            [formatted] => Slug Foo Bar ) 
        [displayName] => Slug
    )
)

和一个存储这些值的路径的表:

['profile']['name']['firstName'],
['profile']['name']['lastName'],
...
['profile']['displayName']

我想遍历路径列表并检索特定数组的值,但似乎无法理解如何构造数组和路径以获取值:

$pathlist = array(
  ['profile']['name']['firstName'],
  ['profile']['name']['midName'],
  ['profile']['name']['lastName'],
  ['profile']['displayName']
);

$user = array(
  [profile] => array ( 
    [name] => array ( 
      [firstName] => Slug 
      [midName] => Foo 
      [lastName] => Bar 
    ), 
    [displayName] => Slug
  )
);

foreach ($pathlist as $path) {
  // display value using user array and pathitem
  echo $user$path;
}

有人有任何建议吗?

感谢。

3 个答案:

答案 0 :(得分:0)

你可以使用eval,但我不会。更好地爆炸你的数组中的['和']并说$ user [$ path] ...

答案 1 :(得分:0)

如果我理解你的问题,你有一个数据数组,并希望根据模式数组自动获取值?

<?php

$schema = array(
    'profile' => array(
        'name' => array(
            'firstName',
            'midName',
            'lastName'
        ),
        'displayName'
    )
);

$users = array(
    1 => array(
        'profile' => array(
            'name' => array(
                'firstName' => 'Joe',
                'midName'   => 'Pooface',
                'lastName'  => 'Bloggs'
            ),
            'displayName' => 'Joey'
        )
    ),
    2 => array(
        'profile' => array(
            'name' => array(
                'firstName' => 'John',
                'midName'   => 'Weeface',
                'lastName'  => 'Doe'
            ),
            'displayName' => 'Johnny'
        )
    )
);


echo "<pre>";

foreach ($users as $user) {

    foreach ($schema as $k1 => $v1) {
        if (is_array($v1)) {
            foreach ($v1 as $k2 => $v2) {
                if (is_array($v2)) {
                    foreach ($v2 as $k3 => $v3) {
                        if (is_string($v3)) {
                            echo "{$v3}: {$user[$k1][$k2][$v3]}\n";
                        }
                    }
                } else {
                    echo "{$v2}: {$user[$k1][$v2]}\n";
                }
            }
        } else {
            echo "{$v1}: {$user[$v1]}\n";
        }
        echo "\n";
    }
}

echo "</pre>";

我确信这可以通过数组回调函数进行清理,而不是手动遍历每个级别。

然而这在设计上是一个不好的方法,你应该至少将每个用户封装在已经知道数据数组结构的某种用户对象中。 E.g。

<?php

$users = array(
    1 => array(
        'profile' => array(
            'name' => array(
                'firstName' => 'Joe',
                'midName'   => 'Pooface',
                'lastName'  => 'Bloggs'
            ),
            'displayName' => 'Joey'
        )
    )
);


class User 
{

    private $data;

    public function __construct(array $data)
    {
        $this->data = $data;
    }

    public function getFirstName()
    {
        return $this->data['profile']['name']['firstName'];
    }

    public function getDisplayName()
    {
        return $this->data['profile']['displayName'];
    }

}

$joe = new User($users[1]);
echo "Hello, {$joe->getDisplayName()}!";
// Hello, Joey!

答案 2 :(得分:0)

如果您只想循环遍历未序列化的数组,请使用array_walk_recursive

$user = array(
    'profile' => array ( 
        'name' => array ( 
            'firstName' => 'John', 
            'midName' => 'Wilberforce', 
            'lastName' => 'Doe' 
        ), 
        'displayName' => 'bobthebuilder'
    )
);

function display($item, $key)
{
        echo "$key => $item\n";
}

array_walk_recursive($user, 'display');

输出:

firstName => John
midName => Wilberforce
lastName => Doe
displayName => bobthebuilder