0

In my code

for ($i = 0; $i < 100; $i++){                   
    $arr[] = [
        $arr1["some_text_one"] = $value1[$i]->some_text_one,
        $arr1["some_text_one"] = $value2[$i]->some_text_one
    ];
}

I'd like to get something like this:

Array
(
    [0] => Array
        (
            [some_text_one] => blablabla-0
            [some_text_two] => blablabla-0
        )
    [1] => Array
        (
            [some_text_one] => blablabla-1
            [some_text_two] => blablabla-1
        )
)

but instead of this i get:

Array
(
    [0] => Array
        (
            [0] => blablabla-0
            [1] => blablabla-0
        )
    [1] => Array
        (
            [0] => blablabla-1
            [1] => blablabla-1
        )
)

How to get text indexes instead of number? I search in multiple places without success. I will be grateful if someone can help me. Thanks

1 Answers1

2

You're not setting associative keys

Unless there is more to your code, and $arr1 actually exists with those keys, you have set the associative keys incorrectly.

In order to get your desired output, you'd want something that looks like the following:

for ($i = 0; $i < 100; $i++){                   
    $arr[] = [
        "some_text_one" => $value1[$i]->some_text_one,
        "some_text_two" => $value2[$i]->some_text_one
    ];
}

The key you're setting you're actually setting would set the key as the value of the ["some_text_one"] key of the potentially non-existant $arr1.

Mark Overton
  • 1,802
  • 3
  • 15
  • 28