irandoct Posted October 4, 2017 Share Posted October 4, 2017 (edited) Hi, I need to convert comma separated values to array in array. My original string items separated with two delimiters ( | and ,). I used the following function for this purpose. function makeKeyboard($str){ $array = array(); $array = explode('|', $str); foreach($array as $key => $str) { $array[$key] = explode(',', $str); } return $array; } $aaaa = "item1|item2,item3|item4"; echo makeKeyboard($aaaa); //////////// I want the output value to be as the following format: [ [['text'=>'item1']], [['text'=>'item2'],['text'=>'item3']] , [['text'=>'item4']] ] can you please suggest a solution thanks mansour Edited October 4, 2017 by irandoct Quote Link to comment Share on other sites More sharing options...
maxxd Posted October 4, 2017 Share Posted October 4, 2017 What are you getting now that isn't what you're looking for? Running your code works fine here - of course, it results in a numerically indexed array but then again you can't have multiple indexes of 'text' within an array so I assume that's not a problem. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 4, 2017 Share Posted October 4, 2017 And - just where is this "text" value coming from? Certainly not the input array. Quote Link to comment Share on other sites More sharing options...
irandoct Posted October 4, 2017 Author Share Posted October 4, 2017 Hi, Thank you for your reply. You are right. however, I need the array key name (TEXT) to be printed in the result. I need the result to be in the following format: [ [['text'=>'item1]], [['text'=>'item2'],['text'=>'item3']] , [['text'=>'item4']] ] Thank You Quote Link to comment Share on other sites More sharing options...
maxxd Posted October 4, 2017 Share Posted October 4, 2017 If every index is 'text', then you're only going to have one value because it's going to be overwritten every time. Right now you'll see numbers starting at 0 and increasing incrementally from there. That's your array index - if you need an associative array, you'll need to specify the unique name of every index inside the loop. Quote Link to comment Share on other sites More sharing options...
Sepodati Posted October 4, 2017 Share Posted October 4, 2017 If you literally want 'text' as keys, no, it's not going to happen. makeKeyboard()... what are you actually trying to do here? I mean as a whole, what's the objective of the code? Do you have control over the inputs and outputs? Is this feeding into some API/widget to actually make a keyboard? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 4, 2017 Share Posted October 4, 2017 Seems ludicrous creating arrays with single items, but here goes $input = 'item1|item,item3|item4'; function makeKeyboard ($str) { $result = []; $arr1 = explode('|', $str); foreach ($arr1 as $k1=>$b) { $arr2 = explode(',', $b); foreach ($arr2 as $c) { $result[$k1][] = ['test'=>$c]; } } return $result; } $a = makeKeyboard($input); echo json_encode($a); //--> [[{"test":"item1"}],[{"test":"item"},{"test":"item3"}],[{"test":"item4"}]] 1 Quote Link to comment Share on other sites More sharing options...
irandoct Posted October 4, 2017 Author Share Posted October 4, 2017 If you literally want 'text' as keys, no, it's not going to happen. makeKeyboard()... what are you actually trying to do here? I mean as a whole, what's the objective of the code? Do you have control over the inputs and outputs? Is this feeding into some API/widget to actually make a keyboard? Hi, Yes, I want to send the makekeyboard() result to a Telegram bot. The input values come from a table of my database and are absolutely under my control. (telegram docs here: https://core.telegram.org/bots/api#inlinekeyboardmarkup) I can create the keyboard objects each time I post to the bot. but I want to make keyboard Items dynamically without hard coding. Thank You Quote Link to comment Share on other sites More sharing options...
Sepodati Posted October 5, 2017 Share Posted October 5, 2017 Do you have a code example that works when you hard code it? From a brief read, I think this gets you what you want. You need an array of Keyboard buttons, which are themselves an array with a "text" fields. Plus some optional fields I assume you're not using. Play with it here: https://3v4l.org/sXqWI <?php function makeKeyboard($str) { $kb = array(); $kb_row = explode('|', $str); foreach($kb_row as $row) { $buttons = explode(',', $row); $newrow = array(); foreach($buttons as $text) { $newrow[] = array('text' => $text); } $kb[] = $newrow; } return $kb; } $test = "button1.1|button2.1,button2.2,button2.3|button3.1|button4.1,button4.2"; $keyboard = makeKeyboard($test); print_r($keyboard); ?>output: Array ( [0] => Array ( [0] => Array ( [text] => button1.1 ) ) [1] => Array ( [0] => Array ( [text] => button2.1 ) [1] => Array ( [text] => button2.2 ) [2] => Array ( [text] => button2.3 ) ) [2] => Array ( [0] => Array ( [text] => button3.1 ) ) [3] => Array ( [0] => Array ( [text] => button4.1 ) [1] => Array ( [text] => button4.2 ) ) ) 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.