0

This is my code

        $pro_qty = '';
        $se_pro = '';

        $pro_id_nn = $this->getDataAll("SELECT session_pro_id,session_pro_qty FROM `jp_session` WHERE session_pro_id IN (".$pro_id.") AND order_status='3'");

        foreach($pro_id_nn as $pro)
        {
            $pro_qty[] = $pro['session_pro_qty'];
            $se_pro[] = $pro['session_pro_id'];

        }
        $proqty = array_combine($pro_qty,$se_pro);
        echo '<br>';
        print_r($se_pro);
        echo '<br>';
        print_r($pro_qty);
        echo '<br>';
        print_r($proqty);

OUTOUT

first array

$se_pro = Array ( [0] => 5 [1] => 1 [2] => 1 ) ;

second array

$pro_qty = Array ( [0] => 24 [1] => 24 [2] => 22 ) ;

Finally combine two array result is

$proqty = Array ( [5] => 24 [1] => 22 );

but my expecting result is

$proqty = Array ( [5] => 24 [1] => 24 [1] => 22 );

how can i get my expecting result . thanks in advance.

0

5 Answers 5

2

Your expected result is not possible, you cannot map one key (1) to two different values (24 and 22). Perhaps you should look at a different solution, such as a "jp_session" class which contains the two values, and then just store it in a list.

Sign up to request clarification or add additional context in comments.

Comments

0

simple solution

foreach($pro_id_nn as $pro)
    {
        $pro_qty[$pro['session_pro_id']][] = $pro['session_pro_qty'];
        }

Comments

0

Try this one

<?php
$se_pro = Array ( 0 => 5, 1 => 1, 2 => 1 ) ;
$pro_qty = Array ( 0 => 24, 1 => 24, 2 => 22 ) ;
$a=sizeof($se_pro);
for($i=0;$i<$a;$i++)
{
  $b=$se_pro[$i];
  $c=$pro_qty[$i];
  $temp[$b]=$c;
  $i++;
}
print_r($temp);
?>

But one condition '$se_pro' values not repeat and both array are same size

1 Comment

yes, working but $se_pro value 1 repeating and third time loop run [1] =>'' over writes with another value. But one condition '$se_pro' values not repeat and both array are same size. The above condition satisfy then you will get you expected output
0

in array_combine() If two keys are the same, the second one prevails..

you can get the result like -

Array
(
    [24] => Array
        (
            [0] => 5
            [1] => 1
        )

    [22] => 3
)

Comments

0

the other way can be

$keys   = array ( '24', '24', '22' );
$values = array ( '5', '1', '1' );
$output = array();

$size = sizeof($keys);
for ( $i = 0; $i < $size; $i++ ) {
    if ( !isset($output[$keys[$i]]) ) {
        $output[$keys[$i]] = array();
    }
    $output[$keys[$i]][] = $values[$i];
}

this will give the output like -

Array ( [24] => Array ( [0] => 5 [1] => 1 ) [22] => Array ( [0] => 1 ) )

or you can use

<?php

    $keys   = array ( '24', '24', '22' );
    $values = array ( '5', '1', '1' );
function foo($key, $val) {
   return array($key=>$val);
}

$arrResult = array_map('foo', $keys, $values);

print_r($arrResult);

?>

depending upon which output is more suitable for you to work with.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.