Array and values from a object

I try to pull out values from a object. It contains full of array values but how to get them into variables?
An example:

 $clientinfo is an object
array(6) {
[“type”]=>
string(7) “browser”
[“name”]=>
string(7) “Chrom”
[“short_name”]=>
string(2) “CR”
[“version”]=>
string(4) “XXX”
[“engine”]=>
string(5) “Gecko”
[“engine_version”]=>
string(4) “XXX”
}
array(4) {
[“name”]=>
string(7) “Mac”
[“short_name”]=>
string(3) “XXX”
[“version”]=>
string(2) “XX”
[“platform”]=>
string(3) “x64”
}
string(7) “desktop”
string(0) “”
string(0) “”

Well these arrays are going to be assigned to a property of the object. So you will need to find that name and use it… $clientInfo->some_name['engine'] and that will equal Gecko.

Now if these arrays are together in an array then they both will be under some property name and could be referenced like… $clientInfo->some_name[1]['name'] and that would give you ‘Mac’

You might want to show us the entire object’s print_r or var_dump output if you are still having trouble locating the correct property name.

Thank you for the message.

Basically we have an array of objects. So, we have to use a combination of array and object syntax to get our value.

I have tested the following arrays:

array(6) {
  ["type"]=>
  string(7) "browser"
  ["name"]=>
  string(7) "Firefox"
  ["short_name"]=>
  string(2) "FF"
  ["version"]=>
  string(4) "90.0"
  ["engine"]=>
  string(5) "Gecko"
  ["engine_version"]=>
  string(4) "90.0"
}
array(4) {
  ["name"]=>
  string(7) "Windows"
  ["short_name"]=>
  string(3) "WIN"
  ["version"]=>
  string(2) "10"
  ["platform"]=>
  string(3) "x64"
}
string(7) "desktop"
string(0) ""
string(0) ""

Do you know why it is not stored inside variable even I assigned array values like engine, name…

$clientInfo->some_name['engine'];
$clientInfo->some_name[1]['name'];
$osInfo->some_name[1]['name'];

I try to break like an example:

$object = $array[0]; // Array Syntax
$status = $object->status; // Object Syntax
$status = $array[0]->status; // Combined Array & Object Syntax

Not according to PHP. What you’re showing us is two arrays, not two objects.

What code are you executing to get this output:

1 Like

Please find the whole code and source: https://packagist.org/packages/matomo/device-detector

require_once ROOT_DIR . "vendor/autoload.php";
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\AbstractDeviceParser;
use DeviceDetector\Parser\Bot AS BotParser;

$userAgent = $_SERVER['HTTP_USER_AGENT'];

$botParser = new BotParser();
$botParser->setUserAgent($userAgent);

// OPTIONAL: discard bot information. parse() will then return true instead of information
$botParser->discardDetails();
$result = $botParser->parse();

if (!is_null($result)) {
    // do not do anything if a bot is detected
    return;
}
// handle non-bot requests

$dd = new DeviceDetector($userAgent);

$dd->parse();
$clientInfo = $dd->getClient();
$osInfo = $dd->getOs();
$device = $dd->getDeviceName();
$brand = $dd->getBrandName();
$model = $dd->getModel();

//Objects with arrays!
//var_dump($clientInfo);
//var_dump($osInfo);
//var_dump($device);
//var_dump($brand);
//var_dump($model);

$clientInfo->some_name['engine'];
$clientInfo->some_name[1]['name'];
$osInfo->some_name[1]['name'];

right, so clientInfo and osInfo are Associative Arrays.

$clientInfo["type"] will output "browser".

It does not show up specifically value into variables even objects and arrays are known. I have an issue how to store clientInfo into PHP variable specifically engine or osInfo specifically

some_name[1]['name'];

clientInfo is already a PHP variable.
$clientInfo["engine"] is Gecko, in your example.
you already know how to do assignment statements, because you used one to assign the result of getClient to $clientInfo.

What am I missing here?

Thank you for the message. You are excellent developer. I will test again tomorrow.

It is working now. My last question:
I see there are also the Boolean data: TRUE/FALSE.
How to retrieve also just these values:

$dd->isSmartphone();
$dd->isFeaturePhone();
$dd->isTablet();
$dd->isPhablet();
$dd->isConsole();
$dd->isPortableMediaPlayer();
$dd->isCarBrowser();
$dd->isTV();
$dd->isSmartDisplay();
$dd->isSmartSpeaker();
$dd->isCamera();
$dd->isWearable();
$dd->isPeripheral();

$dd->isBrowser();
$dd->isFeedReader();
$dd->isMobileApp();
$dd->isPIM();
$dd->isLibrary();
$dd->isMediaPlayer();

Presumably, those functions just return the boolean, so you can just use it wherever you need the boolean value.

if($dd->isSmartphone()) { .... }

I border with the last point. If I use usual device (not a bot) it will start the first IF sentence
As I understand $botParser->discardDetails(); should start the first IF sentence. Is it the correct as my value should show only on the second if and the first IF should EXIT. Need help if I’m wrong.

$botParser->discardDetails();

$result = $botParser->parse();

if (is_null($result)) {

} else {

}

I don’t know what your functions do.
discardDetails makes it sound like its throwing things away;
then you parse, but dont tell it what to parse, so i assume it knows what to go and do.
then you check to see if $result is null.
Generally speaking, i’d expect parse to return SOMETHING, even if it’s a false. false != null.

I have seen source code.
Source: https://github.com/matomo-org/device-detector/blob/master/DeviceDetector.php

/**
 * Sets whether to discard additional bot information
 * If information is discarded it's only possible check whether UA was detected as bot or not.
 * (Discarding information speeds up the detection a bit)
 *
 * @param bool $discard
 */
    public function discardBotInformation(bool $discard = true): void
    {
        $this->discardBotInformation = $discard;
    }
if (!is_null($result)) {
    // do not do anything if a bot is detected
    return;
   
}
else {
  $clientInfo = $dd->getClient(); // holds information about browser, feed reader, media player, ...
  $osInfo = $dd->getOs();
  $device = $dd->getDevice();
  $brand = $dd->getBrandName();
  $model = $dd->getModel();

  echo $model;
  echo $device;
}

An example:
Mozilla/5.0 (compatible; SemrushBot/7~bl; +http://www.semrush.com/bot.html)
(United States,US)

So, bot should be detected and blocked inside the first IF sentence. Our goal is bot detection and block any contact form submissions.

sounds like that code is setting $dd->discardBotInformation to a boolean.