Count element amount of a page array

Asked

Viewed 330 times

0

Hello, good afternoon. How can I make a foreach loop or a preg_match_all to count the amount in that element appid repeats in a page ?

I tried this way and got:

    function get_Jogos() {
$array = [ ["appid" => 291550,"name" => "Brawlhalla"], ["appid" => 000000,"name" => "Teste"], ["appid" => 000023,"name" => "Teste2"], ];    
foreach($array as $arr) 
    if (array_key_exists("appid", $arr))
    echo $jogos =  $arr["name"] . "<br>";
}

get_Jogos();

New doubt:

How to check the existence of the string "appid" on a page after a Curl and so make the listing of the names of "games" ?

curl_setopt($ch, CURLOPT_URL , "https://steamcommunity.com/profiles/$steamid/games/?tab=all");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$resp = curl_exec($ch);
  • 1

    Sabrina, try this: $count = count(array_keys($array, 'appid', true)); and then: echo $count; to see if it displays the result.

  • 2

    It’s in PHP or Javascript!?

  • I tried this way: $array = [&#xA;["appid" => 291550,"name" => "Brawlhalla"],&#xA;["appid" => 000000,"name" => "Teste"],&#xA;];&#xA;&#xA;$count = count(array_keys($array, 'appid', true));&#xA;echo $count; The return is: 0. It’s in php

  • I haven’t been able to.

1 answer

1


$array = [ ["appid" => 291550,"name" => "Brawlhalla"], ["appid" => 000000,"name" => "Teste"], ];

$count = 0;

foreach($array as $arr)
    if (array_key_exists("appid", $arr))
        $count++;

echo $count;

Making a foreach in the variable $array, each $arr that the return function will be a subarray of array main. Then just use the function array_key_exists() passing the element you want to search, which in this case is appid and passing to array where you wish to check, in the case are the $arr, only increment on the counter, $count.

  • I have a new question, I will edit the topic. If you can help me, I will be even more grateful.

  • It would be better to leave the previous question and perform a new one in the stack

  • Sorry, I wanted to save a new topic with a similar "problem"

Browser other questions tagged

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