ArnisR Posted June 11, 2012 Report Share Posted June 11, 2012 Ir multidimensionālais masīvs: $myArray = array( array( "name" => "Jack" "age" => 28 ), array( "name" => "Harry" "age" => 35 ), array( "name" => "Mike" "age" => 19 ) ); Kā ar PHP izdzēst array elementu pēc name vērtības, pietam atjaunojot visa masīva index'us - 0; 1; 2; 3 utt. ??? Paldies! Quote Link to comment Share on other sites More sharing options...
codez Posted June 11, 2012 Report Share Posted June 11, 2012 (edited) Var kaut kā tā: http://codepad.org/z8o88yzc $myArray = array( array( "name" => "Jack", "age" => 28 ), array( "name" => "Harry", "age" => 35 ), array( "name" => "Mike", "age" => 19 ) ); class KeyFilter{ function __construct($key){ $this->key=$key; } function byKey($v){ return !($v[$this->key]==$this->val); } function filter($arr,$val) { $this->val=$val; return array_values(array_filter($arr,array($this,"byKey"))); } } var_dump($myArray); $filter = new KeyFilter("name"); $myArray=$filter->filter($myArray,"Harry"); var_dump($myArray); Edited June 11, 2012 by codez Quote Link to comment Share on other sites More sharing options...
gurkjis Posted June 11, 2012 Report Share Posted June 11, 2012 vienkāršāks variants: http://codepad.org/E78DkzOY <? $myArray = array( array( "name" => "Jack", "age" => 28 ), array( "name" => "Harry", "age" => 35 ), array( "name" => "Mike", "age" => 19 ) ); function del_by_name($a, $name) { foreach ($a as $k => $v) { if ($v['name'] == $name) { unset($a[$k]); return array_merge($a); } } return $a; } var_dump($myArray); $myArray = del_by_name($myArray, 'Harry'); var_dump($myArray); ?> Quote Link to comment Share on other sites More sharing options...
ArnisR Posted June 12, 2012 Author Report Share Posted June 12, 2012 Paldies, problēma atrisināta! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.