John Davidson

laravel - Why is Variable Value Assigned with Null in this PHP array?

0 comments
Message:


   /**
*
* @param string $key
* @param string $val
*/
public function createAdminTask($key, $val)
{
$request_body = [
"order_number" => $val,
"order_id" => null,
"prescription_id" => null,
"payer_id" => null,
"card_id" => null,
"prescription_key" => null,
];

dd($val,$request_body); }

I have this function with parameter ($key,$var) when I return or dd the $request_body the value of "order_number" be null but the $var value returned\printed correctly here is the dd result


"Y81383124932"

array:9 [▼
"order_number" => null
"order_id" => null
"prescription_id" => null
"payer_id" => null
"card_id" => null
"prescription_key" => null
]


but when changing "order_number" position in the above code like this


            $request_body = [
"order_id" => null,
"prescription_id" => null,
"payer_id" => null,
"card_id" => null,
"prescription_key" => null,
"order_number" => $val,
];

the array act as normal and order_number be set with its value here is dd


"Y81383124932"
array:9 [▼
"order_id" => null
"order_number" => "Y81383124932"
"prescription_id" => null
"payer_id" => null
"card_id" => null
"prescription_key" => null
]

So why this could happen, is there a cached value or something the PHP used to assign the variable to the array?



My Answer:
$key => $val, // Assigning the value of the $val variable to the key in the array
'status' => null, // Assigning a null value to the 'status' key in the array
'created_at' => now(), // Assigning the current timestamp to the 'created_at' key in the array
];_ // Code to create the admin task using the $request_body array
}

Rate this post

3 of 5 based on 8711 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us