Как заменить указанное значение c во вложенном массиве в PHP? - PullRequest
0 голосов
/ 27 апреля 2020

У меня есть такой массив:

array(10) { [0]=> array(21) { ["id"]=> string(3) "213" ["form_id"]=> string(2) "11" ["post_id"]=> NULL ["date_created"]=> string(19) "2020-04-20 15:50:16" ["date_updated"]=> string(19) "2020-04-20 15:50:16" ["is_starred"]=> string(1) "0" ["is_read"]=> string(1) "0" ["ip"]=> string(13) "188.211.58.78" ["source_url"]=> string(36) "https://www.torabi-md.ir/attendance/" ["user_agent"]=> string(129) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" ["currency"]=> string(3) "IRR" ["payment_status"]=> bool(false) ["payment_date"]=> NULL ["payment_amount"]=> NULL ["payment_method"]=> NULL ["transaction_id"]=> string(10) "3875200674" ["is_fulfilled"]=> NULL ["created_by"]=> NULL ["transaction_type"]=> NULL ["status"]=> string(6) "active" [1]=> string(8) "ورود" } [1]=> array(21) { ["id"]=> string(3) "214" ["form_id"]=> string(2) "11" ["post_id"]=> NULL ["date_created"]=> string(19) "2020-04-20 16:08:40" ["date_updated"]=> string(19) "2020-04-20 16:08:40" ["is_starred"]=> string(1) "0" ["is_read"]=> string(1) "0" ["ip"]=> string(13) "188.211.58.78" ["source_url"]=> string(36) "https://www.torabi-md.ir/attendance/" ["user_agent"]=> string(129) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" ["currency"]=> string(3) "IRR" ["payment_status"]=> bool(false) ["payment_date"]=> NULL ["payment_amount"]=> NULL ["payment_method"]=> NULL ["transaction_id"]=> string(10) "8137836043" ["is_fulfilled"]=> NULL ["created_by"]=> NULL ["transaction_type"]=> NULL ["status"]=> string(6) "active" [1]=> string(8) "خروج" } [2]=> array(21) { ["id"]=> string(3) "215" ["form_id"]=> string(2) "11" ["post_id"]=> NULL ["date_created"]=> string(19) "2020-04-21 11:35:02" ["date_updated"]=> string(19) "2020-04-21 11:35:02" ["is_starred"]=> string(1) "0" ["is_read"]=> string(1) "0" ["ip"]=> string(13) "188.211.58.78" ["source_url"]=> string(36) "https://www.torabi-md.ir/attendance/" ["user_agent"]=> string(129) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" ["currency"]=> string(3) "IRR" ["payment_status"]=> bool(false) ["payment_date"]=> NULL ["payment_amount"]=> NULL ["payment_method"]=> NULL ["transaction_id"]=> string(10) "5580962715" ["is_fulfilled"]=> NULL ["created_by"]=> NULL ["transaction_type"]=> NULL ["status"]=> string(6) "active" [1]=> string(8) "ورود" } [3]=> array(21) { ["id"]=> string(3) "216" ["form_id"]=> string(2) "11" ["post_id"]=> NULL ["date_created"]=> string(19) "2020-04-21 16:27:10" ["date_updated"]=> string(19) "2020-04-21 16:27:10" ["is_starred"]=> string(1) "0" ["is_read"]=> string(1) "0" ["ip"]=> string(13) "188.211.58.78" ["source_url"]=> string(36) "https://www.torabi-md.ir/attendance/" ["user_agent"]=> string(129) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" ["currency"]=> string(3) "IRR" ["payment_status"]=> bool(false) ["payment_date"]=> NULL ["payment_amount"]=> NULL ["payment_method"]=> NULL ["transaction_id"]=> string(10) "7161122321" ["is_fulfilled"]=> NULL ["created_by"]=> NULL ["transaction_type"]=> NULL ["status"]=> string(6) "active" [1]=> string(8) "خروج" } [4]=> array(21) { ["id"]=> string(3) "217" ["form_id"]=> string(2) "11" ["post_id"]=> NULL ["date_created"]=> string(19) "2020-04-22 11:38:14" ["date_updated"]=> string(19) "2020-04-22 11:38:14" ["is_starred"]=> string(1) "0" ["is_read"]=> string(1) "0" ["ip"]=> string(14) "188.211.42.159" ["source_url"]=> string(36) "https://www.torabi-md.ir/attendance/" ["user_agent"]=> string(129) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" ["currency"]=> string(3) "IRR" ["payment_status"]=> bool(false) ["payment_date"]=> NULL ["payment_amount"]=> NULL ["payment_method"]=> NULL ["transaction_id"]=> string(10) "3069574022" ["is_fulfilled"]=> NULL ["created_by"]=> NULL ["transaction_type"]=> NULL ["status"]=> string(6) "active" [1]=> string(8) "ورود" } [5]=> array(21) { ["id"]=> string(3) "218" ["form_id"]=> string(2) "11" ["post_id"]=> NULL ["date_created"]=> string(19) "2020-04-22 16:22:25" ["date_updated"]=> string(19) "2020-04-22 16:22:25" ["is_starred"]=> string(1) "0" ["is_read"]=> string(1) "0" ["ip"]=> string(14) "188.211.42.159" ["source_url"]=> string(36) "https://www.torabi-md.ir/attendance/" ["user_agent"]=> string(129) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" ["currency"]=> string(3) "IRR" ["payment_status"]=> bool(false) ["payment_date"]=> NULL ["payment_amount"]=> NULL ["payment_method"]=> NULL ["transaction_id"]=> string(10) "4201845766" ["is_fulfilled"]=> NULL ["created_by"]=> NULL ["transaction_type"]=> NULL ["status"]=> string(6) "active" [1]=> string(8) "خروج" } [6]=> array(21) { ["id"]=> string(3) "219" ["form_id"]=> string(2) "11" ["post_id"]=> NULL ["date_created"]=> string(19) "2020-04-25 11:30:12" ["date_updated"]=> string(19) "2020-04-25 11:30:12" ["is_starred"]=> string(1) "0" ["is_read"]=> string(1) "0" ["ip"]=> string(14) "188.211.42.159" ["source_url"]=> string(36) "https://www.torabi-md.ir/attendance/" ["user_agent"]=> string(129) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" ["currency"]=> string(3) "IRR" ["payment_status"]=> bool(false) ["payment_date"]=> NULL ["payment_amount"]=> NULL ["payment_method"]=> NULL ["transaction_id"]=> string(10) "4789618480" ["is_fulfilled"]=> NULL ["created_by"]=> NULL ["transaction_type"]=> NULL ["status"]=> string(6) "active" [1]=> string(8) "ورود" } [7]=> array(21) { ["id"]=> string(3) "220" ["form_id"]=> string(2) "11" ["post_id"]=> NULL ["date_created"]=> string(19) "2020-04-26 11:28:03" ["date_updated"]=> string(19) "2020-04-26 11:28:03" ["is_starred"]=> string(1) "0" ["is_read"]=> string(1) "0" ["ip"]=> string(13) "5.238.225.138" ["source_url"]=> string(36) "https://www.torabi-md.ir/attendance/" ["user_agent"]=> string(129) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" ["currency"]=> string(3) "IRR" ["payment_status"]=> bool(false) ["payment_date"]=> NULL ["payment_amount"]=> NULL ["payment_method"]=> NULL ["transaction_id"]=> string(10) "0141411521" ["is_fulfilled"]=> NULL ["created_by"]=> NULL ["transaction_type"]=> NULL ["status"]=> string(6) "active" [1]=> string(8) "ورود" } [8]=> array(21) { ["id"]=> string(3) "221" ["form_id"]=> string(2) "11" ["post_id"]=> NULL ["date_created"]=> string(19) "2020-04-26 16:30:07" ["date_updated"]=> string(19) "2020-04-26 16:30:07" ["is_starred"]=> string(1) "0" ["is_read"]=> string(1) "0" ["ip"]=> string(13) "5.238.225.138" ["source_url"]=> string(36) "https://www.torabi-md.ir/attendance/" ["user_agent"]=> string(129) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" ["currency"]=> string(3) "IRR" ["payment_status"]=> bool(false) ["payment_date"]=> NULL ["payment_amount"]=> NULL ["payment_method"]=> NULL ["transaction_id"]=> string(10) "5123886653" ["is_fulfilled"]=> NULL ["created_by"]=> NULL ["transaction_type"]=> NULL ["status"]=> string(6) "active" [1]=> string(8) "خروج" } [9]=> array(21) { ["id"]=> string(3) "222" ["form_id"]=> string(2) "11" ["post_id"]=> NULL ["date_created"]=> string(19) "2020-04-27 11:44:57" ["date_updated"]=> string(19) "2020-04-27 11:44:57" ["is_starred"]=> string(1) "0" ["is_read"]=> string(1) "0" ["ip"]=> string(13) "5.238.254.231" ["source_url"]=> string(36) "https://www.torabi-md.ir/attendance/" ["user_agent"]=> string(129) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763" ["currency"]=> string(3) "IRR" ["payment_status"]=> bool(false) ["payment_date"]=> NULL ["payment_amount"]=> NULL ["payment_method"]=> NULL ["transaction_id"]=> string(10) "9130531791" ["is_fulfilled"]=> NULL ["created_by"]=> NULL ["transaction_type"]=> NULL ["status"]=> string(6) "active" [1]=> string(8) "ورود" } } 

Я хочу извлечь другой массив из этого массива с другой схемой, подобной этой:

Array ( [0] => Array ( [2020-04-20] => Array ( [0] => 15:50:16 [1] => 16:08:40) ) ..... )

Я пробовал этот код в API WordPress с гравитационной формой:

$new_array = array();
$search_criteria = array(
'status'        => 'active'
);
$sorting = array( 'key' => 'date_updated', 'direction' => 'ASC' );
$entries = GFAPI::get_entries( 11,$search_criteria,$sorting);
var_dump ($entries);
echo "<hr>";

foreach ($entries as $entry) {
if ($entry [1]=="ورود"){
    $date = explode (" ", $entry ["date_updated"]) [0];
    $start_time = explode (" ", $entry ["date_updated"]) [1];
    //echo $date."===>".$time."<br>";
    array_push($new_array,array($date => array ($start_time,"empty")));
}
}
$x="0";
foreach ($entries as $entry) {
if ($entry [1]=="خروج"){
    $date = explode (" ", $entry ["date_updated"]) [0];
    $end_time = explode (" ", $entry ["date_updated"]) [1];
    $new_array [$x][$date][1] = $end_time;
    $x=$x+1;
}
}

print_r($new_array);

После запуска кода у меня получаются следующие результаты:

Array ( [0] => Array ( [2020-04-20] => Array ( [0] => 15:50:16 [1] => 16:08:40 ) ) [1] => Array ( [2020-04-21] => Array ( [0] => 11:35:02 [1] => 16:27:10 ) ) [2] => Array ( [2020-04-22] => Array ( [0] => 11:38:14 [1] => 16:22:25 ) ) [3] => Array ( [2020-04-25] => Array ( [0] => 11:30:12 [1] => empty ) **[2020-04-26]** => Array ( [1] => 16:30:07 ) ) [4] => Array ( [2020-04-26] => Array ( [0] => 11:28:03 [1] => empty ) ) [5] => Array ( [2020-04-27] => Array ( [0] => 11:44:57 [1] => empty ) ) ) 

Мой код не работает в настоящее время, когда в исходном массиве пропущено значение , Обратите внимание на [2020-04-25]: خروج нет записи. и это правильно. Но после этого у меня неверные результаты.

...