如何从php中的多维数组中获取特定值

时间:2014-08-19 11:26:26

标签: php arrays

我使用print_r语句

显示数组
Array (
    [0] => Array (
        [0] => stdClass Object (
            [id] => 1
            [name] => Price
            [value] => 18,000
            [type] => textfield
            [group] => 1
            [published] => 1
            [ordering] => 1
            [alias] => Price
        )
        [1] => stdClass Object (
            [id] => 2
            [name] => Brand
            [value] => Nokia
            [type] => select
            [group] => 1
            [published] => 1
            [ordering] => 2
            [alias] => Brand
        )
        [2] => stdClass Object (
            [id] => 3
            [name] => Mobile Model
            [value] => X-plus
            [type] => textfield
            [group] => 1
            [published] => 1
            [ordering] => 3
            [alias] => MobileModel
        )
        [3] => stdClass Object (
            [id] => 4
            [name] => Contact Name
            [value] => XYZ
            [type] => textfield
            [group] => 1
            [published] => 1
            [ordering] => 4
            [alias] => ContactName
        )
        [4] => stdClass Object (
            [id] => 5
            [name] => Email
            [value] => XYZ@gmail.com
            [type] => textfield
            [group] => 1
            [published] => 1
            [ordering] => 5
            [alias] => Email
        )
        [5] => stdClass Object (
            [id] => 6
            [name] => Contact Number
            [value] => 03328914989
            [type] => textfield
            [group] => 1
            [published] => 1
            [ordering] => 6
            [alias] => ContactNumber
            )
        [6] => stdClass Object (
            [id] => 7
            [name] => Province
            [value] => KPK
            [type] => select
            [group] => 1
            [published] => 1
            [ordering] => 7
            [alias] => Province
        )
        [7] => stdClass Object (
            [id] => 8
            [name] => City
            [value] => Peshawar
            [type] => textfield
            [group] => 1
            [published] => 1
            [ordering] => 8
            [alias] => City
        )
    )
) 

我想只获得品牌的价值。实现它的适当方法是什么?

5 个答案:

答案 0 :(得分:1)

在输出中添加一些空格,以便首先真正理解结构。

Array ( 
 [0] => Array (
   [0] => stdClass Object (
       [id] => 1 [name] => Price [value] => 18,000 [type] => textfield [group] => 1 [published] => 1 [ordering] => 1 [alias] => Price 
   )
   [1] => stdClass Object (
       [id] => 2 [name] => Brand [value] => Nokia [type] => select [group] => 1 [published] => 1 [ordering] => 2 [alias] => Brand 
   )
   [2] => stdClass Object ( 
      [id] => 3 [name] => Mobile Model [value] => X-plus [type] => textfield [group] => 1 [published] => 1 [ordering] => 3 [alias] => MobileModel
   )

让我们调用主数组$arr。我们注意到的第一件事是它出于某种原因在另一个数组中,因此所有内容都在$arr[0]中。如果您知道品牌将保留在索引[2],您只需使用以下内容:

$brand = $arr[0][2]->value; // Assigns "Nokia" to $brand

否则,我们总是可以循环遍历您拥有的数组($arr),并找到名称为 Brand 的人。像这样:

$brand = '';
foreach ($arr[0] as $obj) {
    if ($obj->name == 'Brand') {
        $brand = $obj->value;
        break;
    }
}

答案 1 :(得分:0)

您必须指定要获得的表的索引:

  echo print_r($table[1]);

答案 2 :(得分:0)

试试这个

<?php 
foreach($your_array[0] as $arr)
{
     echo $arr->name;
}
?>

答案 3 :(得分:0)

您正在将数组与对象结合使用。 您可以访问&#34;品牌&#34;这种方式适用于包含此构造的变量$ a:

$brand = FALSE;
foreach ($a as $a1) {
    if (is_array($a1)) {
        foreach ($a1 as $a2) {
            if (is_object($a2) && $a2->name == 'Brand') {
                $brand = $a2;
                break;
            }
        }
     }
 }

变量$ brand将包含生成的对象,其名称为&#34; Brand&#34;如果没有&#34;品牌&#34;已被发现。您可以对此进行增强,以便与&#34;别名&#34;。

进行比较

答案 4 :(得分:0)

将数组包装到变量

$records = array(
array(
    'id' => 2135,
    'first_name' => 'John',
    'last_name' => 'Doe',
),
array(
    'id' => 3245,
    'first_name' => 'Sally',
    'last_name' => 'Smith',
),
array(
    'id' => 5342,
    'first_name' => 'Jane',
    'last_name' => 'Jones',
),
array(
    'id' => 5623,
    'first_name' => 'Peter',
    'last_name' => 'Doe',
)

通过这种方式,您可以调用特定属性(在您的情况下)

$brand = array_column($records, 'Brand');
print_r($brand);