PHP String in Array Only Returns First Character

PHP String in Array Only Returns First Character

You pretty much answered your own question. $page is "files" (as shown by your first var_dump). This could be caused by the deprecated register_globals, or a manual approximation thereof. Given that,

$page['files']

is "f". This is because non-numeric strings are implicitly converted to 0 (!). You can reproduce this easily with:

$page = 'files';
echo $page['files'];

My Array variable will return only the first character

My advice would be to handle this client side first. Without knowing the exact structure of your array or getting in to breaking it apart into separate query parameters, it's fairly easy to just encode the whole array as json and then decode it server side. Try something like this:

xhttp.open("GET", "ajax/saveSAWorkSched.php?studRegInfo="+encodeURIComponent(JSON.stringify(saveStudRegInfo)));

Then in php you can decode from json:

$studPerInfo = json_decode($_GET['studRegInfo'],1);

Array value only returning first character and extra values

You don't need this loop:

    foreach($pizza as $return)
{
echo $return[des] .'<br>';
}

Here, $pizza is actually a topping array for one topping, and you only want the description from it. However, you're looping over every element, and then trying to deference it as an array -- which it's not. So just replace the above code with this:

echo $pizza['des'];

Note, aways quote array indexes like this: $pizza['des']

Don't do this: $pizza[des]

Echoing a PHP array is only displaying the first letter of each value

You have to do:

foreach($user_rating as $category) {
echo $category['description'] . "<br />";
}

Explanation of your code:

Your array is like

$user_rating = array(array('percent' => ..., 'description' => ...), array(...));

That means you have a two-dimensional array.

When you do foreach($user_rating as $category), you are looping over the outer array. That means $category will be an array, namely, array('percent' => ..., 'description' => ...).

Now you make the mistake, to loop over that array again, which means that $description will always be the string value of percent and description.

In PHP you can access strings with array notation to get the character at that position. As description is not defined it probably resolves to 0 and you get the first character.

$foo = 'bar';
echo $foo[0];
// echos 'b'

By just adding some echos, you can see, which element in your loop contains which value:

$user_rating[0] = array('description' => 'Time Management');
$user_rating[1] = array('description' => 'Attract Clients');
$user_rating[2] = array('description' => 'Internet Marketing');

foreach($user_rating as $category) {
echo '$category: ' . $category . PHP_EOL;
foreach($category as $description) {
echo '$description: '. $description . PHP_EOL;
echo $description[description] . "<br />" .PHP_EOL;
}
}

gives

$category: Array
$description: Time Management
T<br />
$category: Array
$description: Attract Clients
A<br />
$category: Array
$description: Internet Marketing
I<br />

PHP Array only returning first character of string

Both Dr. Molle and Douglas are correct, but I'd like to expand a little on their answers.

$_POST is an array. It can contain a variety of values, either in a key => value structure or in a series of values indexed numerically. Each value can be any other type of data. In this case, your variable:

$_POST['chapter']

is a string, most likely - PHP interprets the array index ([0], etc) as a character index when used against a string (or against a variable that can be trivially cast to string, like an integer or float.) This is why you're getting the first letter out instead of the first value: there's only the one value to get!

Of course, you might run into situations where the value of an array is itself an array - in which case you could access the value by typing, say, $array[0]['candles'] or something, but that's not going to happen with the $_POST array (which is always an array of strings.)

Only getting the first character of the array element

From your array the foreach loop should look like this

$toilet_arr = array ( 0 => 'Water Sealed', 1 => 'Open Pit', 2 => 'None' );

if (count($toilet_arr)) {
foreach($toilet_arr as $row) {
$data = array("hof_id"=>$last_id,"toilet_type"=>$row);
$this->db->insert('toilet_tbl',$data);
}
}

This should insert values as they are if the problem still persists check the length in your database for column toilet_type. You need to set varchar(250) for column toilet_type

PHP foreach array returns only the first character

This is looping every item in the inner arrays:

foreach ($values as $value) {
$filetmp = $value[0];

In the first loop, for example, $value is /storage/ssd3/334/5218334/tmp/php2swaoM. When you do $value[0], it'll return the first character.

That's what you did wrong.

I believe you can omit the inner loop if all inner arrays will have a fixed 3 items.

Getting the first character of a string with $str[0]

Yes. Strings can be seen as character arrays, and the way to access a position of an array is to use the [] operator. Usually there's no problem at all in using $str[0] (and I'm pretty sure is much faster than the substr() method).

There is only one caveat with both methods: they will get the first byte, rather than the first character. This is important if you're using multibyte encodings (such as UTF-8). If you want to support that, use mb_substr(). Arguably, you should always assume multibyte input these days, so this is the best option, but it will be slightly slower.

how to get first character from array in php

hope this will work for you -

$my_array = array("Alpha","Aloo","Amakeaviral","Boki","Bone");
$newArray = array();
foreach($my_array as $value) {
$first_char = $value[0];
if (!empty($newArray)) {
$flag = false;
foreach ($newArray as $key => $val) {
if ($first_char == $key){
$newArray[$key][] = $value;
$flag = true;
}
}
if (!$flag) {
$newArray[$first_char][] = $first_char;
$newArray[$first_char][] = $value;
}
} else {
$newArray[$first_char][] = $first_char;
$newArray[$first_char][] = $value;
}
}
var_dump($newArray);

Same as above but shortened code:

$my_array = array("Alpha","Aloo","Amakeaviral","Boki","Bone");
$newArray = array();
foreach($my_array as $value) {
if (empty($newArray[$value[0]])){
$newArray[$value[0]][]=$value[0];
}
$newArray[$value[0]][] = $value;
}
var_dump($newArray);


Related Topics



Leave a reply



Submit