$jobs is an array retrieved from a DB query. print_r($jobs)
shows:
Array
(
[ID] => 131
[Title] => -bla-
[Baseline] => lorem ipsum ...
[Description] => <ul><li>list 1</li><li>list 2</li></ul>
[EventID] => 1008
)
Array
(
[ID] => 132
[Title] => -bla 2-
[Baseline] => lorem ipsum lorem ipsum...
[Description] => <ul><li>list 1</li><li>list 2</li></ul>
[EventID] => 1009
)
etc ...
Id like to run utf8_encode() on all values of these arrays. I'm not sure if I should use array_map, array_walk_recursive ? The output should not alter the names of the array keys so that I don't need to change anything in my template, so
<h1><?=$j['title']?></h1>
should still work, albeit utf8 encoded.
EDIT: I'm trying the following, no luck
function fix_chars($key, $value)
{
return utf8_encode($value);
}
array_walk_recursive($jobs, 'fix_chars');
B = arrayfun (func,A) applies the function func to the elements of A, one element at a time. arrayfun then concatenates the outputs from func into the output array B, so that for the i th element of A, B (i) = func (A (i)). The input argument func is a function handle to a function that takes one input argument and returns a scalar.
Let’s see what exactly is Applying a function to each element of a list means: Suppose we have a list of integers and a function that doubles each integer in this list. On applying the function to the list, the function should double all the integers in the list.
func can correspond to more than one function file and therefore can represent a set of overloaded functions. In these cases, MATLAB ® determines which function to call based on the class of the input arguments. Example: B = arrayfun (@round,A) returns the integer part of each element of A.
The input function to arrayfun must return a scalar or scalars that can be concatenated into an array. Code generation does not support cell array inputs to func. To predetermine the output type for func, the code generator can call func before processing the accumarray input arguments.
this should work:
<?php
function encode_items(&$item, $key)
{
$item = utf8_encode($item);
}
array_walk_recursive($jobs, 'encode_items');
?>
Here is an example with array_map()
:
function utf8_encode_array($array) {
return array_map('utf8_encode', $array);
}
$encoded_array = array_map('utf8_encode_array', $your_array);
I don't know if there is a performance difference between array_map
and array_walk_recursive
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With