1

i am trying to access the value of an array that i have created, but it seems to fail.

I am looping through an array that sends VIA http, and adding the docno and entryno to new array called $ArrID, it can be added to the new array, however when i try to access the ArrID it seem to get nothing from the array itself while i am confident that ArrID contain the items

CODE

$ArrID = [];
foreach ($form_data_body as $key => $value) {
  $docno = $value -> doc_no;
  $entryno = $value -> entryno;
  if($mysqli->query($sql) == 1){
    $itemArr = array('docno' => $docno, 'entryno' => $entryno);
    $ArrID[] = $itemArr;
  }
}

if(count($ArrID)>0){
    foreach ($ArrID as $key => $values) {
      echo $values -> docno;
    }
}
Shahensha Khan
  • 265
  • 1
  • 19
Joseph Goh
  • 689
  • 5
  • 16
  • 38

2 Answers2

1

You are mixing with object and array

see http://php.net/manual/en/language.types.array.php http://php.net/manual/en/language.types.object.php

$ArrID = [];
foreach ($form_data_body as $key => $value) {
  $docno = $value['doc_no'];
  $entryno = $value['entryno'];
  if($mysqli->query($sql) == 1){
    $itemArr = array('docno' => $docno, 'entryno' => $entryno);
    $ArrID[] = $itemArr;
  }
}

if(count($ArrID)>0){
    foreach ($ArrID as $key => $values) {
      echo $values['docno'];
    }
}
ASR
  • 1,801
  • 5
  • 25
  • 33
0

How are you sure that your array has the data? Make sure by echoing the size of the array and see if its greater then zero.

Try the code below and check if it works or not. There was a redundant assign and reassign code, actually you were assigning array to a variable, and variable to an array again.

$ArrID = [];
foreach ($form_data_body as $key => $value) {
  $docno = $value -> doc_no;
  $entryno = $value -> entryno;
  if($mysqli->query($sql) == 1) {
    $ArrID[] = array('docno' => $docno, 'entryno' => $entryno); 
  }
}
if(count($ArrID)>0) {
  foreach ($ArrID as $key => $values) {
    echo $values -> docno;
  }
}
Minifyre
  • 510
  • 2
  • 5
  • 17
Shahensha Khan
  • 265
  • 1
  • 19
  • i put the condition `if(count($ArrID)>0){` and echoing some dummy data if hit the condition and it does hit the condition – Joseph Goh May 31 '16 at 03:25
  • so does it output anything? the docno as u feed it or something else? – Shahensha Khan May 31 '16 at 03:27
  • something else, not the `docno`, i put `echo "got it";` right after the `if(count($ArrID)>0){` and before `foreach ($ArrID as $key => $values) {` and it does echoing the `got it` so i am assume the array is not empty. – Joseph Goh May 31 '16 at 03:30
  • make sure u r not echoing the memory address, its been 4 years i didnt touch php. but if array is not empty and it hits the condition either u receive the data in wrong format OR ur echoing th memory location instead. – Shahensha Khan May 31 '16 at 03:32
  • as @ASR posted above, `$value['docno']` should be used io `$value -> docno` just wonder what the problem with those syntax – Joseph Goh May 31 '16 at 03:36