2012-02-24 53 views
2

是否有可能做这样的事情在PHP?索引访问PHP关联数组表示字符串

$index1 = "[0][1][2]"; 
$index2 = "['cat']['cow']['dog']"; 

// I want this to be $myArray[0][1][2] 
$myArray{$index1} = 'stuff'; 

// I want this to be $myArray['cat']['cow']['dog'] 
$myArray{$index2} = 'morestuff'; 

我已经搜索了一个解决方案,但我不认为我知道关键字涉及到搞清楚这一点。

+1

你可以用的eval()这样做,但我建议不要使用它。我们可以问为什么你想这样做?有可能是另一种方式来完成你想要的:) – Maurice 2012-02-24 15:46:27

+0

我写一个递归函数数组变造不同的维度。如果你传递'$ array ['cow']'到一个函数中,我敢肯定你不能从该函数访问'$ array'(即使通过引用传递) – afuzzyllama 2012-02-24 15:48:28

回答

3

不能直接使用。 $myArray[$index]将评估为$myArray['[0][1][2]']。你可能会拥有的对每个维度单独或者写一个小功能来解释字符串:

function strIndexArray($arr, $indices, $offset = 0) { 
    $lb = strpos($indices, '[', $offset); 
    if ($lb === -1) { 
     return $arr[$indices]; 
    } 
    else { 
     $rb = strpos($indices,']', $lb); 
     $index = substr($indices, $lb, $rb - $lb); 
     return strIndexArray($arr[$index], substr($indices, $rb+1)); 
    } 
} 

你或许可以找到一些正则表达式更容易提取,这将导致类似的指标:

$indices = /*regex*/; 
$value = ''; 
foreach($indices as $index) { 
    $value = $array[$index]; 
} 

要设置在阵列中的下面的函数,可以使用一个值:

function setValue(&$arr, $indices, $value) { 
    $lb = strpos($indices, '['); 
    if ($lb === -1) { 
     $arr = $value; 
    } 
    else { 
     $rb = strpos($indices, ']', $lb); 
     $index = substr($indices, $lb, $rb); 
     setValue($arr[$index], substr($indices, $lb, $rb+1), $value); 
    } 
} 

注:I上面的代码中的答案编辑器中进行,因此它可能包含一个错字或两个; )

+0

我刚刚意识到我的答案不是一个解决方案,因为它只是为了获得一个值,而提问者想设置一个值。 – 2012-02-24 15:57:02

+0

添加了一个函数,可以设置值 – 2012-02-24 16:03:11

+0

我试图想到一个正则表达式解决方案,但如果遇到'$ index =“['[a]']”;会发生什么?我认为一般解决方案的可能输入可能太复杂了。 – afuzzyllama 2012-02-24 16:04:28

-3

您可以使用两种匿名函数这一点。

$getThatValue = function($array){ return $array[0][1][2]; }; 
$setThatValue = function(&$array, $val){ $array[0][1][2] = $val; }; 

$setThatValue($myArray, 'whatever'); 
$myValue = $getThatValue($myArray); 
+2

我明白这个问题意味着''[0 ] [1] [2]“'是可变的。 – 2012-02-24 15:42:47

+0

他希望它周围,如果指标改变,会发生什么其他的方式:) – Maurice 2012-02-24 15:43:02

+1

? – afuzzyllama 2012-02-24 15:44:36

4
eval('$myArray[0][1][2] = "stuff";'); 
eval('$myArray'.$index1.' = "stuff";'); 

但使用eval和用户输入的时候,因为它很容易受到代码注入攻击要小心。

+0

gahh,是'eval()唯一的解决方案吗? – afuzzyllama 2012-02-24 15:45:35

+0

你也可以用脏方法来解析你的$ index字符串,然后通过数组级别循环。对这种方法感兴趣? – Basti 2012-02-24 15:46:19

+2

'eval'可能不是唯一的解决方案,但它是*最好的*之一。 'eval'完全是*你正在寻找的东西。请不要盲目忽略最好的解决方案,因为你听到人们说“eval是邪恶的”。 – meagar 2012-02-24 15:48:10

0

总有邪恶的eval:

eval('$myArray' . $index1 . ' = "stuff";'); 
1
$index1 = "[0][1][2]"; 
$index2 = "['cat']['cow']['dog']"; 

function myArrayFunc(&$myArray,$myIndex,$myData) { 
    $myIndex = explode('][',trim($myIndex,'[]')); 
    $m = &$myArray; 
    foreach($myIndex as $myNode) { 
     $myNode = trim($myNode,"'"); 
     $m[$myNode] = NULL; 
     $m = &$m[$myNode]; 
    } 
    $m = $myData; 
} 

// I want this to be $myArray[0][1][2] 
myArrayFunc($myArray,$index1,'stuff'); 

// I want this to be $myArray['cat']['cow']['dog'] 
myArrayFunc($myArray,$index2,'morestuff'); 


var_dump($myArray);