durga Posted January 12, 2022 Share Posted January 12, 2022 Ex: $data = "foo:home,food,brand:tulasi"; list($user, $uid) = explode(":", $data); echo "kk".$uid."</br>"; i want store two values home and food into foo how to store guide me Quote Link to comment https://forums.phpfreaks.com/topic/314407-key-with-multiple-values-storing/ Share on other sites More sharing options...
ginerjm Posted January 12, 2022 Share Posted January 12, 2022 What do you think that explode is doing for you? Did you echo out your user and uid values after the explode Here is what I get: $data = "foo:home,food,brand:tulasi"; list($user, $uid) = explode(":", $data); echo "user is $user and uid is $uid"; exit(); Quote Link to comment https://forums.phpfreaks.com/topic/314407-key-with-multiple-values-storing/#findComment-1593311 Share on other sites More sharing options...
Phi11W Posted January 12, 2022 Share Posted January 12, 2022 (edited) 3 hours ago, durga said: $data = "foo:home,food,brand:tulasi"; Why reinvent a perfectly good Wheel? Compound data structures are already rather well catered for by [things like] JSON: $jsonText = '{"foo":["home","food"],"brand":"tulasi"}'; $json = json_decode( $jsonText ); echo count( $json->foo ); // 2 echo $json->brand; // 'tulasi' var_dump( $json ); //object(stdClass)#1 (2) //{ // ["foo"]=> array(2) // { // [0]=> string(4) "home" // [1]=> string(4) "food" // } // ["brand"]=> string(6) "tulasi" //} The problem with your code is that the comma character (",") is being used to separate both the top-level items (foo and brand) and the individual values of foo! The explode() function cannot tell the difference between these two usages of the comma character. Regards, Phill W. Edited January 12, 2022 by Phi11W Quote Link to comment https://forums.phpfreaks.com/topic/314407-key-with-multiple-values-storing/#findComment-1593314 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.