skip to Main Content

I created a multidimensional array to access each of the arrays I stored from a MySQL query, using this statement:

$new_array[$n][$row['MLocID']] = $row;

Below is the output from print_r(get_defined_vars(), true):

[new_array] => Array
    (
    [0] => Array
         (
         [226] => Array
                 (
                  [MLocID] => 226
                  [MemberID] => 2
                  [SupBusName] => Albertsons

I did this to store the results in arrays, so I can access them later on the same PHP page. I thought it would work better the way I did it, but now I think I made it more confusing to access. That is the first issue. I can’t figure out how to get the data? I thought it was like this:

$new_array[0][$row[0]];

But it does not work. I tried different variations, but none worked for me. The second issue is, I don’t know if I did more harm by putting my results in arrays. Is it more logical to do it the way I did, or is it better to do separate queries by using the 'MLocID' number to access the data, for the 5 results I will have on each page?

2

Answers


  1. Chosen as BEST ANSWER

    I figured out. There was never a good reason to do this.

    $row = $resul->fetch_assoc(); $new_array[$n][$row['MLocID']] = $row;

    Instead I did this:

    `` $row = $resul->fetch_array()) $new_array[$n] = $row;

    `` And now I get this:

    [new_array] => Array ( [0] => Array ( [0] => 221 [MLocID] => 221 [1] => 2 [MemberID] => 2 [2] => Walmart 1 And I can use both of these:

    echo $new_array[0][0]; echo $new_array[0]['MLocID'];


  2. In your case you can access the array like this

    foreach ($new_array as $key => $value) {
    
        foreach ($value as $_key => $_value) {
          
            $MLocID = $_value['MLocID'];
            $MemberID = $_value['MemberID'];
            $SupBusName = $_value['SupBusName'];
    
        }
    
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search