Sign in to follow this  
Followers 0

PHP 5.4 ArrayObject?

8 posts in this topic

Posted

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;

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted


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;

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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;


Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

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;


Should I be doing foreach or while, 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.
<?php foreach ($ManyLovrs as $OneLovr => $Lovr) { } ?>

Share this post


Link to post
Share on other sites

Posted

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

?>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.