49013

How to get values in an array with many arrays in php?

Question:

I am confused with this list of array. I have a text file that contains the values that I will need to insert to the database. I have exploded it so that I can get those separated values.

REP 1020001,3,140822,140822;0111,260.00,23,34,3,54,1,2,4,5,12,23,46;0214,22.00,32,4,11,25,4,12,23,5,2,2,44;0313,25.00,5,52,12,45,12,5,6,7,12,3,33;

My code is just like this:

$read = FileRead($tmp); $perline = explode(";",$read); for($j=0; $j<count($perline); $j++) { $percomma = explode(",", $perline[$j]); print_r ($percomma); }

And the result is this:

Array ( [0] => 1020001 [1] => 3 [2] => 140822 [3] => 140822 ) Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) Array ( [0] => 0214 [1] => 22.00 [2] => 32 [3] => 4 [4] => 11 [5] => 25 [6] => 4 [7] => 12 [8] => 23 [9] => 5 [10] => 2 [11] => 2 [12] => 44 ) Array ( [0] => 0313 [1] => 25.00 [2] => 5 [3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] => 5 [8] => 6 [9] => 7 [10] => 12 [11] => 3 [12] => 33 ) Array ( [0] => )

What should I do to get the value from the 2nd array to the 4th array? Should I put it inside another array to make it multidimensional? Or there are other ways to solve this?

<strong>EDIT</strong>

My question is how will I be able to get the values from the print_r($percomma) when there are a lot of arrays in the result. The result array is up there that says <strong>And the result is this</strong>

<strong>EDIT 2</strong>

As making the array to be multidimensional I get this as a result:

Array ( [0] => Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) ) Array ( [0] => Array ( [0] => 0214 [1] => 22.00 [2] => 32 [3] => 4 [4] => 11 [5] => 25 [6] => 4 [7] => 12 [8] => 23 [9] => 5 [10] => 2 [11] => 2 [12] => 44 ) ) Array ( [0] => Array ( [0] => 0313 [1] => 25.00 [2] => 5 [3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] => 5 [8] => 6 [9] => 7 [10] => 12 [11] => 3 [12] => 33 ) ) Array ( [0] => Array ( [0] => ) )

It shows that they're all individual arrays. My code is this:

$read = FileRead($tmp); $perline = explode(";",$read); for($j=0; $j<count($perline); $j++) { $percomma = explode(",", $perline[$j]); $entries = array($percomma); print_r ($entries); }

<strong>EDIT 3</strong>

From <strong>subas_poudel</strong>'s answer I get this result:

Array ( ) Array ( [0] => Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) ) Array ( [0] => Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) [1] => Array ( [0] => 0214 [1] => 22.00 [2] => 32 [3] => 4 [4] => 11 [5] => 25 [6] => 4 [7] => 12 [8] => 23 [9] => 5 [10] => 2 [11] => 2 [12] => 44 ) ) Array ( [0] => Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) [1] => Array ( [0] => 0214 [1] => 22.00 [2] => 32 [3] => 4 [4] => 11 [5] => 25 [6] => 4 [7] => 12 [8] => 23 [9] => 5 [10] => 2 [11] => 2 [12] => 44 ) [2] => Array ( [0] => 0313 [1] => 25.00 [2] => 5 [3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] => 5 [8] => 6 [9] => 7 [10] => 12 [11] => 3 [12] => 33 ) ) Array ( [0] => Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) [1] => Array ( [0] => 0214 [1] => 22.00 [2] => 32 [3] => 4 [4] => 11 [5] => 25 [6] => 4 [7] => 12 [8] => 23 [9] => 5 [10] => 2 [11] => 2 [12] => 44 ) [2] => Array ( [0] => 0313 [1] => 25.00 [2] => 5 [3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] => 5 [8] => 6 [9] => 7 [10] => 12 [11] => 3 [12] => 33 ) )

The last set of array is what I needed but how can I get those values?

<strong>EDIT 4</strong> With my simple mistake I put the array_slice inside my for loop that's why I get too many arrays. Now this is the result of @subas_poudel's answer.

Array ( [0] => Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) [1] => Array ( [0] => 0214 [1] => 22.00 [2] => 32 [3] => 4 [4] => 11 [5] => 25 [6] => 4 [7] => 12 [8] => 23 [9] => 5 [10] => 2 [11] => 2 [12] => 44 ) [2] => Array ( [0] => 0313 [1] => 25.00 [2] => 5 [3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] => 5 [8] => 6 [9] => 7 [10] => 12 [11] => 3 [12] => 33 ) )

Answer1:

you can use multidimensional array to hold all the value. then you can get all the array you want by either $percomma[index you want] or using array_slice.

$read = FileRead($tmp); $perline = explode(";",$read); for($j=0; $j<count($perline); $j++) { $percomma[] = explode(",", $perline[$j]); } echo '<pre>'.print_r (array_slice($percomma,1,3),true).'</pre>';

Answer2:

$array = array ( array("bla",22,18), array("blaa",15,13), array("blaaa",5,2), array("blaaaa",17,15) ); for ($row = 0; $row < 4; $row++) { echo "

<b>Row number $row</b>

"; echo "<ul>"; for ($col = 0; $col < 3; $col++) { echo "<li>".$array[$row][$col]."</li>"; } echo "</ul>"; }

Answer3:

I'm going to copy/edit your original code posting, to show what I was talking about in my comment.

$read = FileRead($tmp); $perline = explode(";",$read); $percomma = array(); //new array-declaration line for($j=0; $j<count($perline); $j++) { $percomma[j] = explode(",", $perline[$j]); print_r ($percomma[j]); }

Your first "And the result is" line is a printed sequence of arrays, and would be unchanged by implementing the above tweaks to your code. However, because the values in the $percomma variable are no longer overwritten in each loop that obtains an array from the explode() function, you may now, at any point after the above code, do something like:

print_r($percomma[1]); //re-print 2nd array, or $ary_el = $percomma[2][1]; //get 2nd element of 3rd array (22.00) into a variable.

Recommend

  • Oreo - Foreground service does not show foreground notification
  • PHP 101 : How to call an external PHP file with action
  • Internet Explorer 9 not posting AJAX post request
  • Link stylesheets to Django template
  • How to sort an array considering localization?
  • How can I find the longest string in a multi-dimensional array?
  • Store array in cookie
  • Cookie multiple values
  • PHP Database driven Multilevel Menu
  • Recursive regex not matching template blocks
  • How to get the relative position of a Dictionary element?
  • Create function that can pass a parameter without making a new component
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • How can we prepend rows to a react native list-view?
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Tamper-proof configuration files in .NET?
  • Appending Character to Character Array In C
  • PHP CURL timing out but CLI CURL works
  • Declaring variable dynamically in VB.net
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Jackson Parser: ignore deserializing for type mismatch
  • Java: can you cast Class into a specific interface?
  • Initializer list vs. initialization method
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Date difference with leap year
  • AES padding and writing the ciphertext to a disk file
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Javascript + PHP Encryption with pidCrypt
  • ORA-29908: missing primary invocation for ancillary operator
  • Rearranging Cells in UITableView Bug & Saving Changes
  • WOWZA + RTMP + HTML5 Playback?
  • AngularJs get employee from factory
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • How does Linux kernel interrupt the application?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?