4 Replies - 161 Views - Last Post: 11 February 2019 - 12:47 PM Rate Topic: -----

#1 natronp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-February 19

passing object with arrays to a function in php

Posted 11 February 2019 - 12:06 PM

Trying to pass an object with arrays to a function and not having luck. where am I missing the boat here?

$langArrs = new stdClass(); 
$langArrs->dog = array("de", "en", "es", "fr", "nl", "sr");
$langArrs->cat = array("de", "en", "it");
$langArrs->snail = array("de", "en", "es", "it");
$langArrs->monkey = array("en");
$langArrs->bird = array("en");

doThis = function($var, $langArrs)
{
$mySys = $langArrs->{$var};
echo $mySys[0];
}

$var = "dog";

doThis($var, $langArrs);




??? Thanks for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: passing object with arrays to a function in php

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2766
  • View blog
  • Posts: 10,958
  • Joined: 03-December 12

Re: passing object with arrays to a function in php

Posted 11 February 2019 - 12:16 PM

Just need to fix your syntax:
<?php
 
$langArrs = new stdClass(); 
$langArrs->dog = array("de", "en", "es", "fr", "nl", "sr");
$langArrs->cat = array("de", "en", "it");
$langArrs->snail = array("de", "en", "es", "it");
$langArrs->monkey = array("en");
$langArrs->bird = array("en");
 
$doThis = function($var, $langArrs)
{
$mySys = $langArrs->{$var};
echo $mySys[0];
};
 
$var = "dog";
 
$doThis($var, $langArrs);

Was This Post Helpful? 0
  • +
  • -

#3 natronp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-February 19

Re: passing object with arrays to a function in php

Posted 11 February 2019 - 12:21 PM

sorry, that was a typo when I made this simplified example. Even if i fix the syntax when declaring that function, I get:

Quote

Undefined property: stdClass::$dog

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2766
  • View blog
  • Posts: 10,958
  • Joined: 03-December 12

Re: passing object with arrays to a function in php

Posted 11 February 2019 - 12:24 PM

https://www.ideone.com/sJGKn8
Was This Post Helpful? 0
  • +
  • -

#5 natronp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-February 19

Re: passing object with arrays to a function in php

Posted 11 February 2019 - 12:47 PM

Thanks... thought I was losing my mind (still am, but at least I know it's not because of that!)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1