0

I am having an issue where I am trying to save an ID as an array index, but when I convert the array to json, the ID is appearing as an object and not as an array element.

This is a brief example:

    $ID = 1001 ;
    $item = [] ;
    $item["regional"] = [] ;
    $item["regional"][$ID] = $someInfo ; //object
    json_encode($item) ;

I am returning the json to my app, and when I look at the json formatting the above lists 1001 as an object:

     [0: 
         { regional:
              { 1001 :
                   someInfoA : valueA
                   someInfoB : valueB
              }
         }
    ]

If I change the value of $ID = 0, then it stores the ID as a proper array index - which is what I am needing:

    $ID = 0 ;
    $item = [] ;
    $item["regional"] = [] ;
    $item["regional"][$ID] = $someInfo ; //object
    json_encode($item) ;

The above now shows ID = 0 as an array index [0]:

     [0: 
         { regional:
              [ 0 :
                   {
                   someInfoA : valueA
                   someInfoB : valueB
                   }
              ]
         }
    ] 

How can I get the first example to set the array index as 1001 and be treated as an array element versus as an object. I am needing to do this so I can quickly see if that array index element/ID exists to either add to it, or create it then add to it.

If this can't be done, then how can I search a multi-dimensional array/object to return the array index that contains the object value 1001 ?

     [0: 
         { regional:
              [ 0 :
                   {
                   ID: 1000
                   someInfoA : valueA
                   someInfoB : valueB
                   }
              ]
              [ 1 :
                   {
                   ID: 1001
                   someInfoA : valueA
                   someInfoB : valueB
                   }
              ]
         }
    ] 

$index = array_key_exists($item["regional"],1001) ;  // or something like this
var_dump($index) ; // index = 1 ;
5
  • 1
    JavaScript does not know “associative” arrays - there is not other way than an object, if you want keys like that. Commented Nov 4, 2019 at 14:27
  • 1
    “I am needing to do this so I can quickly see if that array index element/ID exists” - so check if the object has a property by that name then. Commented Nov 4, 2019 at 14:29
  • @04FS Ok, as I thought. But that is my last question, if I can't do what I was trying, then how (in PHP) can I extract the index that the ID:1001 belongs to without doing a foreach loop. Commented Nov 4, 2019 at 14:33
  • 3
    You can json_decode it as an array using true as second arg. Also, look at array_column, works on objects also in 7+ Commented Nov 4, 2019 at 14:40
  • Its not fully clear what your are trying to do, but, as said above you are probably looking for the second argument (to true) in json_decode, or you can try to convert to an array using the (array) $item["regional"] syntax, and php.net/manual/en/function.array-search.php may be part of your answer Commented Nov 4, 2019 at 16:50

0

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.