Jump to content

Help with arrays


playaz

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 :)
Link to comment
https://forums.phpfreaks.com/topic/4017-help-with-arrays/
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
Link to comment
https://forums.phpfreaks.com/topic/4017-help-with-arrays/#findComment-13960
Share on other sites

Archived

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

×
×
  • Create New...

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.