Jump to content

Archived

This topic is now archived and is closed to further replies.

playaz

Help with arrays

Recommended Posts

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:

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

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

Share this post


Link to post
Share on other sites
try something like this:
[code]
$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);
[/code]

hope this helps

Share this post


Link to post
Share on other sites
[!--quoteo(post=351385:date=Mar 3 2006, 06:16 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Mar 3 2006, 06:16 PM) [snapback]351385[/snapback][/div][div class=\'quotemain\'][!--quotec--]

hope this helps
[/quote]

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.