0

I'm getting confused about how to insert array into array and give it a key name. Not number!.

I have the main array, which contains info about customer. the sub array should be called ["verlauf"].

The ["verlauf"] array conatins multiple arrays ( as same as db row count ). the main idea is that the sub array should be having the key name ["verlauf"]. in my code im getting the [0] as key.

Current array output:

array(19) { 
    ["id"]=> string(1) "1" 
    ["name"]=> string(11) "qwert zuiop" 
    ["email"]=> string(23) "[email protected]" 
    ["phn_num"]=> string(12) "123456789" 
    [0]=> array(1) {
        [0]=> array(4) {
            ["datum"]=> string(10) "30.07.2020"
            ["uhr_zeit"]=> string(5) "22:25"
            ["status"]=> string(1) "0"
            ["info"]=> string(34) "some info"
        }
        [1]=> array(4) {
            ["datum"]=> string(10) "30.07.2020"
            ["uhr_zeit"]=> string(5) "23:25"
            ["status"]=> string(1) "1"
            ["info"]=> string(34) "some info"
        }
    }
}

what i want is that the [0] on line 6 in the above example to be ["verlauf"]

My PHP:

while ($row = mysqli_fetch_array($result)) {
     $verlaufArray[] = array(
          "datum"    => $row['datum'],
          "uhr_zeit" => $row['uhr_zeit'],
          "status"   => $row['status'],
          "info"     => $row['info']);
}
array_push($returnArray, $verlaufArray);

Please note that $returnArray is the "main array".

2
  • 1
    $returnArray['verlauf'] = $verlaufArray; ? Commented Jul 30, 2020 at 21:55
  • him.. that is imperessing. how could i miss that. it works. thank you Commented Jul 30, 2020 at 22:01

2 Answers 2

3

You can simply do that by using associate array-

Your PHP code -

while ($row = mysqli_fetch_array($result)) {
     $verlaufArray[] = array(
          "datum"    => $row['datum'],
          "uhr_zeit" => $row['uhr_zeit'],
          "status"   => $row['status'],
          "info"     => $row['info']);
}
$returnArray['verlauf'] = $verlaufArray;
Sign up to request clarification or add additional context in comments.

4 Comments

To elaborate a bit: Take a look at the documentation for array_push(). All it does is add the variable to the end, and since it doesn't provide a key, it will use the first available index. It's better to use bracket notation when adding single elements or when you need to specify a key. OP's problem can be simulated with $returnArray[] = $verlaufArray; - since there'd be no key, it would get appended with the first available index.
@Xhynk thank you for the explain. really helped me man.
Thank you for more explanation. Feel free to edit my answer.
What is the need for the loop at all if the keys are identical to the original ones?
0

There's no need to complicate things so much. It looks like you need just one line of code.

$returnArray['verlauf'] = $result->fetch_all(MYSQLI_ASSOC);

There's no need for the while loop if the structure remains the same. fetch_all(MYSQLI_ASSOC) will give you an array containing associative results from your query.

1 Comment

Thank you. The structure is not staying the same. there are some other rows going into other array. also im not using the class style

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.