2013-04-16 55 views
-1

我想了解使用「選擇器」可以在多維數組中找到元素的方式。PHP:通過字符串選擇器查找數組元素

$vars = array(
    'site' => 'Stackoverflow', 
    'menu' => array(
     'items' => array('Questions', 'Tags', 'Users', 'Badges', 'Unanswered'), 
    ), 
    'sidebar' => array(
     'tags' => array(
      'c_sharp' => 68, 
      'java' => 62, 
      'javascript' => 52, 
      'jquery' => 50 
     ) 
    ) 
); 

我想有一些功能mixed find(string $selector, array $array)

使用

$site = find('site', $vars); 
// result: "Stackoverflow" 

$menuItems = find('menu.items', $vars); 
// result: ["Questions", "Tags", "Users", "Badges", "Unanswered"] 

$tags = find('sidebar.tags', $vars); 
// result: ["c_sharp" => 68, "java" => 62, "javascript" => 52, "jquery" => 50] 

$javascriptQuestionsCount = find('sidebar.tags.javascript', $vars); 
// result: 52 

$undefinedElement = find('footer.copyright.year', $vars); 
// result: null 

任何人都可以給我建議的方式來實現這種功能或許有一些現成的解決方案?

在此先感謝。

+2

Laravel有一個函數可以做到這一點,它被稱爲[array_get()](https://github.com/laravel/laravel/blob/master/laravel/helpers.php#L59-78) –

+1

你試過了嗎?執行它?從我看到的你只需要'explode('。',$ selector)',這看起來很簡單。 – Passerby

回答

3

Laravel有一個正是這樣做的一個功能,它被稱爲array_get()

你可能會想忽略value()功能,但你可以很return $default;代替。


/** 
* Get an item from an array using "dot" notation. 
* 
* <code> 
*  // Get the $array['user']['name'] value from the array 
*  $name = array_get($array, 'user.name'); 
* 
*  // Return a default from if the specified item doesn't exist 
*  $name = array_get($array, 'user.name', 'Taylor'); 
* </code> 
* 
* @param array $array 
* @param string $key 
* @param mixed $default 
* @return mixed 
*/ 
function array_get($array, $key, $default = null) 
{ 
    if (is_null($key)) return $array; 

    // To retrieve the array item using dot syntax, we'll iterate through 
    // each segment in the key and look for that value. If it exists, we 
    // will return it, otherwise we will set the depth of the array and 
    // look for the next segment. 
    foreach (explode('.', $key) as $segment) 
    { 
     if (! is_array($array) or ! array_key_exists($segment, $array)) 
     { 
      return value($default); 
     } 

     $array = $array[$segment]; 
    } 

    return $array; 
} 

/** 
* Return the value of the given item. 
* 
* If the given item is a Closure the result of the Closure will be returned. 
* 
* @param mixed $value 
* @return mixed 
*/ 
function value($value) 
{ 
    return (is_callable($value) and ! is_string($value)) ? call_user_func($value) : $value; 
} 
+0

非常感謝,正是我所尋找的 –

+0

雖然有一個問題......確切地說,'value($ default)'是做什麼的? – Havelock

+1

@Havelock'call_user_func' if value'is_callable'否則只是返回 –

0

是這樣的:

<?php 
$vars = array(
    'site' => 'Stackoverflow', 
    'menu' => array(
     'items' => array('Questions', 'Tags', 'Users', 'Badges', 'Unanswered'), 
    ), 
    'sidebar' => array(
     'tags' => array(
      'c_sharp' => 68, 
      'java' => 62, 
      'javascript' => 52, 
      'jquery' => 50 
     ) 
    ) 
); 

function find ($key, $array){ 
    $keys = explode(".",$key); 
    $currArr = $array; 
    for ($i=0; $i<count($keys);$i++){ 
     if (isset($currArr[$keys[$i]])){ 
      $currArr = $currArr[$keys[$i]]; 
     }else{ 
      return null; 
     } 
    } 

    return $currArr; 
} 

$site = find('site', $vars); 
print_r($site); 
echo "<br />"; 

$menuItems = find('menu.items', $vars); 
print_r($menuItems); 
echo "<br />"; 

$tags = find('sidebar.tags', $vars); 
print_r($tags); 
echo "<br />"; 

$javascriptQuestionsCount = find('sidebar.tags.javascript', $vars); 
print_r($javascriptQuestionsCount); 
echo "<br />"; 

$undefinedElement = find('footer.copyright.year', $vars); 
print_r($undefinedElement); 
echo "<br />"; 
?> 

輸出:

Stackoverflow 
Array ([0] => Questions [1] => Tags [2] => Users [3] => Badges [4] => Unanswered) 
Array ([c_sharp] => 68 [java] => 62 [javascript] => 52 [jquery] => 50) 
52 
null 

小提琴:

http://phpfiddle.org/lite/code/u7u-8jx

沒有任何lib,只是普通的PHP。