jadeg Posted April 18, 2014 Share Posted April 18, 2014 I am trying to multiply every value in the array by 2. Below is my code. However i get the error Unsupported operand types in C <?php session_start(); ini_set('display_startup_errors',1); ini_set('display_errors',1); error_reporting(-1); $ftp_server="server"; $ftp_user_name="user"; $ftp_user_pass="pass"; $conn_id = ftp_connect($ftp_server) or die ("Couldn't connect to $ftp_server"); $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); if($login_result) { echo '<h1> Last Second Reading for Bridgetford T1 </h1>'; $handle=file('ftp://user:pass@server/public_html/test.csv'); $data =array(); $i=0; foreach ($handle as $row) { if($i==0){ $i++; } else { $data1= explode(",", $row); $count=count($data1); for($c=0; $c<$count; $c++) { if($c>0){ $data[$i][$c]=$data1[$c]; // populate 2 by 2 array } } $i++; } } function myarray (&$val) { $val=$val*2.0; } array_walk($data,'myarray'); var_dump($data); } else { echo "There is a problem\n"; } ftp_close($conn_id); ?> Quote Link to comment https://forums.phpfreaks.com/topic/287875-array_walk-help/ Share on other sites More sharing options...
denno020 Posted April 18, 2014 Share Posted April 18, 2014 I just tried this on my local machine: $data = array(4, 5, 6, 3, 4); function myarray (&$val) { $val=$val*2.0; } array_walk($data,'myarray'); var_dump($data); and it worked perfectly.. so, where exactly are you getting the error? And what is the full error? Can you also do a dump of your $data array before you perform the array_walk, and post that here, so we can see what it is before hand. Quote Link to comment https://forums.phpfreaks.com/topic/287875-array_walk-help/#findComment-1476631 Share on other sites More sharing options...
jadeg Posted April 19, 2014 Author Share Posted April 19, 2014 dump of array $data before I perform the array_walk works just file. After the array_walk though, I get an error fatal error: Unsupported operand types in C:www/wamp/.... Quote Link to comment https://forums.phpfreaks.com/topic/287875-array_walk-help/#findComment-1476709 Share on other sites More sharing options...
denno020 Posted April 19, 2014 Share Posted April 19, 2014 I have no doubt it works, but I asked if you could copy the dump and paste it here, so we can see the content of your array. Quote Link to comment https://forums.phpfreaks.com/topic/287875-array_walk-help/#findComment-1476710 Share on other sites More sharing options...
kicken Posted April 20, 2014 Share Posted April 20, 2014 Your array is multi-dimensional so in your function $val is another array (the second dimension), not a number. You can use a loop to go over the first dimension. foreach ($data as &$arr){ array_walk($arr, 'myarray'); } Quote Link to comment https://forums.phpfreaks.com/topic/287875-array_walk-help/#findComment-1476727 Share on other sites More sharing options...
jadeg Posted April 21, 2014 Author Share Posted April 21, 2014 Thank you Quote Link to comment https://forums.phpfreaks.com/topic/287875-array_walk-help/#findComment-1476891 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.