logo
Tags down

shadow

php function return an array


By : user8014169
Date : May 05 2020, 10:00 AM

I have issue about return value, now i can return back with variable but it automatic separate it become 4 array. Below is my data when i used json_decode $json_template = json_decode($json_body,true); function

{
    "username": "",
    "password": "",
    "environment": "900"
}

then , i need to change the value by merge variable so i use Map function:

$map = new Map($json_template);
$maps = $map->map(function($key, $value){
            if($key == 'password'){
                 $value = 'test';
            }
            return array($key=>$value);
        });

It result is become:

[
    {
        "username": ""
    },
    {
        "password": "test"
    },
    {
        "environment": "900"
    }
]

But i expected my result will come out like

{
    "username": "",
    "password": "test",
    "environment": "900"
}

what should i add or change the code?

Answer :

From doc about the map method:

Returns the result of applying a callback to each value

and:

The callable should return what the key will be mapped to in the resulting map.

It mean you only need to return modified value instead of an array {key:value}.

This is a working example on my pc using (PHP 7.0 CLI):

<?php
$json_template = json_decode(' { "username": "", "password": "", "environment": "900" } ', true); $map = new \Ds\Map($json_template); $maps = $map->map(function($key, $value){ if($key == 'password'){ $value = 'test'; } return $value; }); print_r($maps->toArray());

Output:

Array
(
    [username] =>
    [password] => test
    [environment] => 900
)


Share : facebook icon twitter icon
Related Posts Related Posts :
  • clear array after form submit
  • pass url variable to javascript
  • access data using api
  • div position fixed css
  • add horizontal line between two div
  • disable toggle button css
  • css fixed line height
  • background image not working
  • table cell border
  • text line break css
  • overlay div on div
  • text direction rtl css
  • javascript convert string to an object
  • bind inner div click to outer div
  • jquery ajax oncomplete
  • how to use promise in for loop
  • jquery get element using variable
  • javascript send textbox value
  • pass div class content to another class or id
  • css new line after element
  • css calc not working properly
  • link disabled jquery
  • sql join table to subquery
  • MS Access SQL Issue with OR AND Operators
  • set textarea value using php
  • php merge two arrays into one
  • php form with google captcha
  • update current row in php
  • php date format mysql
  • mysql pdo select query
  • PHP mysqldump database to sql file
  • display image query
  • php create array using foreach loop
  • associative array php
  • group_concat in mysql laravel
  • php array_push empty
  • mysql on update cascade not working
  • pdo insert prepared statement
  • mysql insert ignore vs on duplicate key
  • mysql insert into existing row
  • mysql multi insert query
  • mysql group by show all rows
  • php change profile image
  • combine multiple arrays into one array php
  • how to sum values in an array
  • how to get sum of the total time
  • How to Create Facebook live stream without notification using API ?
  • on button click make div visible
  • css affect another element on hover
  • insert line after paragraph word
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org