Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying a function all values in an array

Tags:

php

$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');
like image 732
stef Avatar asked Oct 12 '10 08:10

stef


People also ask

How do I apply a function to an array?

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.

What is applying a function to each element of a list?

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.

What is an arrayfun in MATLAB?

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.

What are the input and output types for arrayfun?

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.


2 Answers

this should work:

<?php
function encode_items(&$item, $key)
{
    $item = utf8_encode($item);
}

array_walk_recursive($jobs, 'encode_items');
?>
like image 127
ITroubs Avatar answered Nov 03 '22 19:11

ITroubs


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.

like image 34
Felix Kling Avatar answered Nov 03 '22 18:11

Felix Kling