Count amount of element of an array of a page

0

Hello, good afternoon. How can I make a foreach loop or a preg_match_all to count how much the appid element repeats on a page?

I tried and this way:

    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 question:

  

How to check for the "appid" string on a page after a   cURL and thus make the listing of the names of the "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);
    
asked by anonymous 30.07.2018 / 20:19

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;

By doing foreach in variable $array , each $arr that the function return will be a subarray of array principal. So just use the array_key_exists() function by passing the element you want to search, which in this case is appid and passing array you want to check, in this case are $arr , if you find only increment in counter, $count .

    
31.07.2018 / 17:45