我在我的$string
对象数组中,我希望动态获取每一行的所有元素数组,因为我知道我不知道元素是怎样的。进入那个字符串,现在我有 4个主菜,也许在我得到2或10之后,以及如何为每个密钥检索数据。
我的尝试:
var_dump($string) =
array(4) {
["_1514316350293_293"] => array(4) {
["customer_group"] => string(1) "3"
["condition"] => string(1) "2"
["somme"] => string(6) "500.00"
["price"] => string(4) "0.00"
}
["_1514316394524_524"] => array(4) {
["customer_group"] => string(1) "2"
["condition"] => string(1) "2"
["somme"] => string(6) "500.00"
["price"] => string(4) "0.00"
}
["_1514316395828_828"] => array(4) {
["customer_group"] => string(1) "3"
["condition"] => string(1) "1"
["somme"] => string(6) "500.00"
["price"] => string(5) "29.40"
}
["_1514316396756_756"] => array(4) {
["customer_group"] => string(1) "2"
["condition"] => string(1) "1"
["somme"] => string(6) "500.00"
["price"] => string(5) "29.40"
}
}
foreach($string as $key=>$value){
var_dump($value);
}
result=
array(4) {
["customer_group"] => string(1) "3"
["condition"] => string(1) "2"
["somme"] => string(6) "500.00"
["price"] => string(4) "0.00"
}
array(4) {
["customer_group"] => string(1) "2"
["condition"] => string(1) "2"
["somme"] => string(6) "500.00"
["price"] => string(4) "0.00"
}
array(4) {
["customer_group"] => string(1) "3"
["condition"] => string(1) "1"
["somme"] => string(6) "500.00"
["price"] => string(5) "29.40"
}
array(4) {
["customer_group"] => string(1) "2"
["condition"] => string(1) "1"
["somme"] => string(6) "500.00"
["price"] => string(5) "29.40"
}
我想念得到我需要的小小的决定
答案 0 :(得分:1)
您可以尝试array_values功能
var_dump( array_values( $string ) );
它将打印数组中的所有值
如果您想获得特定记录
$rows = array_values( $string );
var_dump( $rows[2] );
答案 1 :(得分:1)
即使 你不知道子阵列的长度,它是关联的还是数字的 ,那么你也可以获得如下所有的值: -
foreach($string as $key=>$value){
$keys = array_keys($value);
foreach($keys as $key){
echo $value[$key].PHP_EOL;
}
}
输出: - https://eval.in/925895
<强> 或者: - 强>
foreach($string as $key=>$value){
$values = array_values($value);
foreach($values as $value){
echo $value.PHP_EOL;
}
}
输出: - https://eval.in/925897
检查条件如下: -
foreach($string as $key=>$value){
$keys = array_keys($value);
if (in_array('customer_group',$keys) && $value['customer_group'] == 3 && in_array('condition',$keys) && $value['condition'] == 2 && in_array('somme',$keys) && $value['somme'] == 500.00 && in_array('price',$keys) && $value['price'] == 0.00){
echo "oh yes";
}
}
输出: - https://eval.in/925925
<强>参考: - 强>
答案 2 :(得分:0)
尝试这个
$string = [["customer_group" => "3", "condition" => "2", "somme" => "500.00", "price" => "0.00"]];
print_r($string);
$newArr = array_map(function ($v) {
return [
'customer_group' => $v['customer_group'],
'condition' => $v['condition'],
'somme' => $v['somme'],
'price' => $v['price']
];
}, $string);
print_r($newArr);