0

Edit: I found a answer, so if we have to loop twice and create array inside array just do:

$data1=array_merge($data1,$result->fetch_all(MYSQLI_ASSOC));

my structure need to look like that:

['some':[{'num1':1,'num2':2},{'num1':10, 'num2':20}],'nextOne':[{'num1':1,'num2':2},{'num1':10, 'num2':20}],..... (and more...)

I have to create json data from around 4k records so i separate main data and i did something like that:

$data1 = array();
$data2 = array();
//etc.. up to 14 records

I had one existing table but i also create next with main separators, for separate data so for example:

$sql1 = "SELECT some.tit AS Title,someExtra.sub AS SubTitle FROM some INNER JOIN someExtra ORDER BY orderNr";
$result1 = $conn->query($sql1);
  while($row = $result1->fetch_assoc()){
  $myData = $row["Title"];
  $my2ndData = $row["SubTitle "];

And now i need to find that data in next table and pass it to my array so..

$sql2 = "SELECT * FROM main WHERE expect = '$myData' AND expect2 = $my2ndData";
  $result2 = $conn->query($sql2);
    if($myData == 'That i need'){

      $data1+=$result2->fetch_all(MYSQLI_ASSOC);

So like in example we have 2 loops, if we will have for example few records in 'someExtra' table that will match 'some' table will look next one for example : 4 times.. So if i will echo number of results inside my array just before i will send it to right array i will have 4,22,1,55,33 record in each. But on end of a script when i will count array i will heave only 33 records instead of have 115.

Also i try

array_push($data1, $result2->fetch_all(MYSQLI_ASSOC));

But that one doesynt work eather, becouse it add arrays with values to existing array... so i will have array in array in array with is not that i wont.

My final output look like that:

$outp=array('some'=>$data1,'AnextOne'=>$data2)
$myJSON = json_encode($outp, JSON_NUMERIC_CHECK);
2
  • 1
    You can answer your own question! stackoverflow.com/help/self-answer I would suggest this instead of the edit so someone in the future looking to solve the same issue can see how you solved it. Commented Apr 29, 2019 at 19:34
  • Ye, you are right :D Commented Apr 29, 2019 at 20:45

1 Answer 1

1

After few hour of trying i solve the issue. So if you will have to transfer your data from database in to json format, and separate your data by specifying keys. just do:

$data1=array_merge($data1,$result->fetch_all(MYSQLI_ASSOC));

Where:

$data1 - name of your 1st array (value for key number 1)

$result->fetch_all(MYSQLI_ASSOC) - array format where key is a row name and value is your data from row.

array_merge - that will connect your new array with old one without creating other arrays inside existing one.

Sign up to request clarification or add additional context in comments.

Comments

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.