0

For example, i have 2 arrays,

$array_1 = array('100','250','300','50');
$array_2 = array('a','b','c','d');

next step : i'll sort array 1 and pass 2 arrays into a function, for example the function will return values greater than 100 .

Note: Sorting based on the value is important.(descending order).

Like: array which we are passing

rsort($array_1);
$return = parse($array_1,$array_2);

echo "<pre>";
print_r($return);

function parse(array $arr1,array $arr2){

    $retArr1 = array();
    $retArr2 = array();
    foreach($arr1 as $k=>$value){
        if($value > 100){
            $retArr1[] = $value; 
            $retArr2[] = $arr2[$k];

        }
    }

    return array($retArr1,$retArr2);

}

Output is coming like

Array
(
    [0] => Array
        (
            [0] => 300
            [1] => 250
        )

    [1] => Array
        (
            [0] => a
            [1] => b
        )

)

But i want like

Array
(
    [0] => Array
        (
            [0] => 300
            [1] => 250
        )

    [1] => Array
        (
            [0] => c
            [1] => b
        )

) 
5
  • In your if, you can add something like this $retArr1[] = $value; $retArr2[] = $arr2[$k]; (don't forget to initalize these empty arrays before the loop) and at the end return array($retArr1,$retArr2); Commented Jul 18, 2016 at 11:40
  • 1
    Slightly unrelated. Doesn't this throw a fatal error? $array_1 = rsort(array('100','250','300','50'));. Array sort functions operate by reference and you can only pass in variables. Commented Jul 18, 2016 at 11:44
  • If your two arrays are related, then don't store them as two separate variables. Here, it looks like you can just use array_combine to map them together. Commented Jul 18, 2016 at 11:46
  • i made some changes Commented Jul 18, 2016 at 11:58
  • It actually looks like you want to relate the array of characters to the unsorted array of values. So you should filter first, and then rsort() the array that is returned from your parse() function. Commented Jul 18, 2016 at 12:15

4 Answers 4

1
// sort both arrays
array_multisort($array_1, SORT_DESC, $array_2);

// Take items from array while values of the 1st array > 100
$i = 0;
$res = [];
while ($i < count($array_1) and $array_1[$i] > 100) {
   $res[0][] = $array_1[$i];
   $res[1][] = $array_2[$i++];
}   
print_r($res);

demo

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

Comments

0

You can create some Array Variables within your parse Function use them inside your foreach Loop to build up the data you wish to extract from the 2 Arrays passed as Arguments to the Function. The code below illustrates how:

    <?php

        $array_1 = array('100','250','300','50');
        $array_2 = array('a','b','c','d');


        function parse(array $arr1,array $arr2) {
            $tmpArr1 = $tmpArr2 = $arrReturn = array();

            foreach ($arr1 as $k => $value) {
                if ($value > 100) {
                    $tmpArr1[] = $value;
                    $tmpArr2[] = $arr2[$k];
                }
            }
            $arrReturn['arr1'] = $tmpArr1;
            $arrReturn['arr2'] = $tmpArr2;
            return $arrReturn;
        }


        $return = parse($array_1, $array_2);
        var_dump($return);

        // DISPLAYS:            
        array (size=2)
          'arr1' => 
            array (size=2)
              0 => string '250' (length=3)
              1 => string '300' (length=3)
          'arr2' => 
            array (size=2)
              0 => string 'b' (length=1)
              1 => string 'c' (length=1)

USING ARRAYS WITH NUMERIC INDICES

    <?php

        $array_1 = array('100','250','300','50');
        $array_2 = array('a','b','c','d');


        function parse(array $arr1,array $arr2) {
            $tmpArr1 = $tmpArr2 = $arrReturn = array();

            foreach ($arr1 as $k => $value) {
                if ($value > 100) {
                    $tmpArr1[] = $value;
                    $tmpArr2[] = $arr2[$k];
                }
            }
            $arrReturn[] = $tmpArr1;
            $arrReturn[] = $tmpArr2;
            return $arrReturn;
        }

        $return = parse($array_1, $array_2);
        var_dump($return);
        // DISPLAYS:        
        array (size=2)
          0 => 
            array (size=2)
              0 => string '250' (length=3)
              1 => string '300' (length=3)
          1 => 
            array (size=2)
              0 => string 'b' (length=1)
              1 => string 'c' (length=1)

3 Comments

But first i want pass the array 1 as sorted array ie, rsort($array_1);
@alialwafaa Then sort it and pass it... it will still work
then it returning a and b
0
<?php

$array_1 = array('100','250','300','50');
$array_2 = array('a','b','c','d');

$def = array_combine($array_2, $array_1);
asort($def);
$array = array_filter($def, function($val) {
    return $val>100;
});

var_dump($array);
//array(2) {
//    ["b"]=>
//  string(3) "250"
//    ["c"]=>
//  string(3) "300"
//}

Then get keys and values.

Comments

0

Hey you did only one mistake, try below code which will definitely give your desire output:

<?php
$array_1 = array('100','250','300','50');
$array_2 = array('a','b','c','d');
arsort($array_1);  // make it correct, this was only the mistake done by you.
$return = parse($array_1,$array_2);
function parse(array $arr1,array $arr2){
     $retArr1 = array();
    $retArr2 = array();
    foreach($arr1 as $k=>$value){
        if($value > 100){
            $retArr1[] = $value; 
            $retArr2[] = $arr2[$k];

        }
    }

    return array($retArr1,$retArr2);

}

echo "<pre>";
print_r($return);

?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.