Questions tagged [array-combine]

A PHP function that creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.

array_combine — Creates an array by using one array for keys and another for its values

array array_combine ( array $keys , array $values )

Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.

Returns the combined array, FALSE if the number of elements for each array isn't equal.

Example:

$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
Array
(
    [green]  => avocado
    [red]    => apple
    yellow] => banana
)
47 questions
2
votes
1 answer

array_combine results only one result

I am using the following code to get two arrays into one json result. But getting only the first index as result. Is there any error in the code or anybody can suggest an alternate method to get the same result. $array1 = $_POST['array1']; $array2 =…
Jerry Jones
  • 776
  • 1
  • 13
  • 29
2
votes
9 answers

Comma-separated string to associative array with lowercase keys

I have a string like this: $string = 'Apple, Orange, Lemone'; I want to make this string to: $array = array('apple'=>'Apple', 'orang'=>'Orange', 'lemone'=>'Lemone'); How to achieve this? I used the explode function like this: $string =…
Salih K
  • 701
  • 2
  • 12
  • 31
1
vote
2 answers

array_combine() on each subarrays of two multidimensional arrays

In PHP, I have 2 multidimensional arrays. such as: array 1: [[1, 2, 3], [1, 2, 3]]; Array 2: [[4, 5, 6], [7, 8, 9]]; I need to combine these two arrays. I need the first array's subarray values to be the keys of resulting multidimensional's…
Stalin
  • 11
  • 3
1
vote
1 answer

how to combine an array with the same keyon react?

i have an output like this: [ { GolonganName: '3B - Niaga Besar', data: [ 172000 ] }, { GolonganName: '3A - Niaga Kecil', data: [ 76700 ] }, { GolonganName: '2B - Rumah Tangga Menengah', data: [ 57000 ] }, { GolonganName: 'IA - Rumah…
1
vote
2 answers

How do I combine two rows with a the same identity?

I'm trying to combine 2 rows with the same identity. I've been looking for a solution, but somehow can't find a working solution. I'm trying to make a tracker for my stocks, but I want it to combine the information if I add the same asset. I made a…
Osman P.
  • 33
  • 6
1
vote
1 answer

How do I add variable numbers of key value pairs using PHP array_combine and for-loop?

I have a db in which I store 20 demographics. The demographics come from a csv and are stored in array. Each array key is a placeholder: d1, d2, up to d20. The incoming data may be [race, gender, class] for Client A and [income, region] for Client…
David Weisser
  • 117
  • 1
  • 10
1
vote
3 answers

php: better way to split string into associative array

I have a string like this: "ALARM_ID/I4=1010001 ALARM_STATE/U4=eventcode ALARM_TEXT/A=WMR_MAP_EXPORT LOTS/A[1]=[ STEFANO ] ALARM_STATE/U1=1 WAFER/U4=1 VI_KLARF_MAP/A=/test/klarf.map KLARF_STEPID/A=StepID KLARF_DEVICEID/A=DeviceID…
Stefano Radaelli
  • 1,088
  • 2
  • 15
  • 35
1
vote
0 answers

Reading 2 TXT files and parsing the files

I am very much a numbie to python. DISPLAY should read: surname, Firstname, Day1 ,Day2,Both Days,NoDays I have 2 txt files. employee.txt has the above information. confpack has 2 lines : 1. going 2. Bonus employee.txt is like this: *(this is written…
1
vote
1 answer

make output from array with same keys from the database

I get the information from the database. For example, my database data is as follows: I get the information from the database. For example, my database data is as follows: ------------------------------------------------- p_id | description_name…
HajAli
  • 49
  • 8
1
vote
0 answers

PHP Warning: array_combine(): Both parameters should have an equal number of elements - CSV not parsing properly?

For the following CSV file (3MB): ACDF_cut.csv And for the following code:
user136649
  • 59
  • 1
  • 7
1
vote
3 answers

Return a single Element from an Associative Array from a Function

Lets assume we have two PHP-Arrays: $some_array = array('a','b','c','d','e','f'); $another_array = array(101,102,103,104,105,106); In PHP, there are already some Array-Functions that allow the construction of an Associative Array (AKA hash),…
rubber boots
  • 14,924
  • 5
  • 33
  • 44
1
vote
7 answers

Array Combine in PHP

I am trying to combine keys and values in arrays. I have an product_id with different price. Let say Product id and price id 101 and price is 100 id 105 and price is 200 id 101 and price is 300 list of product ids in array with $product_ids[]…
Vignesh Pichamani
  • 7,950
  • 22
  • 77
  • 115
0
votes
2 answers

combine 2 array with same key into 1 array

I have 2 Array: $arr1 = Array ( [2] => Array ( [0] => 41000 [1] => 31079 ) [3] => Array ( [0] => 42963 [1] => 41189 ) ) $arr2 = Array ( [2] => Array ( [0] => 40213 [1] => 42054 ) [3] => Array ( [0] => 42998…
0
votes
1 answer

Use column values from one subarray as keys for anotheer subarray's values

I have an object as below, {"metaData":[{"name":"a"},{"name":"b"}],"rows":[[1,2],[3,4],[5,6]]} I would like to change it to [["a"=>1,"b"=>2],["a"=>4,"b"=>5],["a"=>5,"b"=>6]] Is there any faster one liner that can convert this without going thru a…
Wahsei
  • 289
  • 2
  • 6
  • 16
0
votes
0 answers

I don't want to combine two obyes and have keys

I combined 2 objects as below, but I don't want the keys to be in the output I get. $link1 = '{"Success":true,"Result":[{"eventId":650810,"sportId":"2"}]}'; $link2 = '{"Success":true,"Result":[{"eventId":650811,"sportId":"2"}]}'; $a []=…
ThePro
  • 31
  • 6
1
2 3 4