1

Here's the array to store the value from the DB:

$dataTitle[0] = "TIR";
$dataTitle[1] = "OIL";
$dataTitle[2] = "SPK";

$dataDesc[0] = "Tires";
$dataDesc[1] = "Oil";
$dataDesc[2] = "Spark Plugs";

$dataValue[0] = "100";
$dataValue[1] = "10";
$dataValue[2] = "4";

I can use the following to insert the data into 2D array manually but it doesn't serve purpose if i have 100 or more row records to be inserted. That's why FOR loop is needed for the below.

$ResultView = array(array($dataTitle[0], $dataDesc[0], $dataValue[0]),
                array($dataTitle[1], $dataDesc[1], $dataValue[1]),
                    array($dataTitle[2], $dataDesc[2], $dataValue[2])
               );

If am using the following FOR LOOP, the 2D array only stored the last row record and omitted the 1st and 2nd row records.

for ($i=0; $i<=2; $i++) {
          $ResultView = array(array($dataTitle[$i], $dataDesc[$i], $dataValue[$i])
               );
    }

When I issue the output script below for the for loop above, I get the output value for the 3rd ROW and missing 1st and 2nd Row result. Please help!

for ($row=0; $row<=2; $row++) {

    for ($col=0; $col<=2; $col++) { 

        echo $ResultView[$row][$col]."&nbsp | ";
    }

    echo '<br />';
}

But, I am looking a way the use FOR Loop instead for the above. How?

The actual outputs are:

Row 1 => TIR | Tires | 100 
Row 2 => OIL | Oil | 10 
Row 3 => SPK | Spark Plugs | 4

Please advice.

8
  • 1
    Your data is not of any pattern. Do you have any data source e.g. database, etc...? Commented Mar 26, 2013 at 6:51
  • It's an example of DB data. The outputs are Row1 => TIR | Tires | 100 Row2 => OIL | Oil | 10 Row3 => SPK | Spark Plugs | 4 Commented Mar 26, 2013 at 6:53
  • try to use array_push() Commented Mar 26, 2013 at 6:55
  • Okay. Let says I have array that stored this DB data. $data[0] = "TIR" , $data[1] = "Tires", $data[2] = "100" Commented Mar 26, 2013 at 6:56
  • There must be data source in order to achieve the pattern you want Commented Mar 26, 2013 at 6:58

6 Answers 6

5

make it

 for ($i=0; $i<=2; $i++) {
      $ResultView[] = array(array($dataTitle[$i], $dataDesc[$i], $dataValue[$i])
           );
       }
Sign up to request clarification or add additional context in comments.

7 Comments

Hi @tumbernirmal, thanks for the answer. I copied exactly the same but when i try to make the output for it. It doesn't show me the correct result. EXAMPLE for output " echo $ResultView[0]; echo '<br />'; echo $ResultView[1];
Even i tried this "echo $ResultView[0][1]; echo '<br />'; echo $ResultView[1][1]; " It doesn't show me output value. Weird!!
what value you want in echo $ResultView[0];
I want the output for the row that contain data value.
Have just updated the question for the output command above. Please advice.
|
1

this is final soluation

  $dataTitle = array('TIR','OIL','SPK');
  $dataDesc=array('Tires','Oil','Spark Plugs'); 
 $dataValue=array('100','10','4');
 for ($i=0; $i<=2; $i++) { 
 $ResultView[] = array(array($dataTitle[$i], $dataDesc[$i], $dataValue[$i])); 
} 
     for ($row=0; $row<=2; $row++) 
     {   
         for ($col=0; $col<=2; $col++) 
         { 
              echo $ResultView[$row][0][$col]."  | "; 
          } 
   echo '<br />'; }

1 Comment

what in case size of the array are different?
0

If you DB result rows come back as arrays then you should be able to just loop through the results and push them into your products array.

// assuming $results contains your DB response
$products = array();
for ($i = 0; $i < count($results); $i++) {
     $products[] = $results[$i];  // or whatever method you use to fetch a row
}

Though your DB result can probably just be used like a multidimensional array.

2 Comments

Isn't this only for 1 dimensional array? Correct me if am wrong. I have updated my question for further clarify where I stored the data value from DB into a single array at first.
Hah you're right, too much JS recently :) Changed to count($results), but still their should be a disclaimer that you will need to count the number of actual results using the correct method. We don't really know where they are coming from or what format they are in.
0

Based on your array structure you could use this

$data[0] = "TIR";
$data[1] = "Tires";
$data[2] = "100";

$data1[0] = "OIL";
$data1[1] = "Oil";
$data1[2] = "10";

$data2[0] = "SPK";
$data2[1] = "Spark Plugs";
$data2[2] = "4";

$resultCount = 3;  // adjust this value for the number of results you have
$products = array();

for ($i = 0; $i < $resultCount; $i++) {
    $products[] = ${'data' . ($i ? $i : '')};
}

print_r($products);

Comments

0
**Method-1**  Raw method to generate the 2 diamentional array
This is your Array

$products = array( array( 'TIR', 'Tires', 100 ),
array( 'OIL', 'Oil', 10 ),
array( 'SPK', 'Spark Plugs', 4 ),
array( 'SPK', 'Spark Plugs', 4 ));

$cnt=0;
$cnt=count($products);
$product=array();

// am copying the data to $product array

for($i=0;$i<$cnt;$i++){
for($j=0;$j<3;$j++){
$product[$i][$j]= $products[$i][$j];
}
}


If you are getting data from data base 

for($i=0;$i<mysql_num_rows($result);$i++){
 $products[]=result [$i];
}

1 Comment

Hi shivaP, thanks for the clarification. But I want to create the 2D array using the data structure store in the array above.
0

If your data is from database : You can use while loop

$products        = array();
/* data from first table*/
while($dataTitle = mysql_fetch_assoc($sql)){
   $products[]   = $dataTitle;
} 

/* data from second table*/
while($dataDesc  = mysql_fetch_assoc($sql)){
   $products[]   = $dataDesc;
}

/* data from third table*/
while($dataValue = mysql_fetch_assoc($sql)){
   $products[]   = $dataValue;
}

echo "<pre>";
print_r($products);

4 Comments

Hi Prasanth, what if i have the data from DB stored in the array mentioned on my question. And I wanted to insert the data value array into the 2D array using for Loop? Is that possible
@user1109161 : I really don't understand how are you storing 3 diffrent values for $data[0] (TIR,OIL,SPK), without using a 2D array .
you said : "Here's the array to store the value from the DB:", why are you storing the values like the way shown, It will be better if you could show whole code (fetching from DB, assigning to vars, etc), so that we will get a better idea about your problem.
Because the data are acquired from different tables. Thus, the best way is to merge them into a single array first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.