I am trying to build a multidimensional array in PHP using an array of string that I am cutting up so the string of 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 into an array but what is the equivalent in PHP? So far I have the below code 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 get the following error
Fatal error: Uncaught Error: Cannot use object of type stdClass as array in phpData.php:110 Stack trace: #0 [internal function]: {closure}(Object(stdClass)) #1 phpData.php(111): array_filter(Array, Object(Closure)) #2 {main} thrown in phpData.php on line 110
Is this because of the use of $rep = new stdClass();
in my arraySort function? If so what should I be using?
The easiest and shortest solution would be :
$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 you won't have the £
sign in your order value.