Extracting values from PHP array with IDs that change

Viewed 28

I am trying to parse values from an array using PHP. When I dump the output this is what I am seeing. In the case of [25] these are automatic IDs being generated from the application which I cannot change. How would I extract the value for "product" and "customer_name". Any suggestions how to get the values when there is an ever changing id being generated?

Array ( [0] => Array ( [25] => Array ( [product] => ABC product [customer_name] => jemmyzre testermaner  [customer_email] => mtesterman@aol.comz ) [26] => Array ( [product] => CDE Product [customer_name] => jim smitherstein [customer_email] => jimsither@ewz.com ) [] => ) )

1 Answers

The documentation to PHPs foreach will give you access to the key and the value: https://www.php.net/manual/en/control-structures.foreach.php

<?php
$your_array = ...; // the array you posted
foreach($your_array[0] as $key => $value) {
  echo $key; // first iteration would output 25, second will output 26
  echo $value; // this will be the array containing the elements 'product', 'customer_name' and 'customer_email'
}

Please see that these arrays are wrapped inside an array, that is why I used $your_array[0] in the foreach

Related