Jump to content

how to convert comma separated string to array in array


irandoct

Recommended Posts

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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"}]]
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
                )

        )

)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.