Jump to content
Sign in to follow this  
WesleyA

how to use list with a more complex type of array ?

Recommended Posts

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?

Share this post


Link to post
Share on other sites

$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.

Share this post


Link to post
Share on other sites

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 ?

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×