0

I have the following code:

  foreach ($result as $row) {
$categories[] = array('id' => $row['id'], 'name' => $row['name']);
if($categories['id'] = $selectedcategory ){ 
    $categories['selected'] = 'true'; 
}

This creates an array with all id and names from a query. And it's ok. Than it does a check against a specific value ($selectedcategory) and add a key 'selected' with value 'true'.

When I do print_r to $categories I get:

Array ( [0] => Array ( [id] => 1 [name] => Expertise ) [selected] => true [1] => Array ( [id] => 2 [name] => Tools ) [2] => Array ( [id] => 3 [name] => On the go ) [3] => Array ( [id] => 4 [name] => Lab ) [4] => Array ( [id] => 5 [name] => Know How ) ) 

It puts the selected key to the right id value (1) but it is placed out of the Array 0 brackets.

What am I doing wrong?

3 Answers 3

1

Your if condition i wrong,

you are using single = to

it should be

if($categories['id'] == $selectedcategory ){ 
    $categories['selected'] = 'true'; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it's true but you know what? with = it get's the right item but it puts it in the wrong place while with == it gives me an 'Notice: Undefined index: id in /media/dati/siti/ipq/admin/news/index.php'
1

when you add an element to and array using [] you create a new position in this array and add all the information inside it.

$categories[] = array('id' => $row['id'], 'name' => $row['name']);  

This means that $categories[$pos] contains the array you created with an id and a name, and you cant compare against $categories['id'] because this index doesn't exist, you should compare with this

$categories[$pos]['id']  

For this you need to know in what position have you added the new array, or assume it's the last array element

Update: Here you have a code example with @meze suggestion

foreach ($result as $row) {
  $tmp = array('id' => $row['id'], 'name' => $row['name']);
  if($tmp['id'] == $selectedcategory ){ 
    $tmp['selected'] = 'true'; 
  }  
  $categories[] = $tmp;  
}

3 Comments

Better - use a temporary array and assign it in the end of each iteration.
I am in the foreach loop so it adds the value to the right position: the point is that it is outside the () brackets of that position. [0] => Array ( [id] => 1 [name] => Expertise ) [selected] => true. The value is in the right array element but outside the ()
This is because you are adding 'selected' to $categories array, not to some category array position $category['selected'] its different from $category[$pos]['selected']
0

use like this..

foreach ($result as $row) {
     if($row['id'] == $selectedcategory ){ 
       $selected = 'true';
     else 
       $selected = 'false';
     $categories[] = array('id' => $row['id'], 'name' => $row['name'],'selected'=>$selected);

    }

3 Comments

Print_r keeps doint the same... I am building a radio button set out of the array and i want the true item to be selected
Thanks Rajeev but print_r gives [0] => Array ( [id] => 1 [name] => Expertise ) [selected] => true while should be [0] => Array ( [id] => 1 [name] => Expertise [selected] => true) to work
Thanks, this solved the problem... didn't know why I was only thinking to add selected to one item instead that all!