0

I have two arrays like this

array 1

array(3) { [0]=> int(14) [1]=> int(16) [2]=> int(17) } 

array 2

array(3) { [0]=> float(0.46902846738366) [1]=> float(0.40289063077504) [2]=> float(0.54903658244928) } 

array 1 is an array that contains the database table id on the value of the associated array.

14, 16, 17 is the id of my database tables.

array 2 is an array that contains the results of mathematical operations that I've done. I have an array is a dynamic array.

so I want to

id 14 has a value of 0.46902846738366

,

id 16 has a value of 0.40289063077504

, and

id 17 has a value of 0.54903658244928

. then each id is stored in each of the variables themselves.

how to combine the two arrays?? thank you!

  • Bayu, you need something like this array(3) { [0]=> array(2) { [0]=> int(14) [1]=> float(0.343434) } [1]=> array(2) { [0]=> int(16) [1]=> float(0.123454) } [2]=> array(2) { [0]=> int(17) [1]=> float(0.3454345) } } ? – Mohammadhzp May 22 '15 at 15:41
  • @Mohammadhzp yes, I want that value in combination with id. Can I access data in my database tables using that array? and whether it could be any array keys are stored in separate variables? for example for id 14 $ a, $ b for id 17 ...................... – Bayu Angga Satrio May 22 '15 at 16:22
  • @rizier123 : This is not a duplicate,it's better to open question again so people answer this – Mohammadhzp May 22 '15 at 16:24
  • @Mohammadhzp Well you guessed right, but OP didn't specified *how* he wants to combine both arrays. So you also could have guessed incorrect. – Rizier123 May 22 '15 at 16:27

3 Answers3

1

From your question to combine arrays you can use array_combine to zip array together

<?php
$array_a = [14,16,17];
$array_b = [0.33333, 0.6434, 0.123456];
$zip_array = array_combine($array_a, $array_b);

This will answer your question, as for your comment, if you want to get ids as variable with value you can do this:

extract($zip_array, EXTR_PREFIX_ALL, 'id'); // extract all variable and prefix them with id_
print_r($zip_array);
echo $id_14; // the same as $zip_array[14];

Here I added "id" as a prefix to variables since a variable in PHP can't be a number, you can use any prefix you want

update: as for @u_mulder mentioned you can use array_map() too,better practice would be

$zip_array = array_map(NULL, $array_a, $array_b);

but in this case you can't extract values correctly

Mohammadhzp
  • 498
  • 7
  • 20
  • the next question is how do I access information from the database using the id? for example I have a table "storage" that has id 14, 16, 17.? besides, id which has a value before I would do mathematical operations to calculate the id which has the highest value use the function max (); Can you teach me algorithms? thanks again :-) @Mohammadhzp – Bayu Angga Satrio May 22 '15 at 17:10
  • your welcome, for your other question it's better to ask a new question and leave this question clear for someone else – Mohammadhzp May 22 '15 at 17:13
0

If you want to be one array for your keys, and another - for values, than you can use array_combine:

$result = array_combine(
    array(14, 16, 17), 
    array(0.46902846738366, 0.40289063077504, 0.54903658244928)
);

If you want id and value as elements of subarray, then you can use array_map:

$ids = array(14,16,17);
$vals = array(0.46902846738366, 0.40289063077504, 0.54903658244928);

$result = array_map(
    function($id, $val) {
        return array($id, $val);
    },
    $ids,
    $vals
);
u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

Are you looking for something like this? Assumes both arrays will be of same size. You can change int,float if you dont want to force, and want a string array instead.

<?php
$array1 = array('1','24','98');
$array2 = array('0.8778','1.764646','6.9488499');

$result = array();
for($i=0;$i<sizeof($array1);$i++){
$result[] =array('0' =>(int)$array1[$i], '1' => (float)$array2[$i]);
}
var_dump($result);

Output

array(3) {
  [0]=>
  array(2) {
    [0]=>
    int(1)
    [1]=>
    float(0.8778)
  }
  [1]=>
  array(2) {
    [0]=>
    int(24)
    [1]=>
    float(1.764646)
  }
  [2]=>
  array(2) {
    [0]=>
    int(98)
    [1]=>
    float(6.9488499)
  }
}

Better practice would be to use the php functions, as mentioned in other answers. This, is a simple way instead.

Kishor
  • 1,513
  • 2
  • 15
  • 25