Jump to content


Photo

Help with arrays


  • Please log in to reply
2 replies to this topic

#1 playaz

playaz
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 03 March 2006 - 03:37 PM

Hi guys,

Only a few hours til the end of the day and my mind has gone blank and im struggling with some arrays.
I have a multi-dimensional array, shown below:

Array ( [0] => Array ( [category_id] => 101 [parent_id] => 75 [category] => Microsoft ) [1] => Array ( [category_id] => 100 [parent_id] => 75 [category] => Sun  ) [2] => Array ( [category_id] => 102 [parent_id] => 75 [category] => Oracle ) )

I want to grab the 'category_id' for each single element in the array and place this into a variable with a comma seperator.

So basically my array above should place the values of 'category_id' into a variable called $in. This variable should be as follows:

print $in // outputs '101, 100, 102'

Can someone show me how to do this please my brain just seems to have died on me today :)

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 03 March 2006 - 06:16 PM

try something like this:
$data = array ( 
  [0] => array ( 
    ['category_id'] => 101, 
    ['parent_id'] => 75, 
    ['category'] => Microsoft ),
  [1] => array ( 
    ['category_id'] => 100,
    ['parent_id'] => 75,
    ['category'] => Sun  ),
  [2] => array ( 
    ['category_id'] => 102,
    ['parent_id'] => 75,
    ['category'] => Oracle )
  );
$cats = array();
foreach ($data as $x) {
  $cats[] = $x['category_id'];
}

$in = implode(', ', $cats);

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 playaz

playaz
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 06 March 2006 - 10:12 AM

[!--quoteo(post=351385:date=Mar 3 2006, 06:16 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Mar 3 2006, 06:16 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]

hope this helps
[/quote]

Cheers mate its much appreciated keep up the good work :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users