Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    257
    Thanks
    26
    Thanked 10 Times in 10 Posts

    Post Need help Creating an array of arrays using array_map!?!

    I'm using the code below to reach a certain output with an array_map, I've been playing with the code for hours and I'm still having a hard time finding the right results.
    I understand php and array's but i'm new to array_maps and creating them on the fly...

    Can Anyone offer any insight??? (At the bottom is my desired results from the array_map)

    <?php
    // DATABASE CONNECTION
    require("includes/sample.php");

    // DB table to use
    $table = "gl_detail";

    // Table's primary key
    $primaryKey = "RecordNo" ;

    $no = 0 ;

    // SERVERSIDE COLUMN DATA OUTPUT
    $stmtX = $pdo->query ("SELECT * FROM `reports_fields` WHERE RecordNo ='181104091557' AND NOT FieldName = 'MANAGE' ORDER BY sort ");
    while($list=$stmtX->fetch(PDO::FETCH_ASSOC)){
    $row_count = $stmtX ->rowCount();
    $fields = $list[FieldName] ;

    // Top Array in image below *
    $str1 .= "[$no]=>" . ", " ; // count rows
    $str2 .= "Array([db] => $fields [dt] => $no)" . ", " ; // list column fields

    $count = rtrim($str1,", "); // remove last comma in string
    $data = rtrim($str2,", "); // remove last comma in string

    //print_r($count); // test count
    //echo "<br>";
    //print_r($data); // test data



    $no++;
    }

    echo $data ;
    echo "<br><br>";

    ////////////////////////////////////////////////////////////
    Need Help with this Section...

    // Bottom Array in image *
    $x = array(0, 1, 2, 3, 4, 5 , 6);
    $y = array("[db] => RecordNo", "[db] => BusAccName", "[db] => Transaction" , "[db] => JobName" , "[db] => RefDate" , "[db] => Debit" , "[db] => Credit" );

    $columns = array_map(null,$x, $y);

    print_r($columns);
    ?>

    -array-map-sample-jpg


    //////////////////////////////////////////////////////////////////

    Desired output for the section I need help with

    Array ( [0] =>
    Array ( [db] => RecordNo [dt] => 0 ), [1] =>,
    Array ( [db] => BusAccName [dt] => 1 ), [2] =>,
    Array ( [db] => Transaction [dt] => 2 ), [3] =>,
    Array ( [db] => JobName [dt] => 3 ), [4] =>,
    Array ( [db] => RefDate [dt] => 4 ), [5] =>,
    Array ( [db] => Debit [dt] => 5 ), [6] =>,
    Array ( [db] => Credit [dt] => 6 ) )

  2. #2
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,557
    Thanks
    10
    Thanked 156 Times in 151 Posts
    This smells like an XY Problem. Tell us what the real problem is instead of asking about your attempted solution to the real problem. What is the overall problem/task at hand?
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    Make A Donation https://www.paypal.me/KevinRubio

  3. Users who have thanked benanamen for this post:

    DataTalk (Nov 8th, 2018)

  4. #3
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    257
    Thanks
    26
    Thanked 10 Times in 10 Posts
    The overall problem is Script does not to produce the desired output..

    Im trying to create this output:

    Results I'm Look for
    Array ( [0] =>
    Array ( [db] => RecordNo [dt] => 0 ), [1] =>,
    Array ( [db] => BusAccName [dt] => 1 ), [2] =>,
    Array ( [db] => Transaction [dt] => 2 ), [3] =>,
    Array ( [db] => JobName [dt] => 3 ), [4] =>,
    Array ( [db] => RefDate [dt] => 4 ), [5] =>,
    Array ( [db] => Debit [dt] => 5 ), [6] =>,
    Array ( [db] => Credit [dt] => 6 ) )

    the script above is what i used to try to create this structure
    ....
    Are you aware of a better way to create an array structure???

    Currently my script is outputting:
    Array ( [0] =>
    Array ( [0] => 0 [1] => [db] => RecordNo ) [1] =>
    Array ( [0] => 1 [1] => [db] => BusAccName ) [2] =>
    Array ( [0] => 2 [1] => [db] => Transaction ) [3] =>
    Array ( [0] => 3 [1] => [db] => JobName ) [4] =>
    Array ( [0] => 4 [1] => [db] => RefDate ) [5] =>
    Array ( [0] => 5 [1] => [db] => Debit ) [6] =>
    Array ( [0] => 6 [1] => [db] => Credit ) )

  5. #4
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,557
    Thanks
    10
    Thanked 156 Times in 151 Posts
    You don't seem to understand what I said. I don't care about your code. I want to know what the problem is that is supposed to be solved with code.
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    Make A Donation https://www.paypal.me/KevinRubio

  6. #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,223
    Thanks
    3
    Thanked 550 Times in 535 Posts
    Making a string that LOOKS like the php syntax for an array doesn't produce the result that you think it is (use var_dump() instead of print_r() to see what the array elements actually contain), so, the code you have shown doesn't have anything to do with the overall goal you may be trying to reach and therefore doesn't tell us anything useful. Even the stated desired output is syntactical nonsense.

    This is the actual $columns result your code is producing -

    Code:
    array(7) {
      [0]=>
      array(2) {
        [0]=>
        int(0)
        [1]=>
        string(16) "[db] => RecordNo"
      }
      [1]=>
      array(2) {
        [0]=>
        int(1)
        [1]=>
        string(18) "[db] => BusAccName"
      }
      [2]=>
      array(2) {
        [0]=>
        int(2)
        [1]=>
        string(19) "[db] => Transaction"
      }
      [3]=>
      array(2) {
        [0]=>
        int(3)
        [1]=>
        string(15) "[db] => JobName"
      }
      [4]=>
      array(2) {
        [0]=>
        int(4)
        [1]=>
        string(15) "[db] => RefDate"
      }
      [5]=>
      array(2) {
        [0]=>
        int(5)
        [1]=>
        string(13) "[db] => Debit"
      }
      [6]=>
      array(2) {
        [0]=>
        int(6)
        [1]=>
        string(14) "[db] => Credit"
      }
    }
    The information we would need to help you would be a sample of your data and what end result you want from that data (and not your interpretation of what the result should LOOK like, we need to know for what and how you are actually going to use the result.)
    Last edited by CFMaBiSmAd; Nov 11th, 2018 at 01:21 AM.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •