Viewed   63 times

What is the best method for converting a PHP array into a string?
I have the variable $type which is an array of types.

$type = $_POST[type];

I want to store it as a single string in my database with each entry separated by | :

Sports|Festivals|Other

 Answers

2

Use implode

implode("|",$type);
Sunday, September 4, 2022
4

A simple solution that comes to mind: combine array_filter and strpos;

$strings = array('Apple', 'Banana', 'Orange');
$substrings = array('pp', 'range');

$result = array_filter($strings, function($item) use($substrings) {
  foreach($substrings as $substring)
    if(strpos($item, $substring) !== FALSE) return TRUE;
  return FALSE;
});

To reset indices, you can use the array_values function.

Saturday, October 29, 2022
 
2

If you echo an array, it will either give you an error or just output "Array".

In your second example, $products[101] is also an array.

So you can do this:

foreach($products as $product){
    foreach($product as $key => $val){
        echo "$key: $val";
    }
}

Or use print_r to output the data in an array:

foreach($products as $product){
    print_r($product);
}
Saturday, December 24, 2022
5

Here is a way to do the job:

$in = array(
"%@-H-e-l-l-o-7-9#$%",
"Hi$73",
"????????!",
"!",
"",
"?55?W",
'$abc$$$',
"?????_",
"34.5",
'#_!',
);

foreach($in as $elem) {
    preg_match('/^([^pLpN]*)((?=[pLpN]|$)[^_]*(?<=[pLpN])|^)?([^pLpN]*)$/u', $elem, $m);
    printf("'%15s'%s'%10s't%s'%10s't%s'%10s'%s", "$elem","=> (1): ",$m[1],"(2): ",$m[2], "(3): ",$m[3],"n");

}

Where:

  • pL stands for any letter in any language
  • pN stands for any number in any language

Output:

'%@-H-e-l-l-o-7-9#$%'=> (1): '       %@-'   (2): 'H-e-l-l-o-7-9'    (3): '       #$%'
'          Hi$73'=> (1): '          '   (2): '     Hi$73'   (3): '          '
'????????!'=> (1): '          ' (2): '????????' (3): '         !'
'              !'=> (1): '         !'   (2): '          '   (3): '          '
'               '=> (1): '          '   (2): '          '   (3): '          '
'        ?55?W'=> (1): '          ' (2): '   ?55?W' (3): '          '
'        $abc$$$'=> (1): '         $'   (2): '       abc'   (3): '       $$$'
'    ?????_'=> (1): '          '    (2): '?????'    (3): '         _'
'           34.5'=> (1): '          '   (2): '      34.5'   (3): '          '
'            #_!'=> (1): '       #_!'   (2): '          '   (3): '          '
Friday, September 16, 2022
 
2

This should work with PHP 7:

class foo {
var $bar = 'I am bar.';
}

$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo "{$foo->$bar}n";
echo "{$foo->{$baz[1]}}n";

This is caused because in PHP 5 the following line:

echo "{$foo->$baz[1]}n";

is interpreted as:

echo "{$foo->{$baz[1]}}n";

While in PHP 7 it's interpreted as:

echo "{{$foo->$baz}[1]}n";

And so in PHP 7 it's passing the entire array to $foo instead of just that element.

Thursday, September 8, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :