Hướng dẫn mix array php

First is a solution that will consume the input arrays in a loop while building the new structure. You can always cache separate copies of the input if you need them elsewhere.

Nội dung chính

  • How do I add associative array to array in PHP?
  • How do you add to an associative array?
  • How do you sum an associative array in PHP?
  • How do I get associative array in PHP?

All solutions below will work even if the two arrays have different lengths -- any remaining elements will be appended to the end of the result array after the loop.

Code: (Demo)

$result = [];
while ($arr1 && $arr2) {
    $result += array_splice($arr1, 0, 1)
        + array_splice($arr2, 0, 1);
}
$result += $arr1 + $arr2;
var_export($result);

Another way without consuming the input arrays is to build lookup arrays:

Code: (Demo)

$max = max(count($arr1), count($arr2));
$keys1 = array_keys($arr1);
$keys2 = array_keys($arr2);

$result = [];
for ($x = 0; $x < $max; ++$x) {
    if (isset($keys1[$x])) {
        $result[$keys1[$x]] = $arr1[$keys1[$x]];
    }
    if (isset($keys2[$x])) {
        $result[$keys2[$x]] = $arr2[$keys2[$x]];
    }
}
var_export($result);

Or you could use array_slice() to isolate one element at a time from each array without damaging the input arrays, nor generating warnings.

Code: (Demo)

$result = [];
for ($i = 0, $count = count($arr1); $i < $count; ++$i) {
    $result += array_slice($arr1, $i, 1)
        + array_slice($arr2, $i, 1);
}
$result += $arr1 + $arr2;
//go through each question
foreach($file_data as $value) {
   //separate the string by pipes and place in variables
   list($category, $question) = explode('|', $value);

   //place in assoc array
   $data = array($category => $question);
   print_r($data);

}

This is not working as it replaces the value of data. How can I have it add an associative value each loop though? $file_data is an array of data that has a dynamic size.

Nội dung chính

  • How do I add associative array to array in PHP?
  • How do you add to an associative array?
  • How do you sum an associative array in PHP?
  • How do I get associative array in PHP?

MaxiGui

5,8864 gold badges14 silver badges32 bronze badges

asked Mar 21, 2011 at 23:11

You can simply do this

$data += array($category => $question);

If your're running on php 5.4+

$data += [$category => $question];

answered Sep 8, 2014 at 5:52

4

I think you want $data[$category] = $question;

Or in case you want an array that maps categories to array of questions:

$data = array();
foreach($file_data as $value) {
    list($category, $question) = explode('|', $value, 2);

    if(!isset($data[$category])) {
        $data[$category] = array();
    }
    $data[$category][] = $question;
}
print_r($data);

webvitaly

4,1217 gold badges29 silver badges46 bronze badges

answered Mar 21, 2011 at 23:13

ThiefMasterThiefMaster

302k78 gold badges581 silver badges625 bronze badges

2

Before for loop:

$data = array();

Then in your loop:

$data[] = array($catagory => $question);

Uwe Keim

38.6k56 gold badges173 silver badges281 bronze badges

answered Mar 21, 2011 at 23:12

moemoe

28.1k3 gold badges18 silver badges16 bronze badges

6

I know this is an old question but you can use:

array_push($data, array($category => $question));

This will push the array onto the end of your current array. Or if you are just trying to add single values to the end of your array, not more arrays then you can use this:

array_push($data,$question);

lasec0203

2,2141 gold badge19 silver badges35 bronze badges

answered Jun 29, 2017 at 15:54

MikeMike

1,7282 gold badges42 silver badges73 bronze badges

For anyone that also need to add into 2d associative array, you can also use answer given above, and use the code like this

 $data[$category]["test"] = $question

you can then call it (to test out the result by:

echo $data[$category]["test"];

which should print $question

answered Dec 19, 2017 at 7:34

maximranmaximran

4054 silver badges11 bronze badges

How do I add associative array to array in PHP?

Use the array_merge() Function to Add Elements at the Beginning of an Associative Array in PHP. To add elements at the beginning of an associative, we can use the array union of the array_merge() function.

How do you add to an associative array?

Normally add a new element in an existing associative array it will get appended at the end of that array..

Example: <? ... .

Syntax: array array_merge( $arr1, $arr2 ) ... .

Syntax: $arr3 = $arr1 + $arr2..

Program: PHP program to add a new item at the beginning of an associative array..

How do you sum an associative array in PHP?

The array_sum() function returns the sum of all the values in an array(one dimensional and associative). It takes an array parameter and returns the sum of all the values in it.

How do I get associative array in PHP?

Answer: Use the PHP array_values() function You can use the PHP array_values() function to get all the values of an associative array.