Login | Register | FAQ
Anonymous

Searching and formatting an array

+ Post a reply

6 posts Page 1 of 1


Searching and formatting an array

by comport9 » Sat Jun 08, 2013 8:05 pm

Just getting back into working on my website and have forgot how to do a few basic things.

I want to have a list (or array) in my database concerning "friends" or "contacts". Here's how I want to have it set up:

(('Friends', (12, 22, 1345)),('Business', (24, 1999)))

The first element in the list will be the name of the contacts category, the second will be a list of the ID's of the people within that category. How do I cycle through everyone so that I can get the information easily?

$group = "Name of Group"
$person = "ID of person in group"

Thanks!
User avatar

comport9

  • Posts: 28
  • Joined: Sun Nov 04, 2012 5:35 pm

Re: Searching and formatting an array

by XainPro » Tue Jun 11, 2013 9:42 am

well you can to loop
for each and build your array in that loop
i.e

foreach($records as record)
{
$array[] = array("type" => $record["type"], "id" = $record["id"]);
}
etc
show your complete code what you have done so far to get a clear picture what you really wanted
User avatar

XainPro

  • Posts: 3933
  • Joined: Fri Feb 17, 2012 8:10 pm

Re: Searching and formatting an array

by comport9 » Fri Jun 14, 2013 3:09 am

Code: Select all
$list = array(
   array('Friends',12, 22, 1345),
   array('Business',24, 1999)
   );

foreach ($list as $contact)
{
   foreach ($contact as $person)
   {
      if (is_string($person))
      {
         echo "<br>Circle: $person.<br>";
      }
      else
      {
         echo "ID of person is $person.<br>";
      }
   }
}


That's what I did. (As a test). I'd just have to add calls to my database. But I think that would work no? However, how do I save it to the database? Do I literally put in "array('Friends',12, 22, 1345)...", or can I just put a list such as (('Friends',12, 22, 1345),('Business',24, 1999)). The latter would be my preference...

Yeah, your way looks a lot better and cleaner.
User avatar

comport9

  • Posts: 28
  • Joined: Sun Nov 04, 2012 5:35 pm

Re: Searching and formatting an array

by XainPro » Fri Jun 14, 2013 1:13 pm

you can save array by using a function called
serialize()
http://php.net/manual/en/function.serialize.php
User avatar

XainPro

  • Posts: 3933
  • Joined: Fri Feb 17, 2012 8:10 pm

Re: Searching and formatting an array

by comport9 » Fri Aug 16, 2013 5:54 pm

Thanks! As a stupid follow up question: how do I change/add/delete a value in my arrays?

For example, what if I wanted to change "Friends" to "Enemies", or drop a contact from one of them.

I either get error's, or nothing seems to change, when I try and change anything from within my foreach loops.
User avatar

comport9

  • Posts: 28
  • Joined: Sun Nov 04, 2012 5:35 pm

Re: Searching and formatting an array

by XainPro » Fri Aug 16, 2013 8:24 pm

// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);


http://php.net/manual/en/function.str-replace.php
User avatar

XainPro

  • Posts: 3933
  • Joined: Fri Feb 17, 2012 8:10 pm


+ Post a reply

Page 1 of 1