• 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
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;

Link to comment
Share on other sites

  • 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

Link to comment
Share on other sites

  • 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]

Link to comment
Share on other sites

  • 0

Yeah there's no real reason to not be using mysql when building it up really.

You can always code it using PDO and have it use a sqlite DB whilst testing then move to mysql when it's done.

Link to comment
Share on other sites

  • 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) { } ?>

Link to comment
Share on other sites

  • 0

foreach, while should only be used with SQL functions returning data, e.g.

<?PHP
$Records = Array();
while ($Data = MySQL_Fetch_Assoc($RecordSet))
{
	   //Do something with $Data...
	   $Records[] = $Data;
}
print_r($Records);
?>

Link to comment
Share on other sites

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

    • No registered users viewing this page.