简体   繁体   中英

PHP: get value from subarray

How do I get the value from a subarray? In this case I'm interested in the locale

$objects=
Array
(
    [1397300927159026] => Array
        (
            [category] => 2
            [locale] => de_DE
        )
    [10152395537445743] => Array
        (
            [category] => 100
            [locale] => en_US
        )
)

Desired Output:

Array
(
    [1397300927159026] => "de_DE"
    [10152395537445743] => "en_US"
)

I've tried using a foreach loop to iterate through but the results are a mess -- thanks for any help.

Just a simple foreach would suffice. You just need to create it in another array. Consider this example:

$objects = array(
    '1397300927159026' => array('category' => 2, 'locale' => 'de_DE'),
    '10152395537445743' => array('category' => 100, 'locale' => 'en_US'),
);

$new_objects = array();
foreach($objects as $key => $value) {
    // $key will contain = 1397300927159026, and 1397300927159026
    // set that key into the new array as key also 
    $new_objects[$key] = $value['locale'];
}

echo '<pre>';
print_r($new_objects);
echo '</pre>';

Sample Output:

Array
(
    [1397300927159026] => de_DE
    [10152395537445743] => en_US
)

This is what array_map() is for. It loops through the array and applies a callback function to each value and returns the modified array.

$objects = array_map( function( $val ) {
    return $val['locale'];
}, $objects );

Check this CodeViper Demo

PHP

<?php
    $objects=
    Array
    (
        '1397300927159026' => Array
            (
                'category' => 2,
                'locale' => 'de_DE'
            ),
        '10152395537445743' => Array
            (
                'category' => 100,
                'locale' => 'en_US'
            )
    );
        

    $arr = array();
    foreach($objects as $key => $value) {
        $arr[$key] = $value['locale'];
    }
    
    print_r($arr);
?>

Result

Array ( [1397300927159026] => de_DE [10152395537445743] => en_US )

You can easily do it with a foreach loop and a new array .

Declare a new array say $abc

<?php

$abc = array();

//Going to loop through original array ($objects) as key and values.

foreach($objects as $key => $val ) {

    // $key will contain values 1397300927159026, 10152395537445743
    // $val['category'] contain values 2,100 
    // $val['locale'] contain values de_DE, en_US

    $abc[$key] = $val['locale'];
}

print_r($abc);
?>

Replace each subarray with its last element's value? array_map() with end() is pretty succinct ...though it requires your application to keep the element at the end of the subarrays -- so arguably less trustworthy.

Also $objects is a poor variable name choice for a multidimensional array.

Code: ( Demo )

var_export(array_map('end', $objects));

Output:

array (
  1397300927159026 => 'de_DE',
  10152395537445743 => 'en_US',
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM