2

Ok then. I have a filled array defined like this:

$subs = array();
for (...) {
 // extracting of variables $knowledgeLevel, $name, $short, $long
 $subs [] = array (
 'knowledgeLevel' => $knowledgeLevel,
 'name' => $name,
 'shortTheory' => $short,
 'longTheory' => $long
 );
}

then I receive a value $kl, and I want to eventually delete from the array $subs the element that has the knowledgeLevel equal to $kl. I've tried to do so:

foreach ( $subs as $subject ) {
 if ($subject['knowledgeLevel'] == $kl) {
 unset ($subject);
 }
}

and so

foreach ( $subs as $subject ) {
 if ($subject['knowledgeLevel'] == $kl) {
 unset ($subject['knowledgeLevel']);
 unset ($subject['name']);
 unset ($subject['shortTheory']);
 unset ($subject['longTheory']);
 }
}

but these don't seem to work.

Sina R.
1,7782 gold badges19 silver badges38 bronze badges
asked May 11, 2013 at 8:26
4
  • 1
    Where is $kl defined? Commented May 11, 2013 at 8:30
  • You want to remove an element from $subs array? It's a numeric array, not associative. Its elements are associative arrays. Commented May 11, 2013 at 8:33
  • I have two functions that use the same array (declared as global). $klis the parameter of the second function Commented May 11, 2013 at 8:35
  • @Mojtaba sorry! Edited Commented May 11, 2013 at 8:37

2 Answers 2

3

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

Use & with foreach ( $subs as &$subject )

foreach ( $subs as &$subject ) 
 if ( $subject['knowledgeLevel'] == $kl ) 
 unset ($subject);

Or try this:

This form will additionally assign the current element's key to the $key variable on each iteration.

foreach ( $subs as $key => $subject )
 if ( $subject['knowledgeLevel'] == $kl ) 
 unset( $subs[$key] );
answered May 11, 2013 at 8:33

4 Comments

there is no diffrent but I my self choose the second. because its clear.
@Jannuzzo . welcome to stackoverflow. and let me remind you somthing. lets be a good supporter with upvoting and accepting useful answers.(-:
@imsiso 'vote up' privilege requires 15 reputation. OP has only 6.
@AndreyVolk I know but I say it not for now. for all time but he can still accept the answers. btw +1 to you.
0

$subject is not reference to $subs it's new variable and with unset($subject['anything']) you are not deleting anything from $subs

Use for loop:

for($i=0;$i<count($subs);$i++) {
 if ($subs[$i]['knowledgeLevel'] == $kl) {
 unset ($subs[$i]);
 }
}
Sina R.
1,7782 gold badges19 silver badges38 bronze badges
answered May 11, 2013 at 8:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.