How to build a multidimensional array in PHP

I am trying to build a multidimensional array in PHP using a string array that I am slitting, so line 1: wlrb@yahoo.com : 7: 8.35 becomes

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

      

In JavaScript, I would create an object containing the above values ​​and put it in an array, but what is the equivalent in PHP? So far I have the code below which gives me

Array ([0] => stdClass Object ([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);
}

?>

      

but when i try to search the array with

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

      

I am getting the following error

Fatal error: impure error: cannot use object of type stdClass as an array in phpData.php: 110 Stack trace: # 0 [internal function]: {close} (object (stdClass)) # 1 phpData.php (111): array_filter (array , Object (Closure)) # 2 {main} Thrown in phpData.php on line 110

Is this due to the use of $rep = new stdClass();

arraySort in my function? If so, which should I use?

+3


source to share


1 answer


Simplest and shortest solution:

$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

      



It will give you the array you want, except that there is no sign in your order Β£

.

+1


source







All Articles