php foreach with multidimensional array

后端 未结 12 1548
一向
一向 2020-11-27 05:10

I\'m developing a php app that uses a database class to query mySQL.

the class is here: http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql/

相关标签:
12条回答
  • 2020-11-27 05:15

    With arrays in php, the foreach loop is always a pretty solution.
    In this case it could be for example:

    foreach($my_array as $number => $number_array)
        {
        foreach($number_array as $data = > $user_data)
            {
                print "Array number: $number, contains $data with $user_data.  <br>";
            }
        }
    
    0 讨论(0)
  • 2020-11-27 05:17

    If you need to do string manipulation on array elements, e.g, then using callback function array_walk_recursive (or even array_walk) works well. Both come in handy when dynamically writing SQL statements.

    In this usage, I have this array with each element needing an appended comma and newline.

    $some_array = [];
    

    data in $some_array
    0: "Some string in an array"
    1: "Another string in an array"

    Per php.net

    If callback needs to be working with the actual values of the array, specify the first parameter of callback as a reference. Then, any changes made to those elements will be made in the original array itself.

    array_walk_recursive($some_array, function (&$value, $key) {
        $value .= ",\n";
    });
    

    Result:
    "Some string in an array,\n"
    "Another string in an array,\n"

    Here's the same concept using array_walk to prepend the database table name to the field.

    $fields = [];
    

    data in $fields:
    0: "FirstName"
    1: "LastName"

    $tbl = "Employees"
    
    array_walk($fields, 'prefixOnArray', $tbl.".");
    
    function prefixOnArray(&$value, $key, $prefix) { 
        $value = $prefix.$value; 
    }
    

    Result:
    "Employees.FirstName"
    "Employees.LastName"


    I would be curious to know if performance is at issue over foreach, but for an array with a handful of elements, IMHO, it's hardly worth considering.

    0 讨论(0)
  • 2020-11-27 05:24

    This would have been a comment under Brad's answer, but I don't have a high enough reputation.

    Recently I found that I needed the key of the multidimensional array too, i.e., it wasn't just an index for the array, in the foreach loop.

    In order to achieve that, you could use something very similar to the accepted answer, but instead split the key and value as follows

    foreach ($mda as $mdaKey => $mdaData) {
        echo $mdaKey . ": " . $mdaData["value"];
    }
    

    Hope that helps someone.

    0 讨论(0)
  • 2020-11-27 05:24

    Holla/Hello, I got it! You can easily get the file name,tmp_name,file_size etc.So I will show you how to get file name with a line of code.

    for ($i = 0 ; $i < count($files['name']); $i++) {
        echo $files['name'][$i].'<br/>';
    }
    

    It is tested on my PC.

    0 讨论(0)
  • 2020-11-27 05:25

    Ideally a multidimensional array is usually an array of arrays so i figured declare an empty array, then create key and value pairs from the db result in a separate array, finally push each array created on iteration into the outer array. you can return the outer array in case this is a separate function call. Hope that helps

    $response = array();    
    foreach ($res as $result) {
            $elements = array("firstname" => $result[0], "subject_name" => $result[1]);
            array_push($response, $elements);
        }
    
    0 讨论(0)
  • 2020-11-27 05:27

    Wouldn't a normal foreach basically yield the same result as a mysql_fetch_assoc in your case?

    when using foreach on that array, you would get an array containing those three keys: 'id','firstname' and 'lastname'.

    That should be the same as mysql_fetch_assoc would give (in a loop) for each row.

    0 讨论(0)
提交回复
热议问题