Ciprian
Ciprian

Reputation: 3226

Get a column value from the qualifying row of a 2d array

Let's say that one of my users resides in the US, and their country_id is 1. Can I get the ship_cost based on the user's country_id?

Array
(
    [0] => Array
        (
            [ship_cost] => 1
            [country_id] => 1
            [country_code] => US
            [country_name] => United States
        )

    [1] => Array
        (
            [ship_cost] => 0
            [country_id] => 0
            [country_code] => 
            [country_name] => 
        )

)

The html where I need to also include the SHIP PRICE:

<?php if( in_array($user_country_id, $shipArr) && $itemData[0]['user_id'] != $user_id ) { ?>
    <ul class="list-group margin-top-3">
        <li class="list-group-item">
            <span class="badge">SHIP PRICE</span>
            Ships to <?php echo $itemTools->getLocationName($user_country_id); ?>
        </li>
    </ul>
<?php } ?>

Upvotes: 2

Views: 64

Answers (4)

mickmackusa
mickmackusa

Reputation: 47894

From PHP8.4, use array_find() to return the first qualifying row, then directly access the ship_cost. Null coalesce the element access attempt to whatever default value you like. Demo

$user_country_id = 1;
var_export(
    array_find(
        $shipArr,
        fn($row) => $row['country_id'] == $user_country_id
    )['ship_cost'] ?? 0
);

This is more ideal than array_search() (which is also designed for high performance), because array_find() returns the qualifying row instead of the key of the qualifying row.

Upvotes: 1

Akshay Hegde
Akshay Hegde

Reputation: 16997

Try

$key = array_search(1, array_column($array,"country_id"));
if($key !== false)
{
    print "ship_cost : ".$array[$key]['ship_cost'];
}else
{
    print "ship_cost : unknown or not exists";
}

Sample Test

[akshay@localhost tmp]$ cat test.php
<?php

function test($id, $array)
{
    $key = array_search($id, array_column($array,"country_id"));
    if($key !== false)
    {
        print "ship_cost : ".$array[$key]['ship_cost']."\n";
    }else
    {
        print "ship_cost : unknown or not exists\n";
    }
}

$array = array (
  array (
    'ship_cost' => '1',
    'country_id' => '1',
    'country_code' => 'US',
    'country_name' => 'United States',
  ),
  array (
    'ship_cost' => '0',
    'country_id' => '0',
    'country_code' => '',
    'country_name' => false,
  )
);

// Test1
test(1, $array);

// Test2
test(2, $array);
?>

Output

[akshay@localhost tmp]$ php test.php
ship_cost : 1
ship_cost : unknown or not exists

Upvotes: 1

Meenesh Jain
Meenesh Jain

Reputation: 2528

See the new changes here

$arr = array
    (
        "0" => array
            (
                "ship_cost" => 1,
                "country_id" => 1,
                "country_code" => "US",
                "country_name" => "United States",
            ),

        "1" => array
            (
                "ship_cost" => 0,
                "country_id" => 0,
                "country_code" => "",
                "country_name" => "",
            )
    );

Now Processing Your Array

 $user_coutry_id = 1;
 $ship_cost ="";
 foreach ($arr as $key =>  $value) {
    if($arr[$key]["country_id"]==$user_coutry_id){
         $ship_cost = $arr[$key]["ship_cost"];
    }
 } 
 echo $ship_cost;
// return 1 as you expect. 

Upvotes: 0

Vignesh Bala
Vignesh Bala

Reputation: 919

Try this one it will help you

<?php 
        $user_country_id = 1;
            $array = array(array('ship_cost' => 1,'country_id' => 1,'country_code' => 'US','country_name' => 'United States'),
                array('ship_cost' => 0,'country_id' => 0,'country_code' => 'Uk','country_name' => 'United Kingdom'));
            foreach ($array as $value) {
                if ($user_country_id == $value['country_id']) {
                    var_dump($value);
                }
            }

Change this code for what you expect

Upvotes: 1

Related Questions