| Autor | Zpráva | ||
|---|---|---|---|
| BedyR Profil |
Dobrý den,
potřebuji do vícerozměrného pole doplňovat nebo rušit položky. Příklad: <?php
function dejZbytekA($aP=array(),$cesta="",$test="") {
$data = array();
if (count($aP,COUNT_RECURSIVE)>1) {
echo "[aP] má ".count($aP,COUNT_RECURSIVE)." položek<br><pre>".print_r($aP,true)."</pre>";
foreach($aP as $key => $val) {
$cestap = $cesta;
$cestap .= '["'.$key.'"]';
echo "srovnávám ".$test." a ".$key."<br><hr>";
if($key==$test) { $data[$key] = $aP[$key]; $data['cesta'] = $cestap ; }
else { if(count($val)>0) $data = dejZbytekA($val,$cestap,$test); }
if(count($data)>0) { break; }
}
}
return $data;
}
function array_insert (&$array, $position, $insert_array) {
$first_array = array_splice ($array, 0, $position);
$array = array_merge ($first_array, $insert_array, $array);
}
$a['a0'] = "";
$a['a0']['b1']['c2'] = "";
$a['a0']['b1']['c2']['e3'] = "";
$a['a0']['b1']['c2']['f3'] = "";
$a['a0']['b1']['d2'] = "";
$a['a0']['b1']['d2']['h3'] = "";
$a['a0']['b1']['d2']['i3'] = "";
$a['a0']['b1']['g2'] = "";
$test = "c2";
$dopl = array("j3"=>"");
$cesta = "";
$data = dejZbytekA($a,$cesta,$test);
echo "data<pre>".print_r($data,true)."</pre><hr>";
array_insert($data[$test], count($data[$test]), $dopl);
echo "data[".$test."] po vložení<pre>".print_r($data[$test],true)."</pre><hr>";
${"a".$data['cesta']} = $data[$test];
echo "výsledek [a] má ".count($a,COUNT_RECURSIVE)." položek<br><pre>".print_r($a,true)."</pre><hr>";
?>vše funguje až na řádek : ${"a".$data['cesta']} = $data[$test];$a=5;
$b="a";
${$b} = 10;
echo $a;Nakopli byste mne někdo? Prosím. |
||
| tiso Profil |
function addItem(&$arr, $key, $item) {
foreach ($arr as $k => &$v) {
if ($k == $key) {
$v = array_merge($v, $item);
break;
}
if (is_array($v)) {
addItem($v, $key, $item);
}
}
}
//edit:
addItem($a,$test,$dopl);
print_r($a); |
||
| BedyR Profil |
tiso:
Díky, tohle funguje addItem($a,$test,$dopl); echo "[a] po úpravě<pre>".print_r($a,true)."</pre><hr>"; $a['a0']['b1']['c2']['f3'] = "";
$b['a0']['b1']['c2']['f3'] = "";
$cesta ="['a0']['b1']['c2']['f3']";
$pole = "a";
${$pole.$cesta} = "test a";
$pole = "b";
${$pole.$cesta} = "test b"; |
||
| tiso Profil |
BedyR: dá sa to napríklad takto:
$path = array('a0', 'b1', 'c2', 'f3');
substituteArrayItem($a, $path, 'test a');
substituteArrayItem($b, $path, 'test b');
print_r($a);
print_r($b);
function substituteArrayItem(&$arr, $path, $value) {
$key = array_shift($path);
if (empty($path)) {
$arr[$key] = $value;
} else {
substituteArrayItem($arr[$key], $path, $value);
}
} |
||
| BedyR Profil |
#5 · Zasláno: 9. 12. 2015, 15:53:25
tiso:
Super, já tam pořád tlačil řetězec. To bylo to nakopnutí. Díky |
||
|
Časová prodleva: 11 let
|
|||
0