1

My function is waiting response in array as in format below:

main[insert][id][insert]='some value'

I have prepared loop:

for ($i=0; $i<100; $i++) {
$data_array[] = array(
"insert" => array($data[$i]["id"] => 
array ("insert" => "some value"; }

However after I run it I have such values:

main[insert][//i value from 0 to 99][id][insert] = "some value"
main[insert][0][005][insert] = "some value"
main[insert][1][008][insert] = "some value"

Everything looks good I just don't need this loop i values, I just need values without it of such view: main[insert][id][insert]='some value'

8
  • 1
    What is the value of $data? Commented Dec 10, 2018 at 23:28
  • @MA But I take values from other array in a loop. So it changes dynamically, there are different values of id for every separate $i. And it works perfect as I have different ids, but I don't need [$i] in it. Commented Dec 10, 2018 at 23:31
  • 1
    Can you paste content of $data array? Commented Dec 10, 2018 at 23:32
  • 1
    The code you have written should work fine and I am helpless unless you tell more what is returned by $data[$i]["id"]? Commented Dec 10, 2018 at 23:46
  • 1
    Also, you have syntax errors. Commented Dec 10, 2018 at 23:49

1 Answer 1

2

Try replacing your loop with the below code:

It will be great if you could provide the structure of $data

for ($i=0; $i<100; $i++) {

  $data_array[][ "insert"] = [ 
                                $data[$i]["id"] => ["insert" => "some value"]
                               ]
}

edit: From your solution in comment: @VAPPM

for ($i=0; $i<100; $i++) { 
  $current_id=$data[$i]['id']; 
  $data_array['insert'][$current_id]['insert'] = "some value"; 
}
Sign up to request clarification or add additional context in comments.

8 Comments

Yes, it worked. Thank you for your support. Wish all the best in your coding.
@VAPPM This code won't even run due to syntax errors! How can you accept this answer?
@Nick I have updated the answer. Please check it now.
@Nick But this worked in my case, I have just simplified code by removing one array as dexter has proposed and now I have what I wanted.
@VAPPM The code still has syntax errors and produces exactly the same result as your original code after those errors are fixed.
|

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.