I created an array from a very large text file with more than 10.000 entries. This is how my textfile is formatted:
1232.cat=Fred
1232.age=13
1232.size=44
1232.food=chicken
1233.cat=Moe
1233.age=4
1233.size=23
1233.food=fish
This is my $animals array:
array(9435) {
[0]=>
array(5) {
["number"]=>
string(4) "1232"
["cat"]=>
string(4) "Fred"
["age"]=>
string(2) "13"
["size"]=>
string(2) "44"
["food"]=>
string(7) "chicken"
}
...(and so on)
I want to do various checks, for example if for each number cat exists.
foreach($animals as $row) {
if(empty($row['cat'])){
echo "cat is missing in number: ".$row["number"];
}
}
This is working very well for textfiles with around 3000 entries. But for larger files I cannot get any results.
So my question is, what can I do. Obviously I cannot loop through very large arrays. But what is my alternative?
Related
I have a bigger multi dim array I pull from a db.
array(43593) {
[0]=>
array(4) {
["artnum"]=>
string(5) "0422272221"
["ekprice"]=>
string(5) "52.78"
["ekpriceist"]=>
string(5) "52.78"
["lieferantnr"]=>
string(7) "7000202"
}
[1]=>
array(4) {
["artnum"]=>
string(5) "04233337133333"
["ekprice"]=>
string(5) "49.45"
["ekpriceist"]=>
string(5) "49.45"
["lieferantnr"]=>
string(7) "7000211"
}
from another DB server I pull this array
array(73287) {
[0]=>
array(2) {
["ARTNUM"]=>
string(5) "0422272221"
["QUALITAETSMERKMAL"]=>
string(1) "p"
}
[1]=>
array(2) {
["ARTNUM"]=>
string(5) "04233337133333"
["QUALITAETSMERKMAL"]=>
string(1) "m"
}
When I excecute the following from user interface
foreach($aStockArticles as $aStockArticle) {
foreach($aArticleStamm as $artikel)
{
if($aStockArticle['artnum'] == $artikel['artnum'])
{
$aStockArticle['quality'] = $artikel['QUALITAETSMERKMAL'];
}
} }
I recieve
[line 451] [code ] [message Maximum execution time of 30 seconds
exceeded]
As you can see, the amount of articles and the sequence they are ordered can differ from each other.
Since I have no access environment variables
I usually would do a simple join in the db query but in this case its not possible due to the two db servers.
QUESTION:
How do I merge these arrays on the ID (artnum)?
So it looks like the following and stay within the excecution time?
array(43593) {
[0]=>
array(4) {
["artnum"]=>
string(5) "0422272221"
["ekprice"]=>
string(5) "52.78"
["ekpriceist"]=>
string(5) "52.78"
["lieferantnr"]=>
string(7) "7000202"
["QUALITAETSMERKMAL"]=>
string(1) "p"
}
[1]=>
array(4) {
["artnum"]=>
string(5) "04233337133333"
["ekprice"]=>
string(5) "49.45"
["ekpriceist"]=>
string(5) "49.45"
["lieferantnr"]=>
string(7) "7000211"
["QUALITAETSMERKMAL"]=>
string(1) "m"
}
So, I am returning 2 different multi-dimensional arrays from 2 separate db calls.(Can't use a join because of our specific API for db calls)
I need to search through 2nd array and if a value matches that of a value in the first array, I need to append the related data to that row in first array for display in datatable. I have tried numerous things to just get the values to show. Here is what my arrays look like and where I am at with it.
Array 1
{"status":"SUCCESS","message":"Successfully read","data":
[{"ordnbr":"12345",
"custid":"CUSTID #",
"custname":"My Customers Name",
"custordnbr":"54321",
"slsperid":"ID",
"orddate":"2015-05-14",
"shipdate":"2015-06-03",
"dtrdy":"2015-05-29",
"carrier":"Some Carrier",
"totord":"21703",
"shipped":"4341",
"notes":"These are notes",
"siteid":"Some Site",
"orderstatus":"RDY"},
{"ordnbr":"23456",
"custid":"Anoter Customer",
"custname":"Another Customers Name",
"custordnbr":"65432",
"slsperid":"ID2",
"orddate":"2015-05-19",
"shipdate":"2015-06-05",
"dtrdy":"2015-06-05",
"carrier":"Another Carrier",
"totord":"98875",
"shipped":"0",
"notes":" ",
"siteid":"Another Site",
"orderstatus":"GR"},
etc... The list goes on as there are numerous records.
Second array looks kind of like this(var_dump) I realize this is an array of objects but am casting each line to array.
array(41) {
[0]=>
object(stdClass)#2923 (15) {
["trknbr"]=>
string(12) "7890"
["siteid"]=>
string(3) "This Site"
["flatrate"]=>
string(1) "0"
["ratemile"]=>
string(1) "0"
["miles"]=>
string(3) "822"
["covered"]=>
string(1) "N"
["dateship"]=>
string(10) "0000-00-00"
["trkco"]=>
string(0) ""
["totalchrg"]=>
string(1) "0"
["fuelchrg"]=>
string(1) "0"
["tarp"]=>
string(1) "N"
["shipdirection"]=>
string(3) "out"
["comments"]=>
NULL
["custname"]=>
string(30) "CustName"
["detail"]=>
array(1) {
[0]=>
object(stdClass)#2922 (7) {
["trknbr"]=>
string(12) "BR549"
["ordnbr"]=>
string(6) "12345"
["dtrelease"]=>
string(10) "2015-06-04"
["custid"]=>
string(6) "Custid"
["shipto"]=>
string(13) "Some City"
["slsperid"]=>
string(2) "WE"
["comments"]=>
string(21) "Truck # 2 of 5
$2360"
}
}
}
[1]=>
object(stdClass)#2916 (15) {
["trknbr"]=>
string(12) "34563"
["siteid"]=>
string(3) "My Other Site"
["flatrate"]=>
string(1) "0"
["ratemile"]=>
string(1) "0"
["miles"]=>
string(3) "244"
["covered"]=>
string(1) "N"
["dateship"]=>
string(10) "0000-00-00"
["trkco"]=>
string(0) ""
["totalchrg"]=>
string(1) "0"
["fuelchrg"]=>
string(1) "0"
["tarp"]=>
string(1) "N"
["shipdirection"]=>
string(3) "out"
["comments"]=>
NULL
["custname"]=>
string(25) "Another Customer"
["detail"]=>
array(1) {
[0]=>
object(stdClass)#2830 (7) {
["trknbr"]=>
string(12) "34563"
["ordnbr"]=>
string(6) "34578"
["dtrelease"]=>
string(10) "2015-06-08"
["custid"]=>
string(6) "wwweee"
["shipto"]=>
string(12) "Another City"
["slsperid"]=>
string(3) "RRR"
["comments"]=>
string(31) "DO NOT SHIP BEFORE 6/8
$ 769P"
}
}
}
So what I need to do is, for example, if the ordnbr field is the same for any 2 arrays, I need to append a couple fields from the 2nd array onto the first array. Right now, I am just trying to get the data to return so I can make the comparison and it doesn't seem to be comparing. I have tried using a foreach loop and if array1->ordnbr == array2->ordnbr, then do something... That doesn't work. Tried using in_array and searching for array1->ordnbr in array2.
Nothing I have done works. Can someone please tell me in what direction I should proceed to make these comparisons?
Thanks in advance.
I may not have been real clear on what I need to do... Ok... With Array 1(which did start out as a JSON object but which I converted to multidimensional array using json_decode), I need all rows regardless of whether they match Array 2 or not. However, if ordnbr matches an ordnbr on a row in Array2, then I need to include some data from that row in Array2 into the corresponding row of Array1.
What you would do is to create 2 associative arrays, one for each array with key is ordnbr and value is the whole object contain ordnbr. For example:
$arr1 = array()
foreach ($input1["data"] as $row){
$arr1[$row["ordnbr"]] = $row;
}
The do the same for your second array $arr2. After that, a simple foreach would do the job
foreach ($arr1 as $order_number => $row){
if in_array($order_number, $arr2){
// $row2 will have the same $order_number as $row1
$row2 = $arr2[$order_number];
// then do whatever you need here
// ...
}
}
I'm trying to get the values of a sub array out in a foreach loop.
I looked at this example as it seemed really relevant to my problem, but unfortunately I've not been able to get it to work, and I'm hoping someone here can pick up where I've gone wrong.
PHP foreach not working with sub-array
I have an array called $booked and inside that array I have a sub array $booked['30_booked'].
Within that second array there are multiple values, such as title, name, address etc.
My code currently looks like this:
foreach($booked['30_booking'] as $new_var){
var_dump('</br>', $booked['30_booking']);
var_dump('</br>', $new_var);
var_dump($new_var['title'], $new_var->title, $booked['30_booking']->title); exit();
}
I've output the data as you can see above in var_dump statements to try and get one of these methods to work.
Unfortunately nothing within $new_var is pulling out the title, but $booked['30_booking']->title
Have I not put it into the foreach statement correctly?
All help appreciated - thanks!
EDIT:
Main array output snippet:
array(6) { ["30_booked"]=> object(stdClass)#21 (34) { ["id"]=> string(2) "30" ["title"]=> string(2) "Ms" ["firstname"]=> string(5) "FIRST NAME" ["surname"]=> string(9) "LAST NAME" ["address"]=> string(6) "- -- -" ["postcode"]=> string(7) "FAK E99" ["country"]=> string(14) "United Kingdom" ["phone"]=> string(11) "01221111111" ["alt_phone"]=> string(0) "" ["email"]=> string(25) "fake#fake.co.uk" ["notes"]=> string(8) "FAKE DEAL" } }
EDIT 2:
Sub Array $booked['30_booking'] snippet:
object(stdClass)#21 (34) { ["id"]=> string(2) "30" ["title"]=> string(2) "Ms" ["firstname"]=> string(5) "FIRST NAME" ["surname"]=> string(9) "LAST NAME" ["address"]=> string(6) "- -- -" ["postcode"]=> string(7) "FAK E99" ["country"]=> string(14) "United Kingdom" ["phone"]=> string(11) "01221111111" ["alt_phone"]=> string(0) "" ["email"]=> string(25) "fake#fake.co.uk" ["notes"]=> string(8) "FAKE DEAL" }
EDIT 3:
My var_dump of the $new_var by itself is bringing back the value of the id - but when I try and get the second value out the sub array "title" it doesn't return anything.
FINAL FIX:
Thanks to Kita I realised I was returning a std class object and not a second array, something that I stupidly missed the first time round. Because of that I can't actually foreach on the object.
Which led me to this post which will help me fix the issue:
PHP foreach array with stdClass Object
Thank you very much for all your help!!!
You expected an array inside $booked['30_booking'] but in fact there was a stdClass object.
array(6) {
["30_booked"]=> object(stdClass)#21 (34) {
["id"]=> string(2) "30"
["title"]=> string(2) "Ms"
["firstname"]=> string(5) "FIRST NAME"
["surname"]=> string(9) "LAST NAME"
["address"]=> string(6) "- -- -"
["postcode"]=> string(7) "FAK E99"
["country"]=> string(14) "United Kingdom"
["phone"]=> string(11) "01221111111"
["alt_phone"]=> string(0) ""
["email"]=> string(25) "fake#fake.co.uk"
["notes"]=> string(8) "FAKE DEAL"
}
//I assume you have left out the other array elements from the Main array snippet.
}
Getting stdClass instead of array usually happens when you parse a JSON string with json_decode() without the second parameter.
// without second parameter
var_dump( json_decode( '{"id":"30"}' ) );
object(stdClass)#1 (1) {
["id"]=>
string(2) "30"
}
// 'true' as second parameter
var_dump( json_decode( '{"id":"30"}', true ) );
array(1) {
["id"]=>
string(2) "30"
}
Above examples are hosted at: http://ideone.com/GNMRlD
Since stdClass object itself does not provide iteration functionality, using it with foreach will yield errors.
You can convert stdClass into array with functions such as http://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/ .
For eg. if your array looks like below. foreach which I used will be working
$new_array = array(array('total'=>array('title'=>'test','text'=>'text')));
foreach ($new_array as $val)
{
print_r($val['total']['title']); // Use this for array
print_r($val->total->title); // Use this for object
}
if your array looks like below. Use the below foreach.
$new_array = array(array('total'=>array(array('title'=>'test','text'=>'text'),array('title'=>'test1','text'=>'text1'))));
foreach ($new_array as $val)
{
foreach($val['total'] as $new_val)
{
print_r($new_val['title']);
}
}
You can try this, I hope that helps you
foreach($booked as $new_var){
var_dump('</br>', $new_var);
var_dump('</br>',$new_var['30_booking']->title);
}
I think in your foreach there is mistake of word 30_booking
since in your main array out put display it shows 30_booked
check that also
I am noticing some strange behavior while looping through some data. I'm sure it's something simple, but I can't seem to be able to find the bug.
I have the following logic:
<?php
print 'dumping data : <BR>';
var_dump($portvlan);
print '<BR>';
print 'looping through data: <BR>';
foreach ($portvlan as $vlandetail){
echo 'Vlanid: '.$vlandetail['VlanId'].'<BR>';
echo 'Name: '.$vlandetail['Name'].'<BR>';
echo 'Mode: '.$vlandetail['Mode'].'<BR>';
}
?>
This is the output that I'm getting:
dumping data :
array(3) { ["VlanId"]=> string(2) "33" ["Name"]=> string(6) "USR_33" ["Mode"]=> string(6) "Access" }
looping through data:
Vlanid: 3
Name: 3
Mode: 3
Vlanid: U
Name: U
Mode: U
Vlanid: A
Name: A
Mode: A
What I was expecting was to see it print a row with 3 cells, with the following values:
33, USR_33, Access.
Can you tell me where I'm going wrong?
Thanks.
EDIT 1
This logic works fine when the $portvlan array has more than one entry.
For example, on another set of data, the var_dump gives this result:
array(6) { [0]=> array(3) { ["VlanId"]=> string(1) "1" ["Name"]=> string(1) "1" ["Mode"]=> string(5) "Trunk" } [1]=> array(3) { ["VlanId"]=> int(2) ["Name"]=> int(2) ["Mode"]=> string(5) "Trunk" } [2]=> array(3) { ["VlanId"]=> int(3) ["Name"]=> int(3) ["Mode"]=> string(5) "Trunk" } [3]=> array(3) { ["VlanId"]=> int(4) ["Name"]=> int(4) ["Mode"]=> string(5) "Trunk" } [4]=> array(3) { ["VlanId"]=> int(5) ["Name"]=> int(5) ["Mode"]=> string(5) "Trunk" } [5]=> array(3) { ["VlanId"]=> string(2) "33" ["Name"]=> string(2) "33" ["Mode"]=> string(5) "Trunk" } }
And the loop logic works fine.
$vlandetail will be populated with a different item from the array on every iteration of the loop. You shouldn't be treating $vlandetail as an array. Just use it directly.
To get the key name of the array entry, you have to change the loop structure to this:
foreach ($portvlan as $key => $vlandetail) {
echo $key . ': ' . $vlandetail . '<br>';
}
How you get your $portvlan?
If you can know is $portvlan is not an array of $vlandetail, you can simply do this,
$portvlan = array($portvlan);
// start loop
Or you can do this before you loop it, to make sure $portvlan is a numerical array formed by $vlandetail
$portvlan = (isset($portvlan[0]))?$portvlan:array($portvlan);
// start loop
I hope I can make this question clear enough.
I'm looking to put a list of arrays inside one master array, dynamically, so that it looks like this:
masterarray {
array1
{ [0]=>VAL1 [1]=>VAL2 }
array2
{ [0]=>VAL1 [1]=>VAL2 }
array3
{ [0]=>VAL1 [1]=>VAL2 }
}
I've tried, but I could only get it to look like this:
array(1) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
array(2) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [1]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
array(3) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [1]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [2]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
And that's definitely not what I'm aiming for. Nothing seems contained. I need the format specified above.
I'm using the explode function on a string pulled from a file to make this table of arrays (I think you call it that)
Here is the code I'm using that's not working.
$variabledebugging = file("FILE.TXT");//LOOK IN THIS FILE FOR THE NUMBER AND SET IT TO A VAR.
$i=0;
foreach($variabledebugging as $placeholder){
$variabledebuggingtbl[] = explode("\t",$variabledebugging[$i]);
var_dump($variabledebuggingtbl);
$i++;
}
I've tried a couple of different variations, but that's the one I'm using now.
To be clear, that file being pulled (each line as a value in an array) has 2 things written to each line, separated by a tab character, so that's the system I'm going on.
Thank you! I'm sure this is a simple task, I just can't think it through.
Oh and while I'm at is there a way to make debugging more readable?
You ARE getting the right result. The reason it seems wrong is that you are running var_dump inside the loop. And why don't you use the $placeholder variable?
$variabledebugging = file("FILE.TXT");
foreach($variabledebugging as $placeholder){
$variabledebuggingtbl[] = explode("\t", $placeholder);
}
var_dump($variabledebuggingtbl);
I'm not sure what you mean by "making debugging more readable", but if you want some linebreaks and indentation you should just look in the generated HTML code. var_dump do add spacing to make it readable but it is ignored by the web browser. If you don't want to read the HTML source, just add your var_dump to a <pre> element.