0

im trying to insert an array into my multidimensional array, in the first sql part of the code it works fine, however when i use array_splice it doesnt seem to understand that im trying to insert the whole array and just puts up something else. could anyone help me to get the correct answer?

This is the result im trying to get:

Array
(
[0] => Array
    (
        [0] => 1
        [1] => företag1
    )
[1] => Array
    (
        [0] => 3
        [1] => subföretag1
    )

[2] => Array
    (
        [0] => 5
        [1] => subföretag2
    )

[3] => Array
    (
        [0] => 6
        [1] => subföretag3
    )

[4] => Array
    (
        [0] => 2
        [1] => företag2
    )

[5] => Array
    (
        [0] => 4
        [1] => företag3
    )
)

and this is what i get:

Array
(
[0] => Array
    (
        [0] => 1
        [1] => företag1
    )

[1] => 3
[2] => 6
[3] => subföretag3
[4] => 5
[5] => subföretag2
[6] => subföretag1
[7] => Array
    (
        [0] => 2
        [1] => företag2
    )

[8] => Array
    (
        [0] => 4
        [1] => företag3
    )

)

this is the arrays im using:

$partarray=array($row['id'],$row['name']);
in the first part: $partarray=array(random number from db,random name);
in the second part: $partarray=array(random number from db,random name);

this is the code im currently using:

$departments = array();
$sth = $pdo->prepare('SELECT id, name FROM departments WHERE companyid = 1 AND subgroupof = 0');
$sth->execute(); //executes the array
while($row = $sth->fetch()){
    $partarray=array($row['id'],$row['name']);
    array_push($departments, $partarray);
}
$sth = $pdo->prepare('SELECT * FROM departments WHERE companyid = 1 AND subgroupof != 0'); 
$sth->execute(); //executes the array
while($row = $sth->fetch()){
      $partarray=array($row['id'],$row['name']);
      array_splice($departments, $row['subgroupof'], 0, $partarray);
}
print("<pre>".print_r($departments,true)."</pre>");
5
  • Please edit in what your original 2 arrays look like. Commented Dec 8, 2016 at 14:57
  • Possibly you dont understand what array_splice does. It replaces occurances in an array. As your 2 queries should not return that same data, what are you trying to replace with what? Commented Dec 8, 2016 at 15:01
  • PS: There is a lot of unnecessary code in this script as well also demonstrating a lack of understanding. Maybe if you describe what it is you are trying to do in words someone will put you on the right road Commented Dec 8, 2016 at 15:03
  • @bugfroggy ive edited those in if thats what you mean. Commented Dec 8, 2016 at 15:06
  • @RiggsFolly im well aware that this isnt the optimal way, but i couldnt figure out another way to do it. im not a god in php and im here to learn. i thought array_splice with a 0 removed nothing and just inserted? what i wanted to do with the code is create groups and then have subgroups to those groups Commented Dec 8, 2016 at 15:06

1 Answer 1

1

Try this.

$departments = array();
$sth = $pdo->prepare('SELECT id, name FROM departments WHERE companyid = 1 AND subgroupof = 0');
$sth->execute(); //executes the array

while($row = $sth->fetch()){
    $departments[] = array($row['id'], $row['name']);

    $sub_sth = $pdo->prepare(
        "SELECT id, name FROM departments WHERE companyid = 1 AND subgroupof = {$row['id']}"
    );

    $sub_sth->execute(); //executes the array
    while($sub_row = $sub_sth->fetch()) {
          $departments[] = array($sub_row['id'], $sub_row['name']);
    }
}

print("<pre>".print_r($departments,true)."</pre>");
Sign up to request clarification or add additional context in comments.

4 Comments

Pretty sure this is what he says he wants to do. However when he gets this I would bet it is not going to be much use to him. But that is not our problem until he comes back and asks his next question
@mowshon yes this is exactly what i wanted. thanks. could you explain to me what those {} around $row['id'] do or what they're for?
@RiggsFolly i dont get why you have to be so rude just because im not on your level of knowledge in this. if i dont know how to solve a problem and cant ask for it, how am i to learn how to do it?
@Munik You need to know the what is the difference between single-quoted and double-quoted. stackoverflow.com/a/23025917/7205861

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.