Snippet:
$temp = array();
foreach ($data as $key => $value) {
// This line varies, from HTML to URLs, to anything in between
array_push($temp, "<b>$key:</b> $value");
}
$request = implode('<br/>', $temp); // Glue also varies depending on needs
unset($temp);
This is a getaway from the usual $string .= 'blah<br/>'. Never mind the rtrim.
Can anyone show me a better way to achieve above without use of a temp array and possibly without a loop? Bonus points for not using above code in a function, lambda acceptable though.
P.S. While writing this question I have found a wonderful http_build_query(). One down, one to go.
Edit:
What result should look like:
<b>Title: </b> Value</br>
<b>Title2: </b> Value2</br>
<b>Title3: </b> Value3
Or with different settings (not required, but shows possibility):
key=value&key2=value2&key3=value3
I am trying to improve my code, I use the above snippet everywhere.
My answer: There are some ways, but:
Loops & arrays are the best friends of any programmer.
They provide pretty good readability, reusability and generally are considered to be a right way for performing pretty much the same actions.
You may also take a look on array_map or array_walk. Less code, but it is a loop anyways.
array_walk($data, function($value, $key){
$temp[] = "<b>{$key}:</b> {$value}" ; //Shorthand for array_push is $arr[] = $value ;
}) ;
I suppose this is one way; yay for "one-liners":
$data = ...;
echo join("\n", array_map(function($key) use ($data) {
return sprintf('<b>%s:</b> %s',
htmlspecialchars($key, ENT_QUOTES, 'UTF-8'),
htmlspecialchars($data[$key], ENT_QUOTES, 'UTF-8')
);
}, array_keys($data)));
You basically map the keys using a closure that binds the original array as well so that you have access to both the key and value in each invocation.
I think you will prefere your solution:
$data = range('a', 'e'); // some data
$request = ''; // empty string
array_map(function($value, $key)use(&$request, $data){
static $i = 1;
if($i == count($data)){
$request .= "<b>$key:</b> $value";
}else{
$request .= "<b>$key:</b> $value<br/>";
}
$i++;
}, $data, array_keys($data));
echo $request;
Online demo
A solution with a loop but without $temp array:
$data = range('a', 'e'); // some data
$request = ""; // empty string
foreach ($data as $key => $value) {
$request .= "<b>$key:</b> $value<br/>"; // add to string
}
$request = substr($request,0,-5); // remove last <br/>
echo $request; //output
Related
I'm trying to all URLs that contain http://api.example.com/data/ to https://example.com/data/
in an array from a Database while using the Fat-Free Framework, the code iv tried below gives me
Internal Server Error, Array to string conversion
$f3->route('GET /details/#itemid',
function($f3) {
$arr = array('result' => $f3->get('DBh')->exec('SELECT * FROM DB_table WHERE id=?',$f3->get('PARAMS.itemid')));
str_replace('http://api.example.com/data/', 'https://example.com/data/', $arr);
$f3->mset($arr);
echo \Template::instance()->render('views/details.html');
}
);
Well you've got a couple simple options to help with this. I'm also going to assume you mean to reference $arr['result'] and not just $arr and I'd need to assume you're only looking for one column out of the db column. I'll call this column url.
Option 1
foreach($arr['result'] as $key => $value) {
if(empty($value['url'])) {
continue;
}
$arr['result'][$key]['url'] = str_replace('http://api.example.com/data/', 'https://example.com/data/', $value['url']);
}
Option 2
foreach($arr['result'] as &$value) {
if(empty($value['url'])) {
continue;
}
$value['url'] = str_replace('http://api.example.com/data/', 'https://example.com/data/', $value['url']);
}
Option 3
$arr['result'] = array_map(function($value) {
$value['url'] = str_replace('http://api.example.com/data/', 'https://example.com/data/', $value['url']);
return $value;
}, $arr['result']);
My personal preference for readability and simplicity is Option 2. I believe foreach statements have been optimized to actually run faster than array_map which is......interesting.
I am running this against an associative (non-numerical keys) array that is 80K lines, decoded from a 2.1K json file:
$ret = array();
function recursive(array $array, $tableName, $level=''){
global $ret;
$tableData = array();
foreach($array as $key => $value){
if(!is_array($key)) $k = $key; // keep array keys for tablename
if(is_array($value)){
$key = is_int($key) ? $tableName : $key; //skip empty subarrays
recursive($value, $level=$key);
} else{
$tableData=[$tableName, $k, $value];
}
if (!empty($tableData))
{array_push($ret, $tableData);
}
}
return $ret;
}
$ret =(recursive($array, 'project'));
The json data contains empty sub-arrays which are discarded during the 'is_int($key) recursive loop. This is a typical file size that will be processed and the function is taking over 2 mins to run - which may be as good as it gets - but I wanted to ask if there were any ways to speed this function up by better coding.
thank you,
It seems to be always true: if(!is_array($key)) $k = $key;
You are don't use the $level variable. Why do you need it?
Globals are really bad idea. You probably need to send variables to your function by link instead of value:
function recursive(&$data, &$result, $tableName)
{
//some of your logic here
//...
//if you need a recursion call
recursive($data, $result, $tableName);
}
//function call
$result = [];
recoursive($data, $result, 'project');
//all of your data now in the result variable
Also it may be a good idea to read and process your data by chunks. Iterators or generators can help you with this.
i have the following code:
while (list($key, $value) = each($arr)) {
$implode_keys_values = #"$key=$value,";
echo"$implode_keys_values";
}
which ends up echoing out xxxx=xxxx,xxxx=xxxx,xxxx=xxxx,xxxx=xxxx,
depending on how many keys/values there are.
how do I take this dynamically changing string and remove the very last comma on it?
keep in mind:
$implode_keys_values = substr($implode_keys_values,0,-1);
will not work, it will take out all the commas.
rtrim($implode_keys_values, ",") would cut trailing commas.
You can learn more about rtrim here at the PHP docs
$implode_keys_values = "";
while (list($key, $value) = each($arr)) {
$implode_keys_values .= #"$key=$value,";
}
echo rtrim($implode_keys_values, ",");
PHP has a function built in for that if the data in the array is not too complex (works for the xxxxxx values you have, can break with others):
echo http_build_query($arr, '', ',');
See http_build_query().
Another alternative is using iterators and checking if the current iteration is the last one. The CachingIterator::hasNext() method is helpful for that:
$it = new CachingIterator(new ArrayIterator($arr));
foreach($it as $key => $value) {
echo $key, '=', $value, $it->hasNext() ? ',' : '';
}
This variant does work with any data.
In your case, this would be a better way to implement implode():
$data = array();
while (list($key, $value) = each($arr)) {
$data[] = "$key=$value";
}
echo implode(",",$data);
This is probably a simple question, but how do you iterate through an array, doing something to each one, until the last one and do something different?
I have an array of names. I want to output the list of names separated by commas.
Joe, Bob, Foobar
I don't want a comma at the end of the last name in the array, nor if there is only one value in the array (or none!).
Update: I can't use implode() because I have an array of User model objects where I get the name from each object.
$users = array();
$users[] = new User();
foreach ($users as $user) {
echo $user->name;
echo ', ';
}
How can I achieve this and still use these objects?
Update: I was worrying too much about how many lines of code I was putting in my view script, so I decided to create a view helper instead. Here's what I ended up with:
$array = array();
foreach($users as $user) {
$array[] = $user->name;
}
$names = implode(', ', $array);
Use implode:
$names = array('Joe', 'Bob', 'Foobar');
echo implode(', ', $names); # prints: Joe, Bob, Foobar
To clarify, if there is only one object in the array, the ', ' separator will not be used at all, and a string containing the single item would be returned.
EDIT: If you have an array of objects, and you wanted to do it in a way other than a for loop with tests, you could do this:
function get_name($u){ return $u->name; };
echo implode(', ', array_map('get_name', $users) ); # prints: Joe, Bob, Foobar
$array = array('joe', 'bob', 'Foobar');
$comma_separated = join(",", $array);
output: joe,bob,Foobar
Sometimes you might not want to use implode.
The trick then is to use an auxiliary variable to monitor not the last, but the first time through the loop.
vis:
$names = array('Joe', 'Bob', 'Foobar');
$first = true;
$result = '';
foreach ($names as $name)
{
if (!$first)
$result .= ', ';
else
$first = false;
$result .= $name;
}
implode(', ', $array_of_names)
psuedocode....
integer sigh=container.getsize();
sigh--;
integer gosh=0;
foreach element in container
{
if(gosh!=sigh)
dosomething();
else
doLastElementStuff();
gosh++;
}
looking at all the other answers, it seems PHP has gotten a lot more syntactic S since I last wrote anything in it :D
I come accross this a lot building SQL statements etc.
$joiner = " ";
foreach ($things as $thing) {
echo " $joiner $thing \n";
$joiner = ',';
}
FOr some reason its easier to work out the logic if you think of the ",", "AND" or "OR" as an option/attribute that goes before an item. The problem then becomes how to suppress the the "," on the first line.
I personally found the fastest way (if you're into micro optimization) is:
if(isset($names[1])) {
foreach ($names as $name) {
$result .= $name . ', ';
}
$result = substr($result, 0, -2);
} else {
$result = $names[0];
}
isset($names[1]) is the fastest (albeit not so clear) way of checking the length of an array (or string). In this case, checking for at least two elements is performed.
I actually find it easier to create my comma delimited text a little differently. It's a bit more wordy, but it's less function calls.
<?php
$nameText = '';
for ($i = 0; $i < count($nameArray); $i++) {
if ($i === 0) {
$nameText = $nameArray[$i];
} else {
$nameText .= ',' . $nameArray[$i];
}
}
It adds the comma as a prefix to every name except where it's the first element if the array. I have grown fond of using for as opposed to foreach since I have easy access to the current index and therefore adjacent elements of an array. You could use foreach like so:
<?php
$nameText = '';
$nameCounter = 0;
foreach ($nameArray as $thisName) {
if ($nameCounter === 0) {
$nameText = $thisName;
$nameCounter++;
} else {
$nameText .= ',' . $thisName;
}
}
I am trying to create a multi-dimensional array whose parts are determined by a string. I'm using . as the delimiter, and each part (except for the last) should be an array
ex:
config.debug.router.strictMode = true
I want the same results as if I were to type:
$arr = array('config' => array('debug' => array('router' => array('strictMode' => true))));
This problem's really got me going in circles, any help is appreciated. Thanks!
Let’s assume we already have the key and value in $key and $val, then you could do this:
$key = 'config.debug.router.strictMode';
$val = true;
$path = explode('.', $key);
Builing the array from left to right:
$arr = array();
$tmp = &$arr;
foreach ($path as $segment) {
$tmp[$segment] = array();
$tmp = &$tmp[$segment];
}
$tmp = $val;
And from right to left:
$arr = array();
$tmp = $val;
while ($segment = array_pop($path)) {
$tmp = array($segment => $tmp);
}
$arr = $tmp;
I say split everything up, start with the value, and work backwards from there, each time through, wrapping what you have inside another array. Like so:
$s = 'config.debug.router.strictMode = true';
list($parts, $value) = explode(' = ', $s);
$parts = explode('.', $parts);
while($parts) {
$value = array(array_pop($parts) => $value);
}
print_r($parts);
Definitely rewrite it so it has error checking.
Gumbo's answer looks good.
However, it looks like you want to parse a typical .ini file.
Consider using library code instead of rolling your own.
For instance, Zend_Config handles this kind of thing nicely.
I really like JasonWolf answer to this.
As to the possible errors: yes, but he supplied a great idea, now it is up to the reader to make it bullet proof.
My need was a bit more basic: from a delimited list, create a MD array. I slightly modified his code to give me just that. This version will give you an array with or without a define string or even a string without the delimiter.
I hope someone can make this even better.
$parts = "config.debug.router.strictMode";
$parts = explode(".", $parts);
$value = null;
while($parts) {
$value = array(array_pop($parts) => $value);
}
print_r($value);
// The attribute to the right of the equals sign
$rightOfEquals = true;
$leftOfEquals = "config.debug.router.strictMode";
// Array of identifiers
$identifiers = explode(".", $leftOfEquals);
// How many 'identifiers' we have
$numIdentifiers = count($identifiers);
// Iterate through each identifier backwards
// We do this backwards because we want the "innermost" array element
// to be defined first.
for ($i = ($numIdentifiers - 1); $i >=0; $i--)
{
// If we are looking at the "last" identifier, then we know what its
// value is. It is the thing directly to the right of the equals sign.
if ($i == ($numIdentifiers - 1))
{
$a = array($identifiers[$i] => $rightOfEquals);
}
// Otherwise, we recursively append our new attribute to the beginning of the array.
else
{
$a = array($identifiers[$i] => $a);
}
}
print_r($a);