我的哈希数组如下:
[
{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!107", :name=>"Folder 11", :type=>"folder"},
{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!105", :name=>"Pictures", :type=>"album"},
{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!113", :name=>"Public", :type=>"folder"},
{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!124", :name=>"sasd", :type=>"folder"},
{:id=>nil, :name=>nil, :type=>nil},
{:id=>nil, :name=>nil, :type=>nil},
{:id=>nil, :name=>nil, :type=>nil},
{:id=>nil, :name=>nil, :type=>nil},
{:id=>nil, :name=>nil, :type=>nil},
{:id=>nil, :name=>nil, :type=>nil},
{:id=>nil, :name=>nil, :type=>nil}
]
如果我的哈希nil
中id
为nil
,如何删除 <!doctype html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="text" name="Filename">
<input type="file" name="my_file[]" multiple>
<input type="submit" value="Upload">
</form>
<?php
if (isset($_FILES['my_file'])) {
$myFile = $_FILES['my_file'];
$fileCount = count($myFile["name"]);
$Filename=$_POST['Filename'];
$structure = "Uploads/SystemConfiguration/$Filename";
if (!mkdir($structure, 0777, true))
{
die('Failed to create folders...');
}
for ($i = 0; $i < $fileCount; $i++) {
$name = $_FILES["my_file"]["name"][$i];
$tmp_name=$_FILES["my_file"]["tmp_name"][$i];
move_uploaded_file($tmp_name,"$structure/$name");
?>
<p>File #<?= $i+1 ?>:</p>
<p>
Name: <?= $myFile["name"][$i] ?><br>
Temporary file: <?= $myFile["tmp_name"][$i] ?><br>
Type: <?= $myFile["type"][$i] ?><br>
Size: <?= $myFile["size"][$i] ?><br>
Error: <?= $myFile["error"][$i] ?><br>
print "Your file upload was successful and can be seen/downloaded <a href='./uploads/systemconifguration/$Filename'>here</a>
</p>
<?php
}
}
?>
</body>
</html>
值?
答案 0 :(得分:4)
答案 1 :(得分:1)
如果您关注删除元素哪个哈希id
为nil
,那么最好使用 delete_if
array.delete_if { |h| h[:id].nil? }
=> [
{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!107", :name=>"Folder 11", :type=>"folder"},
{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!105", :name=>"Pictures", :type=>"album"},
{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!113", :name=>"Public", :type=>"folder"},
{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!124", :name=>"sasd", :type=>"folder"}
]
注意: reject
将返回一个新数组,其中包含self
中给定块不是true
的项目。虽然delete_if
会删除self
的每个元素,但哪个块的评估结果为true
。
答案 2 :(得分:0)
我假设您希望得到其值不是全部arr = [{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!107", :name=>"Folder 11", :type=>"folder"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!105", :name=>"Pictures", :type=>"album"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!113", :name=>"Public", :type=>"folder"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!124", :name=>"sasd", :type=>"folder"}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}]
arr.find_all { |h| !h.values.compact.empty? }
# => [{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!107", :name=>"Folder 11", :type=>"folder"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!105", :name=>"Pictures", :type=>"album"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!113", :name=>"Public", :type=>"folder"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!124", :name=>"sasd", :type=>"folder"}]
的所有哈希值。你可以通过以下方式做到:
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.serpentcs.artlovers.main.CustomCursorAdapter$CustomPagerAdapter.instantiateItem(CustomCursorAdapter.java:329)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:869)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1085)
at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:250)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 3 :(得分:0)
您想使用select method过滤数组。
list = list.select { |item| item[:id] != nil }