数组中的长度数组

时间:2016-02-29 10:51:24

标签: php arrays

如果我有这样的数组:

$array = array(
     1,
     2,
     3,
     ["thing1", "thing2", "thing3"]
);

如何检查数组中数组的长度([“thing1”,“thing2”,“thing3”])?

3 个答案:

答案 0 :(得分:6)

您可以使用Array Count function import java.io.File; import java.io.FileInputStream; import java.io.PrintWriter; import org.apache.pdfbox.cos.COSDocument; import org.apache.pdfbox.pdfparser.PDFParser; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.util.PDFTextStripper; public class JavaApplication14 { PDFParser parser; String parsedText; PDFTextStripper pdfStripper; PDDocument pdDoc; COSDocument cosDoc; // PDDocumentInformation pdDocInfo; // PDFTextParser Constructor public JavaApplication14() { } // Extract text from PDF Document String pdftoText(String fileName) { System.out.println("Parsing text from PDF file " + fileName + "...."); File f = new File(fileName); if (!f.isFile()) { System.out.println("File " + fileName + " does not exist."); return null; } try { parser = new PDFParser(new FileInputStream(f)); } catch (Exception e) { System.out.println("Unable to open PDF Parser."); return null; } try { parser.parse(); cosDoc = parser.getDocument(); pdfStripper = new PDFTextStripper(); pdDoc = new PDDocument(cosDoc); parsedText = pdfStripper.getText(pdDoc); } catch (Exception e) { System.out.println("An exception occured in parsing the PDF Document."); e.printStackTrace(); try { if (cosDoc != null) { cosDoc.close(); } if (pdDoc != null) { pdDoc.close(); } } catch (Exception e1) { e.printStackTrace(); } return null; } System.out.println("Done."); return parsedText; } // Write the parsed text from PDF to a file void writeTexttoFile(String pdfText, String fileName) { System.out.println("\nWriting PDF text to output text file " + fileName + "...."); try { PrintWriter pw = new PrintWriter(fileName); pw.print(pdfText); pw.close(); } catch (Exception e) { System.out.println("An exception occured in writing the pdf text to file."); e.printStackTrace(); } System.out.println("Done."); } public static void main(String args[]) { String fileList[] = {"E:\\JavaApplication14\\src\\javaapplication14\\issues.pdf", "E:\\JavaApplication14\\src\\javaapplication14\\newTextDocument.txt"}; if (fileList.length != 2) { System.out.println("Usage: java PDFTextParser <InputPDFFilename> <OutputTextFile>"); System.exit(1); } JavaApplication14 pdfTextParserObj = new JavaApplication14(); String pdfToText = pdfTextParserObj.pdftoText(fileList[0]); if (pdfToText == null) { System.out.println("PDF to Text Conversion failed."); } else { System.out.println("\nThe text parsed from the PDF Document....\n" + pdfToText); pdfTextParserObj.writeTexttoFile(pdfToText, fileList[1]); } } } sizeof功能。

尝试以下代码:

count

答案 1 :(得分:3)

您可以使用count功能:

echo count($array[3]);

但是,如果您想要获得长度的数组并不总是处于相同的位置,您可以这样做:

foreach ($array as $element) {
    if (is_array($element) {
        echo count($element);
    }
}

答案 2 :(得分:0)

你必须在这里使用count功能。

public class myFrame {

    public static void main(String[] args) {

        JFrame lv_frame = new JFrame();
        // setup jframe here

        lv_frame.add(new image());

        lv_frame.setVisible(true);

    }

}

class image extends JPanel {

    public void paintComponent(Graphics graphic) {

        super.paintComponent(graphic);

        // Image and graphics are now created here
        BufferedImage image = new BufferedImage(250, 250, BufferedImage.TYPE_4BYTE_ABGR_PRE);
        Graphics2D graphic2D = image.createGraphics();

        draw(graphic2D);
        save(image);
    }

    public void draw(Graphics graphic) {

        Graphics2D graphic2D = (Graphics2D) graphic;
        graphic2D.fillArc(0, 0, 250, 250, 0, 90);

    }

    public void save(BufferedImage image) {
        try {
            File output = new File("file.png");
            ImageIO.write(image, "png", output);
        } catch(IOException log) {
            System.out.println(log);
        }

    }

}
  

虽然你可以使用sizeof但是sizeof()是count()的别名,   他们的工作方式相同。

您可以像这样使用sizeof ..

$array = array(
    1,
    2,
    3,
    ["thing1", "thing2", "thing3"]
);
echo count($array[3]); //out put 3