WesleyA Posted January 29, 2016 Share Posted January 29, 2016 Down here the code to call a function it works with list. I dont understand how list works, In my example list gives a survey of 2 arrays that are executed inside the function here is how I call the function: $row = list ($variable, $number) = my_function(); var_dump($row); the output is like this: array (size=2) 0 => array (size=8) 0 => string 'Apple' (length=5) 1 => string 'Peach' (length=5) 2 => string 'Cherry' (length=6) 3 => string 'Grape' (length=5) 4 => string 'Blueberry' (length=9) 5 => string 'Lemon' (length=5) 6 => string 'Mango' (length=5) 7 => string 'Banana' (length=6) 1 => array (size=8) 0 => string '1' (length=1) 1 => string '2' (length=1) 2 => string '3' (length=1) 3 => string '4' (length=1) 4 => string '5' (length=1) 5 => string '6' (length=1) 6 => string '7' (length=1) 7 => string '8' (length=1) I need the values separated but how can I list the second array (the one with the numbers) as single numbers outside an own function? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 29, 2016 Share Posted January 29, 2016 $variable and $number are both arrays, so you could use list on each of those if you want to extract the values, or just access the values directly. I've never really had a need to use list, all it does is make one set of values available with other names. Link to comment Share on other sites More sharing options...
WesleyA Posted January 30, 2016 Author Share Posted January 30, 2016 Beside the example above; I have a script that accepts the values of a test array but this array has keys. For the part obtained code is retrieved from the database with mysql. This is done with a while loop. But this is a string. So I used explode to make an array. But explode makes different arrays, because all key values seem to have 0. How can I make an array with explode that has key values starting with 0 to value n ? Link to comment Share on other sites More sharing options...
WesleyA Posted January 30, 2016 Author Share Posted January 30, 2016 I tried to use array_slice. It works, but not for loops. is there a way to loop through the array offset number? this is a part of the function I wrote: $sql = "SELECT name FROM $var"; if (!$result = $conn->query($sql)){ die('There was an error running the query[' .$conn->error. ']'); } $result = $conn->query($sql); if ($result->num_rows > 0){ while($row = $result->fetch_assoc()){ $name = $row["name"]; $testrow[] = explode(' ' , $name); $slice[] = array_slice($testrow, 0 , 1); // 0 is the $offset but can be a number from 1 until n related to size of the table // var_dump($offset); // var_dump($slice); $name = implode(" ", $slice[0]); var_dump($name); Is array_slice the right method or is there another way to loop through an array? Link to comment Share on other sites More sharing options...
WesleyA Posted February 1, 2016 Author Share Posted February 1, 2016 solved Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now