拜托,我需要帮助。 如何通过PHP转换我的数组
<div id="showNumPad">
<ul id="enteredPin"class="pin-list">
<li><span></span></li>
<li><span></span></li>
<li><span></span></li>
<li><span></span></li>
</ul>
<div class="keypad-container">
<ul id="numberList">
<li><span>1</span></li>
<li><span>2</span></li>
<li><span>3</span></li>
<li><span>4</span></li>
<li><span>5</span></li>
<li><span>6</span></li>
<li><span>7</span></li>
<li><span>8</span></li>
<li><span>9</span></li>
<li><span>0</span></li>
</ul>
</div>
</div>
/* CSS is HERE/*
.pin-list{ width:100%; float:left; text-align:center;}
.pin-list li{ display:inline-block;}
.pin-list li span{ width:15px; height:15px; display:inline-block; border:solid 1px #fff; border-radius:50%; margin:7px;}
.keypad-container{ width:100%; float:left; margin:10px 0;}
.keypad-container ul{ width:99%; display:inline-block; text-align:center;}
.keypad-container ul li{ display:inline-block; margin:7px 10px;}
.keypad-container ul li span{ width:60px; height:60px; display:inline-block; border-radius:50%; color:#fff; border:solid 1px #fff; text-align:center; line-height:60px; font-size:25px; cursor:pointer;}
.keypad-container ul li span:hover, .keypad-container ul li span:active, .keypad-container ul li span:focus{ background:#fff; color:#000;}
到此
Array
(
[0] => Apple
[1] => Orange
[2] => Tomato
)
我不知道我的阵列中有多少元素。 谢谢大家。
答案 0 :(得分:5)
试试这个:
if (buttonIndex==0) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
答案 1 :(得分:5)
尝试:
$target = array();
$value = array();
$path = array('apple', 'orange', 'tomato');
$rv = &$target;
foreach($path as $pk)
{
$rv = &$rv[$pk];
}
$rv = $value;
unset($rv);
print_r($target);
输出:
Array
(
[apple] => Array
(
[orange] => Array
(
[tomato] => Array
(
)
)
)
)
更新1:解释
这里我使用引用/变量别名来遍历动态的密钥堆栈。该引用使得可以使用堆栈而不是通常更精益的递归。此外,此代码可防止覆盖$target
数组中的现有元素。
有关参考文献的更多详细信息,请查看Reference Explained
$target = array(); //target array where we will store required out put
$value = array(); //last value i.e. blank array
$path = array('apple', 'orange', 'tomato'); //current array
$rv = &$target; //assign address of $target to $rv (reference variable)
foreach($path as $pk)
{
$rv = &$rv[$pk]; // Unused reference [ex. $rv['apple'] then $rv['apple']['orange'] .. so on ] - actually assigned to $target by reference
print_r($target);
echo '-----------------<br />';
}
$rv = $value; //here $rv have unused refernce of value tomato so finally assigned a blank array to key tomoto
//
unset($rv); // Array copy is now unaffected by above reference
echo "final array<br />";
print_r($target);
<强>输出:强>
Array
(
[apple] =>
)
-----------------
Array
(
[apple] => Array
(
[orange] =>
)
)
-----------------
Array
(
[apple] => Array
(
[orange] => Array
(
[tomato] =>
)
)
)
-----------------
final array
Array
(
[apple] => Array
(
[orange] => Array
(
[tomato] => Array
(
)
)
)
)
在解释输出中,您可以跟踪$target
循环中foreach
的值
答案 2 :(得分:5)
<强>输出强>
Array
(
[0] => Apple
[1] => Orange
[2] => Tomato
[3] => Banana
[4] => Papaya
)
Array
(
[Apple] => Array
(
[Orange] => Array
(
[Tomato] => Array
(
[Banana] => Array
(
[Papaya] => Array
(
)
)
)
)
)
)
<强>代码强>
$fruits = [
"Apple",
"Orange",
"Tomato",
"Banana",
"Papaya"
];
// Result Array
$result = [
$fruits[count($fruits) - 1] => []
];
// Process
for ($counter = count($fruits) - 2; $counter >= 0; $counter--) {
$temp = $result;
unset($result);
$result[$fruits[$counter]] = $temp;
}
// Display
echo "<pre>".print_r($fruits, true)."</pre>";
echo "<pre>".print_r($result, true)."</pre>";
答案 3 :(得分:2)
您也可以使用foreach
和ksort
<?php
$fruits = array(
"Apple",
"Orange",
"Tomato",
"Banana",
"Papaya"
);
krsort($fruits);
$tmp = array();
foreach($fruits as $fruit){
$tmp = array($fruit => $tmp);
}
echo "<pre>".print_r($tmp, true)."</pre>";
?>
[概念证明]
Array ( [Apple] => Array ( [Orange] => Array ( [Tomato] => Array ( [Banana] => Array ( [Papaya] => Array ( ) ) ) ) ) )
答案 4 :(得分:2)
while循环和array_pop:
$fruits = [
"Apple",
"Orange",
"Tomato",
"Banana",
"Papaya"
];
$output = [];
while ( $fruit = array_pop($fruits) )
{
$output = [$fruit => $output];
}
答案 5 :(得分:1)
$result = array_reduce(array_reverse($fruits), function (array $acc, $fruit) {
return [$fruit => $acc];
}, []);
您只需从内到外开始并将值包装到$key => $value
数组中。