How can I remove the underlines from my post array elements? - php

I can't seem to find a simple solution. I'm trying to loop through a $_POST array like this:
foreach($_POST as $value => $key)
{
echo $value;
}
The values that I get back are all chained together with underline character like this:
Element_one_Element_two_Element_Three_Element_Four_submit
How can I get the array to simply display each elements without the underline and the appropriate break like this:
Element one
Element two
Element three
Element four
Because I'm creating and sending the contents of the post array dynamically to the database I don't to use hard code like $_POST["someArrayElementName'];
Thanks for any help!

This should work for you:
(Here I just simply use str_replace() to change all underlines to spaces and at the end I append a new line tag)
echo str_replace("_", " ", $value) . "<br />";

Related

How can I avoid printing the separator when elements are empty in the join()?

Here is my code:-
echo join(',', ['','']);
As you know, it prints just a comma. I want to avoid that. Because elements are empty and the only comma doesn't make any sense. Also the comma should be deleted in this:
echo join(',', ['sth','']);
// the expected result: sth
How can I do that?
Just remove empty items of the array. You can do that simply by using array_filter() function. So try this:
echo join(',', array_filter(['sth','']));
Online Demo

Can I use foreach loop to make Textarea

I am wondering if I can use foreach loop to generate textarea and also print values inside of the textarea. For example:
Group 1: textarea here with some value inside
Group 2: textarea here with some value inside
So let say I have this code to split and string and put into different group by length. How can I make textarea using foreach loop?
foreach($strArr as $v) {
$result["Group ".strlen($v)][] = $v;
}
Thank you!
As PHP Developer said, you can echo html tags out and set the values inside the text area.
A simple example of this using a variable would be:
foreach ($result_array as $result) {
echo '<textarea>'. $result .'</textarea>';
}

Returning string value in php array

Its a simple problem but i dont remember how to solve it
i have this array:
$this->name = array('Daniel','Leinad','Leonard');
So i make a foreach on it, to return an array
foreach ($this->name as $names){
echo $names[0];
}
It returns
DLL
It returns the first letter from my strings in array.I would like to return the first value that is 'Daniel'
try this one :
foreach ($this->name as $names){
echo $names; //Daniel in first iteration
// echo $names[0]; will print 'D' in first iteration which is first character of 'Daniel'
}
echo $this->name[0];// gives only 'Daniel' which is the first value of array
Inside your loop, each entry in $this->name is now $names. So if you use echo $names; inside the loop, you'll print each name in turn. To get the first item in the array, instead of the loop use $this->name[0].
Edit: Maybe it makes sense to use more descriptive names for your variables.
For example $this->names_array and foreach ( $this->names_array as $current_name ) makes it clearer what you are doing.
Additional answer concerning your results :
You're getting the first letters of all entries, actually, because using a string as an array, like you do, allows you to browse its characters. In your case, character 0.
Use your iterative element to get the complete string everytime, the alias you created after as.
If you only want the first element, do use a browsing loop, just do $this->name[0]. Some references :
http://php.net/manual/fr/control-structures.foreach.php
http://us1.php.net/manual/fr/language.types.array.php

How to get specific instance in PHP array?

I am trying to get the specific value of file extension in this array. All I can do so far is .
I am wanting the fileextention ".jpg"
All I know how to do is echo the values like so using foreach;
file_nameBob7213.jpg file_typeimage/jpeg
file_pathC:/xampp/htdocs/midas/records/
full_pathC:/xampp/htdocs/midas/records/Bob7213.jpg raw_nameBob7213
orig_nameBob72.jpg client_nameafasfafs.jpg **file_ext.jpg** file_size44.96
is_image1 image_width716 image_height474 image_typejpeg
image_size_strwidth="716" height="474"
I am only interested in retrieving the file_ext from this array. How do I select that exact thing?
foreach ($file['upload_data'] as $item => $value)
{
echo $item; echo $value; echo "<br/>";
}
How do I do this? , thanks!
$file['upload_data']['file_ext']
It's just an array within an array, so specify 2 array keys
Incidentally, if you want to see the contents of an array, a quick way of doing it is to use var_export:
var_export($file); # echoes the entire array
You don't need to write a foreach loop every time
$file['upload_data']['file_ext'] contains '.jpg'.

Rookie PHP question

I am hacking together a theme for wordpress and I am using the following code to pull out data from a custom field with several values:
<?php $mykey_values = get_post_custom_values('services');
foreach ( $mykey_values as $key => $value ) {
echo "<span>$value, </span>";
} ?>
I use a comma to seperate the results, but I don't want a comma after the last result. How do I get around this?
Best way is with implode:
echo('<span>' . implode('</span>, <span>', $mykey_values) . '</span>');
Many ways to do this... the first one I can think of is instead of using echo, concatenate all the results into a string, then remove the last , character.
Another way would be to use a for loop instead of foreach and then iterate to the size of $mykey_values - 1 and then print the last one without a ,. And I'm sure others will post other ways (maybe with real code too - my PHP is too rusty for me to risk a real code sample).
echo "<span>" . implode(',</span><span>',$mykey_values) . "</span>;
Edit: BTW, you don't use the loop if you use this code.

Categories