0

Есть 2 массива:

    Array
(
    [0] => 8
    [1] => 99
    [2] => 0
    [3] => 65

)
Array
(
    [0] => Text1
    [1] => Text2
    [2] => Text3
    [3] => Text4

)

Можно ли как то обьединить их в один вида:

Array
(
    [0] => Text1 => 8
    [1] => Text2 => 99
    [2] => Text3 => 0
    [3] => Text4 => 65

)

Или как-нибудь удобнее, чтобы суметь вывести в php Text1 = 8 и т.д?

2 ответа 2

2

Используйте array_combine():

$keys = ['Text1', 'Text2', 'Text3', 'Text4'];
$vals = [8, 99, 0, 65];

$array = array_combine($keys, $vals);

Результат:

array (
  'Text1' => 8,
  'Text2' => 99,
  'Text3' => 0,
  'Text4' => 65
)
0

Возможно я не так объяснил, что хотел получить, но ответ нашел

 $result = array_map(function($text, $num){
   return [ 
    'text' => $text,
    'num' => $num,
   ];
   // return "$text $num"; 
 }, $text, $num);

 print_r($result);

Результат:

Array
(
    [0] => Array
        (
            [text] => Text1
            [num] => 8
        )

    [1] => Array
        (
            [text] => Text2
            [num] => 99
        )

    [2] => Array
        (
            [text] => Text3
            [num] => 0
        )

    [3] => Array
        (
            [text] => Text4
            [num] => 65
        )


)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.