How to format a simple PHP array of strings? - php

I have this simple function which I pass in an array of strings:
function myfunction( $arg = array() )
{
// do stuff to $arg...
// return a $string;
}
Simple so far, but I need some of the strings in the $arg array to be formatted, while some remain unformatted. I can't figure out how to do it?
Say I run this $arg through myfunction():
echo myfunction( array( 'format me!', 'do not format me!' ) );
My tiny little brain can't figure out how to tell myfunction() that the first value in $arg array needs to have formatting, and it should not format the second value.
I thought about an associative array, but I think that could be the wrong approach because of having identical indexes.
echo myfunction( array( 'format' => 'hi', 'format' => 'bye', 'noformat' => 'foo');
Just looking for a "nudge" in the right direction.
EDIT 1:
Forgot to mention, I can only have one $arg array because I need the keys to be in a specific order.
EDIT 2:
The $arg array can have as many keys as the user wants.

You can do:
function myfunction(array $format, array $noformat) {
...
}
or
function myfunction(array $strings) {
foreach ($strings['format'] as $format) {
// do stuff
}
foreach ($strings['noformat'] as $noformat) {
// do stuff
}
}
with:
myfunction(array(
'format' => array('one', 'two', 'three'),
'noformat' => array('four', 'five', 'six'),
));
If (and only if) the strings are unique you can put them in the key instead of the value:
$strings = array(
'one' => true,
'two' => false,
'three' => false,
'four' => true,
);
myfunction($strings);
with:
function myfunction(array $strings) {
foreach ($strings as $k => $v) {
if ($v) {
// format string
}
}
}
But since you can't have duplicate keys this method falls down if you have repeated strings.

Instead of having myfunction() take an array as an argument, why not just have it take a single element as the argument. Then you can use array_map to process each element of the array.
Sort of like this:
function myfunction($element) {
if( do-formatting) {
//do your formatting stuff
$element = formatted-stuff
}
return $element
}
$arr = array("format me", "don't format me");
//This calls myfunction on each element of the array
//A new array is returned where each element has been replaced
//by the return value from myfunction.
$arr = array_map('myfunction', $arr);

Here is how I would to it.
The implementation of __tostring is not mandatory but is syntax sugar inside the myFunction.
<?php
class MyString{
private $_value;
public $to_format;
public function __construct($str, $to_format = true){
$this->_value = $str;
$this->to_format = $to_format;
}
public function __tostring(){
return $this->_value;
}
}
$args = array( new MyString('format me'), new MyString('Not me!', false) );

This is oddly similar to how table rows are formatted (with a different formatting function for each row). I would provide the data as a single array, then provide formatting information as a second array based on keys. For instance:
function rowFormatter(Array $data, Array $format=array()) {
ob_start();
foreach ($data as $key => $value) {
if (isset($format[$key]))
echo call_user_func($format[$key],$value);
else
echo $value;
}
return ob_get_clean();
}
function makeBold($x) { return '<b>'.$x.'</b>'; }
rowFormatter(array( "Hello", "Mister" ), array( 0 => 'makeBold' ));

I don't know what these strings you are formatting actually represent, so I used generic naming on these classes:
class LineItem
{
var $value;
function __construct($val)
{
$this->value = $val;
}
function __toString()
{
return $this->value;
}
function getFormatted( $formatter )
{
return $this->value;
}
}
class FormattedLineItem extends LineItem
{
function getFormatted( $formatter )
{
return $formatter( $this->value );
}
}
function myfunction( $arr=array() )
{
$fnFormat = function($str) {
return ucwords($str);
};
foreach($arr as $obj)
{
$str = $obj->getFormatted($fnFormat);
echo "$str\n";
}
}
$arr = array(
new FormattedLineItem('format me!'),
new LineItem('do not format me!')
);
myfunction($arr);
The idea is to use separate classes to distinguish between strings.

Related

PHP - Find array by name given as string

I've got some arrays like this
$something = array('foo' => 'bar');
Now how can I get the content of $something? I want to use this method to retrieve values from arrays but can't work out how to find an array with only it's name given as a string.
getArrayData($array,$key){
// $array == 'something';
// $key == 'foo';
// this should return 'bar'
}
EDIT:
I abstracted this too much maybe, so here is the full code:
class Config {
public static $site = array(
'ssl' => 'true',
'charset' => 'utf-8',
// ...
);
public static $menu = array(
'home' => '/home',
'/hub' => '/hub',
// ...
);
public static function get($from, $key){
return self::$from[$key];
}
public static function __callStatic($method, $key){
return self::get($method,$key);
}
}
In the end the configuration should be accessible from within the whole app by using Config::site('charset') to return 'utf-8'
You can use Variable-Variables
<?php
$something = array('foo' => 'bar');
$key="foo";
$arrayName="something";
echo getArrayData($$arrayName,$key); // Notice the use of $$
function getArrayData($array,$key){
return isset($array[$key])? $array[$key] : NULL ;
}
Fiddle
$array == 'something' doesn't mean much, you can easily check the array keys and return the value if the key exists:
function getArrayData($array,$key){
if(isset($array[$key])) return $array[$key];
else return "";
}
You should pass the array itself as parameter instead of the name. Then you can just return the value by the given key:
function getArrayData($array,$key){
return $array[$key];
}

PHP Object, set multiple properties

Is it possible to set multiple properties at a time for an object in php?
Instead of doing:
$object->prop1 = $something;
$object->prop2 = $otherthing;
$object->prop3 = $morethings;
do something like:
$object = (object) array(
'prop1' => $something,
'prop2' => $otherthing,
'prop3' => $morethings
);
but without overwriting the object.
Not like the way you want. but this can be done by using a loop.
$map = array(
'prop1' => $something,
'prop2' => $otherthing,
'prop3' => $morethings
);
foreach($map as $k => $v)
$object->$k = $v;
See only 2 extra lines.
You should look at Object Oriented PHP Best Practices :
"since the setter functions return $this you can chain them like so:"
$object->setName('Bob')
->setHairColor('green')
->setAddress('someplace');
This incidentally is known as a fluent interface.
I would recommend you don't do it. Seriously, don't.
Your code is much MUCH cleaner the first way, it's clearer of your intentions, and you aren't obfocusing your code to the extent where sometime in the future someone would look at your code and think "What the hell was the idiot thinking"?
If you insist on doing something which is clearly the wrong way to go, you can always create an array, iterate it and set all the properties in a loop. I won't give you code though. It's evil.
You could write some setters for the object that return the object:
public function setSomething($something)
{
$this->something = $something;
return $this; //this will return the current object
}
You could then do:
$object->setSomething("something")
->setSomethingelse("somethingelse")
->setMoreThings("some more things");
You would need to write a setter for each property as a __set function is not capable of returning a value.
Alternatively, set a single function to accept an array of property => values and set everything?
public function setProperties($array)
{
foreach($array as $property => $value)
{
$this->{$property} = $value;
}
return $this;
}
and pass in the array:
$object->setProperties(array('something' => 'someText', 'somethingElse' => 'more text', 'moreThings'=>'a lot more text'));
I realise this is an old question but for the benefit of others that come across it, I solved this myself recently and wanted to share the result
<?php
//Just some setup
header('Content-Type: text/plain');
$account = (object) array(
'email' => 'foo',
'dob'=>((object)array(
'day'=>1,
'month'=>1,
'year'=>((object)array('century'=>1900,'decade'=>0))
))
);
var_dump($account);
echo "\n\n==============\n\n";
//The functions
function &getObjRef(&$obj,$prop) {
return $obj->{$prop};
}
function updateObjFromArray(&$obj,$array){
foreach ($array as $key=>$value) {
if(!is_array($value))
$obj->{$key} = $value;
else{
$ref = getObjRef($obj,$key);
updateObjFromArray($ref,$value);
}
}
}
//Test
updateObjFromArray($account,array(
'id' => '123',
'email' => 'user#domain.com',
'dob'=>array(
'day'=>19,
'month'=>11,
'year'=>array('century'=>1900,'decade'=>80)
)
));
var_dump($account);
Obviously there are no safeguards built in. The main caveat is that the updateObjFromArray function assumes that for any nested arrays within $array, the corresponding key in $obj already exists and is an object, this must be true or treating it like an object will throw an error.
Hope this helps! :)
I wouldn't actually do this....but for fun I would
$object = (object) ($props + (array) $object);
you end up with an stdClass composed of $objects public properties, so it loses its type.
Method objectThis() to transtypage class array properties or array to stdClass. Using direct transtypage (object) would remove numeric index, but using this method it will keep the numeric index.
public function objectThis($array = null) {
if (!$array) {
foreach ($this as $property_name => $property_values) {
if (is_array($property_values) && !empty($property_values)) {
$this->{$property_name} = $this->objectThis($property_values);
} else if (is_array($property_values) && empty($property_values)) {
$this->{$property_name} = new stdClass();
}
}
} else {
$object = new stdClass();
foreach ($array as $index => $values) {
if (is_array($values) && empty($values)) {
$object->{$index} = new stdClass();
} else if (is_array($values)) {
$object->{$index} = $this->objectThis($values);
} else if (is_object($values)) {
$object->{$index} = $this->objectThis($values);
} else {
$object->{$index} = $values;
}
}
return $object;
}
}

PHP recursion stuck

I am trying to recurse through a multidimensional Object/Array structure to create JSON, but the following isn't working. $data is reset, but I'm not sure how to prevent this.
public function encodeJSON($data) {
foreach ($data as $key => $value) {
if (is_array($value) || is_object($value)) {
$json->$key = $this->encodeJSON($value);
} else {
$json->$key = $value;
}
}
return json_encode($json);
}
If you're trying to learn recursion that's one thing, but at least for me json_encode automatically encodes objects and arrays recursively so it's not really necessary to write the extra function.
Tested with this code:
class TestClass {
var $c1;
var $c2;
function __construct() {
$this->c1 = 'member variable 1';
$this->c2 = 8080;
}
}
$test = array('hello' => 'world', 'age' => 30,
'arr' => array('a' => 'b', 'c' => 'd'), 'obj' => new TestClass());
echo(json_encode($test));
// I get the following JSON object:
// {"hello":"world","age":30,"arr":{"a":"b","c":"d"},"obj":{"c1":"member variable 1","c2":8080}}

What is the best way of reading parameters in functions?

I've created several helper functions which I use when creating templates for Wordpress.
An example is this:
function the_related_image_scaled($w="150", $h="150", $cropratio="1:1", $alt="", $key="related" )
The problem is, if I only want to pass along the $alt parameter, I also have to populate $w, $h and $cropratio.
In one of my plugins, I use the following code:
function shortcode_display_event($attr) {
extract(shortcode_atts(array(
'type' => 'simple',
'parent_id' => '',
'category' => 'Default',
'count' => '10',
'link' => ''
), $attr));
$ec->displayCalendarList($data);
}
This allows me to call the function only using e.g.count=30.
How can I achieve the same thing in my own functions?
SOLUTION
Thanks to my name brother (steven_desu), I have come up with a solution that works.
I added a extra function (which I found on the net) to create value - pair from a string.
The code looks as follows:
// This turns the string 'intro=mini, read_more=false' into a value - pair array
function pairstr2Arr ($str, $separator='=', $delim=',') {
$elems = explode($delim, $str);
foreach( $elems as $elem => $val ) {
$val = trim($val);
$nameVal[] = explode($separator, $val);
$arr[trim(strtolower($nameVal[$elem][0]))] = trim($nameVal[$elem][1]);
}
return $arr;
}
function some_name($attr) {
$attr = pairstr2Arr($attr);
$result = array_merge(array(
'intro' => 'main',
'num_words' => '20',
'read_more' => 'true',
'link_text' => __('Read more')
), $attr);
extract($result);
// $intro will no longer contain'main' but will contain 'mini'
echo $intro;
}
some_name('intro=mini, read_more=false')
Info
With good feedback from Pekka, I googled and found some info regarding the Named Arguments and why it's not in PHP: http://www.seoegghead.com/software/php-parameter-skipping-and-named-parameters.seo
I would suggest using array_merge() and extract() at the beginning of your function, then passing parameters as arrays if this is a possibility.
function whatever($new_values){
$result = array_merge(array(
"var1" => "value1",
"var2" => "value2",
"var3" => "value3"
), $new_values);
extract($result);
echo "$var1, $var2, $var3";
}
whatever(array("var2"=>"new_value"));
The above will output:
value1, new_value, value3
it's a bit sloppy and uses more memory since it has to allocate the arrays, so it's the less efficient solution. But it does allow you to avoid redundancy. I'm sure a better method exists using magic meta-code, but I can't think of it off-hand.
Say this is your function:
function related_image_scaled($w="150", $h="150", $alt="", $key="related")
You can do this:
class ImageScaleParams {
public $w = 150;
public $h = 150;
public $cropratio = "1:1";
public $alt = "";
public $key = "related";
}
function related_image_scaled(ImageScaleParams $params) { ... }
Then call it like this:
$imgscale = new ImageScaleParams();
$imgscale.alt="New Alt";
related_image_scaled($imgscale);
You can also have various factories in ImageScaleParams such as:
class ImageScaleParams {
static function altFactory($alt) {
$imgscale = new ImageScaleParams();
$imgscale->alt = $alt;
return $imgscale;
}
}
Which you could call like this (equivalent to previous example):
related_image_scaled(ImageScaleParams::altFactory("New Alt"));
New Answer:
Could you not write the function using the extract function's default EXTR_OVERWRITE option?
function the_related_image_scaled($params) {
$w="150"; $h="150"; $cropratio="1:1"; $alt=""; $key="related";
extract($params);
//Do Stuff
}
Called with:
the_Related_image_scaled(array("alt"=>"Alt Text"));
You have the option of defaulting the parameters to null and only using them if they are not null:
function the_related_image_scaled($w=null, $h=null, $cropratio=null, $alt=null, $key = null) {
$output = //Get the base of the image tag
//including src leave a trailing space and don't close
if($w!==null) {
$output .= "width=\"$w\"";
}
//... Through all your parameters
return $output;
}
So only passing the alt parameter would look like:
echo the_related_image_scaled(null,null,null,$alt);

Declarative access to structured PHP variable without foreach loops

Background
Assume I have the following nested variable in PHP.
$data = Array(
Array('lname' => 'Simpson','fname' => 'Homer','age' => '35','motto' => '_blank_'),
Array('lname' => 'Simpson','fname' => 'Marge','age' => '34','motto' => '_blank_'),
Array('lname' => 'Flintstone','fname' => 'Fred','age' => '33','motto' => '_blank_'),
Array('lname' => 'Flintstone','fname' => 'Wilma','age' => '29','motto' => '_blank_')
);
Assume also the standard methods for accessing specific values:
print($data[0]['fname']); // Homer
print($data[1]['age']); // 34
Question
Is there an existing library or framework that would allow me to easily
acess specific values declaratively, without using foreach loops?
$test = $data->get_record_by_fname['Homer']
print $test['age'] //35
If you really wanted to overkill everything, you could try an approach using magical methods!
class Simpsons
{
protected $_data = array();
public function __construct(array $data)
{
$this->_data = array_map(function ($i) { return (object)$i; }, $data);
}
public function __call($method, $args)
{
if (count($args) == 0)
return NULL;
foreach ($this->_data as $row)
{
if (property_exists($row, $method) && $row->$method == $args[0])
{
return $row;
}
}
return NULL;
}
}
Usage:
$p = new Simpsons($data); // Stored in the format provided
var_dump($p->fname('Homer')); // Gets the record with fname = Homer
Is there a particular reason you don't want to use foreach loops? If it's merely for conciseness, you could just declare the function yourself, it's fairly trivial:
function get_record($set, $field, $value) {
foreach($set as $key => $val) {
if($val[$field] === $value) return $set[$key];
}
return NULL;
}
Then your example would become:
$test = get_record($data, 'fname', 'Homer');
print $test['age']; //35
class SomeClass{
// Stores the Array of Data
public $data;
// Sets up the object. Only accepts arrays
public function __construct(array $data)
{
$this->data = $data;
}
// Gets a record based on the key/value pair
public function getByKey($key, $value)
{
foreach($this->data as $array)
{
if(is_array($array)
{
if(array_key_exists($key, $array) && $array[$key] == $value)
{
return $array;
}
}
}
}
}
$array = array( 1 => array("Test" => "Hello"));
$obj = new SomeClass($array);
$record = $obj->getByKey('Test', 'Hello');
This lets you get a record based on what a key/value pair inside the array is. Note, the type hinting in the constructor is PHP 5.3(?)
BTW, No, there is no way to escape the foreach as even internal PHP functions (anything beginning with array_) uses a foreach or some other type of loop. However, if you encapsulate the loop into a class, you don't have to think about it.

Categories