krisis007 Posted October 26, 2011 Report Share Posted October 26, 2011 Sveiki. Man jatājums Jums, lielajiem profiņiem, kas noteikt man mācēs paskaidrot kā dabūt ko tādu gatavu. Pieņemsis ka man ir array. $array = array('a'=>array('b'=>array('c'=>'d'))); un es gribu iegūt vērtību $array['a']['b']['c'] BET vajadzētu tā kad es viņu varu iegūt šādi: $str = "['a']['b']['c']"; echo $array.$str; Pašreizējais piemērs izvada "Array['a']['b']['c']", bet nevis "d" kā vajadzētu. Paldies. Quote Link to comment Share on other sites More sharing options...
daGrevis Posted October 26, 2011 Report Share Posted October 26, 2011 $string = "{$x['y']['z']}"; echo $string; Quote Link to comment Share on other sites More sharing options...
briedis Posted October 26, 2011 Report Share Posted October 26, 2011 Drīzāk ir jātiek galā ar šāda risinājuma nepieciešamību. Kur rodas šāds: "$str = "['a']['b']['c']";" ? Noteikti var labāk! Quote Link to comment Share on other sites More sharing options...
daGrevis Posted October 26, 2011 Report Share Posted October 26, 2011 Tas arī. Ja nezināji, vari darīt šādi: $x[$y]; Quote Link to comment Share on other sites More sharing options...
krisis007 Posted October 26, 2011 Author Report Share Posted October 26, 2011 (edited) nu pieņemsim man ir usera ievadīta vērtība $_POST['arr']; šādā formātā : "a|b|c" tad es ar explode('|',$_POST['arr']); iegūstu array ar trīs vērtībām `a`,`b`,`c` un tad ar foreach($arr as $arr){ $str .= '[\''.$arr.'\']'; } iegūstu ka $str = '['a']['b']['c']'; un tālāk jau kā pirmajā postā Edit: gribu pārbaudīt vai tādā vērtība pastāv un ja jā echo tās vērtību. Paldies. Edited October 26, 2011 by krisis007 Quote Link to comment Share on other sites More sharing options...
krisis007 Posted October 26, 2011 Author Report Share Posted October 26, 2011 $string = "{$x['y']['z']}"; echo $string; Paldies par ideju, bet šo biju mēģinājis, man vajag ka $string = "{$x['y']['z']}"; šīs ['y']['z'] vērtības ir dinamiskas un var mainīties atkarībā no ievades. Quote Link to comment Share on other sites More sharing options...
daGrevis Posted October 26, 2011 Report Share Posted October 26, 2011 <input type="text" name="stuff[]" /> <input type="text" name="stuff[]" /> <input type="text" name="stuff[]" /> <input type="text" name="stuff[]" /> <?php foreach ($_POST['stuff'] as $stuff) { echo $stuff . "\n"; } ?> Ui, neizlasīju īsti ko tev vajag. Sanāk, ka ir inputs, kur ir jāievada kkas, bet lietotājam tas, ko viņš ievada, pašam ir jāatdala ar '|'? <input type="text" name="stuff" /> <?php $stuff = explode('|', $_POST['stuff']); // Shajaa briidii es saprotu, ka nesaprotu, ko tu gribi panaakt... foreach ($stuff as $value) { echo $value . "\n"; } ?> Quote Link to comment Share on other sites More sharing options...
krisis007 Posted October 26, 2011 Author Report Share Posted October 26, 2011 Vienu variantu izdomāju pats, ja Jums ir kādas idejas tad padodie tās, bet ja nav tad paldies atbildētājiem. Quote Link to comment Share on other sites More sharing options...
krisis007 Posted October 26, 2011 Author Report Share Posted October 26, 2011 <input type="text" name="stuff[]" /> <input type="text" name="stuff[]" /> <input type="text" name="stuff[]" /> <input type="text" name="stuff[]" /> <?php foreach ($_POST['stuff'] as $stuff) { echo $stuff . "\n"; } ?> Ne, ne, ne inputs būtu tikai viens, un tas array būtu definēts php vidē. Mans pažreizējais variants: $arr = array('a'=>array('b'=>array('c'=>'d'))); $ievade = 'a|b|c'; $str = explode('|',$ievade); foreach($str as $str){ if(isset($output,$output[$str])&&is_array($output)){ $output = $output[$str]; } elseif(isset($arr[$str])) { $output = $arr[$str]; } else { $output = false; } } echo ((is_string($output))?$output:'NaN'); Quote Link to comment Share on other sites More sharing options...
krisis007 Posted October 26, 2011 Author Report Share Posted October 26, 2011 Pieņemam ka $ievade = $_POST['kaukas'] vienvārdsakot kaut kas ko useris iepoustojis. 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.