Jump to content
  • 0
Sign in to follow this  
Followers 0

PHP 5.4 ArrayObject?

Question

Posted

I'm working on my own social network at [url="http://mylovr.com"]http://mylovr.com[/url] 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.

[code]

$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
]
]
[/code]

How do I turn #1 into #2?

[code]
$Lovr['first_name'];

$Lovr->first_name;
[/code]

Share this post


Link to post
Share on other sites

7 answers to this question

  • 0

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

Posted

[code]
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;
[/code]

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='threetonesun' timestamp='1352749571' post='595314502'] 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. [/quote]

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

[quote name='virtorio' timestamp='1352750054' post='595314538'] [code] 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; [/code] [/quote]

Really appreciate it! :D

Share this post


Link to post
Share on other sites
  • 0

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...
[CODE]
$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]

Share this post


Link to post
Share on other sites
  • 0

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

Posted

[quote name='CrispCreations' timestamp='1352898529' post='595319306']
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...
[CODE]
$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]
[/quote]

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) { } ?>[/code]

Share this post


Link to post
Share on other sites
  • 0

Posted

foreach, while should only be used with SQL functions returning data, e.g.
[code]<?PHP
$Records = Array();
while ($Data = MySQL_Fetch_Assoc($RecordSet))
{
//Do something with $Data...
$Records[] = $Data;
}
print_r($Records);
?>[/code]

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.