0

I was bug squashing my script when I discovered that an array ($array) contained two arrays with no key like so:

array(19) {
  ["id"]=>
  string(3) "243"
  ["var"]=>
  string(4) "test"
}

array(10) {
  ["id"]=>
  int(243)
}

They both contain different data, i just removed most of it to show what I'm talking about. Now I will find the cause of this eventually and fix it, but what I need is a temporary fix to get the value of var from the first array. Currently when I use print_r and var_dump I do get the actual value of var but also a NULL. For that reason I can't seem to store the value of var in a variable.

Any ideas?

Here is the full array (with some sensitive data masked)

Array
(
    [id] => 243
    [ordering] => 0
    [state] => 1
    [checked_out] => 203
    [checked_out_time] => 2013-07-17 14:28:15
    [status] => new
    [order_id] => 84
    [username] => 267
    [ankleside] => left
    [engraving] => left
    [serial] => 152
    [color_padding] => left
    [color_shell] => left
    [scan] => SCAN_2013-07-17_xxxxxx_X_hotmail.com_LEFT.PNG
    [workfile] => WORK_2013-07-17_xxxxxx_X_hotmail.com_LEFT.png
    [stlfile] => 2013-07-17_xxxxxx_X_hotmail.com_LEFT_.jpg
    [timespent] =>
    [created_by] => 203
)

Array
(
    [id] => 243
    [status] => new
    [username] => 267
    [ankleside] => left
    [engraving] => left
    [scan] => SCAN_2013-07-17_xxxxxxx_X_hotmail.com_LEFT.PNG
    [workfile] =>
    [stlfile] => fb-foto.png
    [issues] =>
)
Prix
  • 19,417
  • 15
  • 73
  • 132
Nick
  • 2,862
  • 5
  • 34
  • 68
  • 3
    Aren't "id" and "var" the keys? – StephenTG Jul 17 '13 at 14:41
  • you have a proper ID on both arrays being 243 the id however on 1 its in int on the other it is as string. – Prix Jul 17 '13 at 14:42
  • 1
    They is no such thing as "an array with no key". Every element of an array has a corresponding key. The output shown above is the result of two separate variables passed to `var_dump()` – DaveRandom Jul 17 '13 at 14:42
  • @StephenTG they are for each of their arrays, but the two arrays themselves don't have keys. So when I try to get the value of a key which is in the first array and not in the second array, I can't store that in a variable – Nick Jul 17 '13 at 14:42
  • When you do `var_dump($string)` it doesn't show a numerical key for each of those arrays? – Expedito Jul 17 '13 at 14:43
  • Is the code you've posted the result of doing var_dump() on $array? – StephenTG Jul 17 '13 at 14:44
  • @DaveRandom I'm just passing a single variable to `var_dump()`, but it throws out two arrays. So what I mean with "an array with no key" is that I can't select the array which I want, since there are apparently two of them – Nick Jul 17 '13 at 14:44
  • 3
    please show us your PHP-code to avoid misunderstanding – steven Jul 17 '13 at 14:45
  • @Ortix92 so you have 2 arrays being 1 a complete array and the other an incomplete one with the same data ? Would be better if you show the complete dump of either print_r or var_dump. – Prix Jul 17 '13 at 14:45
  • 1
    Are you sure it isn't inside a loop or something making it `var_dump()` twice? Try adding `echo "done";` after the `var_dump();` – Pitchinnate Jul 17 '13 at 14:45
  • You say you've removed some data - did you remove the "var" element from the second array? If not, that's probably why you get "test" and NULL - it doesn't exist in the second one. – MDEV Jul 17 '13 at 14:45
  • 1
    @Ortix92 `I'm just passing a single variable to var_dump()` - That is simply not possible. Are you sure you're not calling it in a loop? – DaveRandom Jul 17 '13 at 14:47
  • [Merge the two arrays based on unique fields](http://stackoverflow.com/questions/4660675/php-array-merge-array-unique) – Prix Jul 17 '13 at 14:48
  • Wow.. apparently it's called twice! But there is no `for-loop` in my code anywhere that's why i didn't think of that. Maybe somewhere in a parent class somewhere :S Thanks for pointing me in the right direction guys! – Nick Jul 17 '13 at 14:48
  • I thought that php array values always have keys. – webrama.pl Jul 17 '13 at 14:49

1 Answers1

3

Somehow var_dump(); is called twice. Try adding echo "done"; after var_dump($array); to verify.

You could also add debug_print_backtrace(); to help you see how it is called twice. http://www.php.net/manual/en/function.debug-print-backtrace.php

Pitchinnate
  • 7,517
  • 1
  • 20
  • 37
  • At this point, it's probably just hidden somewhere in a (likely sizeable) chunk of code. Seems like it's just a matter of hunting it down, not really what SO is for... – StephenTG Jul 17 '13 at 14:52
  • 1
    @steven there is no point. It's 200 lines of code which I'm sure you don't want to sift through. And on top of that, the method returns to a parent method which is another 600 lines of code. There are no loops in my method. So it must be in the parent – Nick Jul 17 '13 at 14:52
  • 2
    @Ortix92 see updated answer, try `debug_print_backtrace();` might help you figure it out. – Pitchinnate Jul 17 '13 at 14:53