繁体   English   中英

如何在 PHP 中构建多维数组

[英]How to build a Multidimensional array in PHP

我正在尝试使用我正在切割的字符串数组在 PHP 中构建一个多维数组,因此 1:wlrb@yahoo.com:7:8.35 的字符串变为

"id": "1",  
"email_address": "wlrb@yahoo.com",  
"domain": "yahoo.com",  
"number_of_orders": "7",    
"total_order_value": "£8.35"

在 JavaScript 中,我会创建一个包含上述值的对象并将其放入一个数组中,但 PHP 中的等价物是什么? 到目前为止,我有以下代码,它给了我

数组([0] => stdClass 对象([id] => 1 [email] => wlrb@yahoo.com [domain] => yahoo.com [number_of_orders] => 7 [total_order_value] => 8.35)

<?php

$data = file_get_contents('orderdata');
/* echo $data; */

$lines = explode("\n", $data);

array_splice($lines, 0, 8);
/* var_dump($lines); */

array_splice($lines, -3);
/* var_dump($lines); */

/* removes all NULL, FALSE and Empty Strings but leaves 0 (zero) values */
$lines = array_values(array_filter($lines, 'strlen'));


function arraySort($lines ,$i) {
    $rep = new stdClass();
    $rep->id = strId($lines, $i);
    $rep->email = strEmail($lines, $i);
    $rep->domain = strDomain($lines, $i);
    $rep->number_of_orders = orderNo($lines, $i);
    $rep->total_order_value = orderValue($lines, $i);
    /* var_dump($rep); */
    return $rep;
}


function strDomain($lines, $i) {
    if($lines[$i] == null){
        return "";
    } 
    else {
        $str = $lines[$i];
        $splt = explode(':', $str);
        $domain = explode('@', $splt[1]);
        return $domain[1];
    }
}


function strId($lines, $i) {
    if($lines[$i] == null){
        return "";
    } 
    else {
        $str = $lines[$i];
        $splt = explode(':', $str);
        return $splt[0];
    }

}


function strEmail($lines, $i) {
    if($lines[$i] == null){
        return "";
    } 
    else {
        $str = $lines[$i];
        $splt = explode(':', $str);
        return $splt[1];
    }
}


function orderNo($lines, $i) {
    if($lines[$i] == null){
        return "";
    } 
    else {
        $str = $lines[$i];
        $splt = explode(':', $str);
        return $splt[2];
    }
}


function orderValue($lines, $i) {
    if($lines[$i] == null){
        return "";
    } 
    else {
        $str = $lines[$i];
        $splt = explode(':', $str);
        return '£' + $splt[3];
    }
}

$reports = array();
$reps = array();
for($i = 0, $length = count($lines); $i < $length; ++$i) {
    $reps = arraySort($lines, $i);
    array_push($reports, $reps);
}

?>

但是当我尝试搜索数组时

$filteredArray = 
array_filter($reports, function($element) use($search){
  return isset($element['domain']) && $element['domain'] == $search;
});

我收到以下错误

致命错误:未捕获错误:无法在 phpData.php:110 中使用 stdClass 类型的对象作为数组堆栈跟踪:#0 [内部函数]:{closure}(Object(stdClass)) #1 phpData.php(111):array_filter( Array, Object(Closure)) #2 {main} 在第 110 行的 phpData.php 中抛出

这是因为使用了$rep = new stdClass(); 在我的 arraySort 函数中? 如果是这样,我应该使用什么?

最简单和最短的解决方案是:

$value = "1:wlrb@yahoo.com:7:8.35";
$keys = array('id', 'email_address', 'number_of_orders', 'total_order_value');
$fused = array_combine($keys, explode(':', $value));
$fused['domain'] = strDomain($fused['email_address']); //using your "strDomain()" function

它会给你你想要的数组,除非你的订单价值中没有£符号。

您可以像这样使用对象获取方法:

$filteredArray = 
array_filter($reports, function($element) use($search){
  return isset($element->domain) && $element->domain == $search;
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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