Jump to content



Photo

PHP 5.4 ArrayObject?


  • Please log in to reply
7 replies to this topic

#1 Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 12 November 2012 - 19:31

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;



#2 threetonesun

threetonesun

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 26-February 02

Posted 12 November 2012 - 19:46

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.

#3 virtorio

virtorio

    Neowinian Senior

  • Tech Issues Solved: 13
  • Joined: 28-April 03
  • Location: New Zealand
  • OS: OSX 10.10, Windows 8.1
  • Phone: LG G3

Posted 12 November 2012 - 19:54


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;



#4 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 12 November 2012 - 20:08

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

#5 CrispCreations

CrispCreations

    Neowinian

  • Joined: 19-April 04
  • Location: UK

Posted 14 November 2012 - 13:08

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;



#6 n_K

n_K

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 19-March 06
  • Location: here.
  • OS: FreeDOS
  • Phone: Nokia 3315

Posted 14 November 2012 - 13:12

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.

#7 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 14 November 2012 - 13:36

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


#8 n_K

n_K

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 19-March 06
  • Location: here.
  • OS: FreeDOS
  • Phone: Nokia 3315

Posted 14 November 2012 - 16:31

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