Count items with a certain extension within a PHP array

Asked

Viewed 48 times

-1

Hello, good morning! All right? I need to set up a media gallery for a certain image extension ('.jpg') and I’m having difficulty counting the files ($key) in the value field (ds_midia_link). How should I proceed?

Thanks in advance,

[midias] => Simplexmlelement Object ( [media] => Array ( [0] => Simplexmlelement Object ( [id_image] => 5a29842d8ec9f [ds_imagem_credi] => Google [ds_image] => Black Panther [ds_imagem_link] => http://portal.interno.com.br//_midias/jpg/example.jpg

                [1] => SimpleXMLElement Object
                    (
                        [id_imagem] => 5a29842d8ec9f
                        [ds_imagem_credi] => Google
                        [ds_imagem] => Pantera 
                        [ds_imagem_link] => http://portal.interno.com.br//_midias/jpg/exemplo1.jpg

                [2] => SimpleXMLElement Object
                    (
                        [id_imagem] => 5a29842d8ec9f
                        [ds_imagem_credi] => Google
                        [ds_imagem] => Pantera 
                        [ds_imagem_link] => http://portal.interno.com.br//_midias/jpg/exemplo2.jpg

1 answer

0

There is a function in php that counts count(). Assign the value to a variable for example $qtdReg = count($seuArray);

Ref: http://php.net/manual/en/function.count.php

<?php

$arr = [
        [
            'id_imagem' => '5a29842d8ec9f',
            'ds_imagem_credi' => 'Google',
            'ds_imagem' => 'Pantera',
            'ds_imagem_link' => 'http://portal.interno.com.br//_midias/jpg/exemplo1.jpg'
        ],
        [
            'id_imagem' => '5a29842d8ec9f',
            'ds_imagem_credi' => 'Google',
            'ds_imagem' => 'Pantera',
            'ds_imagem_link' => 'http://portal.interno.com.br//_midias/jpg/exemplo2.jpg'
        ],
];

$qtdReg = count($arr);

echo $qtdReg;

The output will be: 2

  • I get it. The problem is that my array I can have media of type image with another extension (e.g. png, gif, etc)... would have as I refine by file extension?

  • Ai you will have to go through the array of a better example then, type you will have some key in the array that will be extension?

  • I would need to get to $value from $key ['ds_imagem_link'] scroll through it and check if it has a certain extension...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.