• 0

PHP 5.4 ArrayObject?


Question

I'm working on my own social network at http://mylovr.com and I want to make the experience as great (and safe) as possible.

I'm creating a static list of users before I use MySQL as I'm working with FuelPHP and PHP 5.4.

Here's an array I'm using and I'm already extracted it with "foreach", but I wanted to make things more simpler when using a variable for every name, or at least more secure.


$ManyLovrs = [
		'mrxxiv' => [
			'id' => '0',
			'first_name' => 'Terrence',
			'last_name' => 'Campbell',
			'gender' => 'Male',
			'age' => '19',
			'sex_orient' => 'Straight',
			'city' => 'Miami',
			'state' => 'FL',
			'zip' => '33157',

			'avatar' => 'http://en.gravatar.com/userimage/36694356/07de52f2fac0069f8c9d5e76af07217f.png?size=200',

			'facebook' => 'MrXXIV',
			'twitter' => 'MrXXIV',

			'pro' => 1
		]
]

How do I turn #1 into #2?

$Lovr['first_name'];

$Lovr->first_name;

Link to comment
https://www.neowin.net/forum/topic/1118942-php-54-arrayobject/
Share on other sites

7 answers to this question

Recommended Posts

  • 0
class Lovr
{
  public $first_name;
  public $last_name;
  //.. more
  public $twitter;
  public $facebook;
}

$aLovr = new Lovr;
$aLovr->first_name = "Zodiac";
$aLovr->last_name = "MF";
//.. more
$aLovr->twitter = "@zodiac_mf";
$aLovr->facebook = "";

$ManyLovrs['mrxxiv'] = $aLovr;

  • 0
Create a lovr class, then just write a method that will return any data saved to that object. Or, start using MySQL now. From what I see here, there's no reason to not get started.

You're definitely right! The solution virtorio gave me, gives the conclusion that I have no choice but to move on to MySQL. :)

 class Lovr { public $first_name; public $last_name; //.. more public $twitter; public $facebook; } $aLovr = new Lovr; $aLovr->first_name = "Zodiac"; $aLovr->last_name = "MF"; //.. more $aLovr->twitter = "@zodiac_mf"; $aLovr->facebook = ""; $ManyLovrs['mrxxiv'] = $aLovr; 

Really appreciate it! :D

  • 0

You mention ArrayObject in the title, yet no-one seems to have given an example using it. You can pass it a flag telling it to make the array keys available as if they were properties of the class like so...


$array = array(
'id' => '0',
'first_name' => 'Terrence',
'last_name' => 'Campbell',
'gender' => 'Male',
'age' => '19',
'sex_orient' => 'Straight',
'city' => 'Miami',
'state' => 'FL',
'zip' => '33157',
'avatar' => 'http://en.gravatar.com/userimage/36694356/07de52f2fac0069f8c9d5e76af07217f.png?size=200',
'facebook' => 'MrXXIV',
'twitter' => 'MrXXIV',
'pro' => 1
);

// create new ArrayObject instance, along with flag to use array keys as property names
$obj = new ArrayObject($array, ArrayObject::ARRAY_AS_PROPS);

// access array keys as property names of object
echo $obj->first_name;

[/CODE]

  • 0

You mention ArrayObject in the title, yet no-one seems to have given an example using it. You can pass it a flag telling it to make the array keys available as if they were properties of the class like so...

$array = array(
	  'id' => '0',
	  'first_name' => 'Terrence',
	  'last_name' => 'Campbell',
	  'gender' => 'Male',
	  'age' => '19',
	  'sex_orient' => 'Straight',
	  'city' => 'Miami',
	  'state' => 'FL',
	  'zip' => '33157',
	  'avatar' => 'http://en.gravatar.com/userimage/36694356/07de52f2fac0069f8c9d5e76af07217f.png?size=200',
	  'facebook' => 'MrXXIV',
	  'twitter' => 'MrXXIV',
	  'pro' => 1
);

// create new ArrayObject instance, along with flag to use array keys as property names
$obj = new ArrayObject($array, ArrayObject::ARRAY_AS_PROPS);

// access array keys as property names of object
echo $obj->first_name;

[/CODE]


Should I be doing [b]foreach[/b] or [b]while[/b], I'm not exactly sure where to head from there as this is what I had before I started attempting ArrayObject, plus the array was originally nested for multiple users.
[code]<?php foreach ($ManyLovrs as $OneLovr => $Lovr) { } ?>

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • It depends on how technical of an answer you want. Does it stop manufacture drivers? Yes, the universal driver is the only thing that interfaces with the printer hardware. However, print support apps (PSAs) are the new way to provide that functionality you speak of, and they integrate directly into the print dialog box, so the user doesn't see any difference at all. This is nothing new...this has been the preferred way to do print drivers for almost 8 years! The only thing changing here is that the older printer model drivers of the past are being deprecated. I highly suspect that you yourself have installed a print support app described as a driver package from the manufacture's support page, had it work exactly as expected, without realizing it was using the universal IPP driver under the covers. For example, USB keyboards have all used the generic HID driver for just about as long as they have existed, but that doesn't stop manufacture "drivers" from enabling more features.
    • https://www.neowin.net/news/microsofts-captionbot-will-try-to-caption-any-picture---as-long-as-hitlers-not-in-it/ The early days of "AI"!
    • Wow..... 1.8GB of RAM usage. That's just a bit more than MS Teams or VS Code uses.
    • its slower than ever. especially on mobile ios. really poor app. Slack is a million times better. shame the company i work for moved from Slack to teams few years back. was like moving from 2026 to windows ME.
    • Manufacturers will be able to ship printer-specific functions through their support apps (their companion software, in other words). But no, drivers won't be needed from them anymore, only the universal one included with Windows. Think about this as printers all agreeing to a stardandized API for common functions, all talking the same language and supporting the most common functionality via that API, so only a single driver is needed to talk to them. Any additional functionality not handled by that driver will be provided by the companion software for the printer.
  • Recent Achievements

    • One Month Later
      Sopa flores earned a badge
      One Month Later
    • First Post
      StaticMatrix earned a badge
      First Post
    • Week One Done
      StaticMatrix earned a badge
      Week One Done
    • Rookie
      lamborghiniv10 went up a rank
      Rookie
    • One Month Later
      pinnclepd earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      531
    2. 2
      PsYcHoKiLLa
      209
    3. 3
      +Edouard
      156
    4. 4
      Steven P.
      99
    5. 5
      ATLien_0
      82
  • Tell a friend

    Love Neowin? Tell a friend!