How to trim white spaces of array values in php


 
Many time we want to trim array value dynamically. But we always go with foreach and trim that value.

If we are not aware from “array_map” function then we used simply foreach function and used trim function.

But its better to use following function. It will directy trim all value of array.

$arr = array('  Jainish ',' S  ','            Senjaliya ');
		
$trimmed_array  =  array_map('trim',$arr);

print_r($trimmed_array);

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya

Advertisements

How to read stdClass object in php


 
How would I read this array (“stdClass Object”)

Object is not an array, but rather, well, an Object. So use the -> operator to access its properties:

If you currently have the following object:- [If you print your data and it will look like below]

stdClass Object
(
    [name] => Jainish Senjaliya
    [contact] => Array
        (
            [0] => stdClass Object
                (
		    [id] => 143
                    [email] => jainish.online@gmail.com
                    [URL] => http://www.hariomrubber.com
                    [phone] => 99******93
                )
        )
) 

For example, to get the name, you would do:

echo $data->name;
Output : Jainish Senjaliya

It contains a property which itself is an array of additional objects. For example, to get the URL of id 143, you would do:

echo $data->contact[0]->URL;

Output : http://www.hariomrubber.com

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya