PHP数组索引未定义,虽然它存在

Given an array as follow :

$z = [
 "cat lovers" => [
     "name" => "cat lovers",
     "impressions" => 2038,
     "ctr" => 0.032875368007851,
     "actions" => [
       [
         "action_type" => "attention_event",
         "value" => "232",
       ],
       [
         "action_type" => "landing_page_view",
         "value" => "18",
       ],
     ],
 "shorty kodi" => [
     "name" => "shorty kodi",
     "impressions" => 534,
     "ctr" => 0.041198501872659,
     "actions" => [
       [
         "action_type" => "attention_event",
         "value" => "56",
       ],
       [
         "action_type" => "landing_page_view",
         "value" => "7",
       ]
     ]
]

Following code run with no error :

foreach($z as $i) {
    print_r(array_column($i["actions"], "action_type"));
}

But if we remove the print_r function to like :

foreach($z as $i) {
   $b = array_column($i["actions"], "action_type");
}

It results an error saying :

PHP error:  Undefined index: actions on line 2

Any idea why?

Thanks

It's working fine, I just checked it. put print_r($b); inside or after foreach. You will get the same result as you're getting from the first foreach.

The provided array has a syntax error.

Try this:

$z = [
 "cat lovers" => [
     "name" => "cat lovers",
     "impressions" => 2038,
     "ctr" => 0.032875368007851,
     "actions" => [
       [
         "action_type" => "attention_event",
         "value" => "232",
       ],
       [
         "action_type" => "landing_page_view",
         "value" => "18",
       ],
     ]
  ],
 "shorty kodi" => [
     "name" => "shorty kodi",
     "impressions" => 534,
     "ctr" => 0.041198501872659,
     "actions" => [
       [
         "action_type" => "attention_event",
         "value" => "56",
       ],
       [
         "action_type" => "landing_page_view",
         "value" => "7",
       ]
     ]
  ]
];

foreach($z as $i) {
   $b = array_column($i["actions"], "action_type");
}

I dont know what went wrong but i coped and pasted your code and it is working fine.

Here is what I did.

$z = [
"cat lovers" => [
    "name" => "cat lovers",
    "impressions" => 2038,
    "ctr" => 0.032875368007851,
    "actions" => [
        [
            "action_type" => "attention_event",
            "value" => "232",
        ],
        [
            "action_type" => "landing_page_view",
            "value" => "18",
        ],
    ],
    "shorty kodi" => [
        "name" => "shorty kodi",
        "impressions" => 534,
        "ctr" => 0.041198501872659,
        "actions" => [
            [
                "action_type" => "attention_event",
                "value" => "56",
            ],
            [
                "action_type" => "landing_page_view",
                "value" => "7",
            ]
        ]
    ]
]];
$b = "";
foreach($z as $i) {
    $b=array_column($i["actions"], "action_type");
}
print_r($b);

Just change your code like that :

<?php 
$z = [
    "cat lovers" => [
        "name" => "cat lovers",
        "impressions" => 2038,
        "ctr" => 0.032875368007851,
        "actions" => [
            [
                "action_type" => "attention_event",
                "value" => "232",
            ],
            [
                "action_type" => "landing_page_view",
                "value" => "18",
            ],
        ],
    ],
    "shorty kodi" => [
     "name" => "shorty kodi",
     "impressions" => 534,
     "ctr" => 0.041198501872659,
     "actions" => [
       [
         "action_type" => "attention_event",
         "value" => "56",
       ],
       [
         "action_type" => "landing_page_view",
         "value" => "7",
       ]
     ]
]
];
echo '<pre>';
$b = [];
foreach($z as $i) {
   $b[] = array_column($i["actions"], "action_type");
}
print_r($b);

?>

That sit!

Your array has a syntax issue. Try this fixed array,

$z = [
 "cat lovers" => [
 "name" => "cat lovers",
 "impressions" => 2038,
 "ctr" => 0.032875368007851,
 "actions" => [
   [
     "action_type" => "attention_event",
     "value" => "232",
   ],
   [
     "action_type" => "landing_page_view",
     "value" => "18",
   ],
  ]
 ],
 "shorty kodi" => [
 "name" => "shorty kodi",
 "impressions" => 534,
 "ctr" => 0.041198501872659,
 "actions" => [
   [
     "action_type" => "attention_event",
     "value" => "56",
   ],
   [
     "action_type" => "landing_page_view",
     "value" => "7",
   ]
  ]
 ]
];

Sorry, I found the cause to be simple. The array actually quite long when I tested it, and in one of the element, there is no "action" index, all the rest has!

That's why when loop through each, it will fail on that one.

Thank you for all the feedbacks.