Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dk4210's avatar

PHP array help

I'm learning how to manipulate arrays, therefore I could use some help with this.

I have the following soap call

public function __construct($coid, $env, $app_id, $product) 
    {
        
        $url = APP_ROOT."dev/install/wsdl/my.wsdl";

        try{
        $soapclient = new SoapClient($url);
        //This overwrites the location at the bottom of the WSDL file
        $soapclient->__setLocation('http://exampleurl'); 
        $params = array('clientOid' => $coid);
        $response =$soapclient->getClientSetup($params);
        
        
        
        //var_dump($response);
        //$array = json_decode(json_encode($response), true);
        print_r($response);
        echo "--------";
         
        //echo  $array['code']['5']['Description'];
        var_dump($array);
                
        }catch(Exception $e){
            echo $e->getMessage();
        }   

My output look like this (Notice there also isn't a productUrl and I need this key to display)

[
{
clientSetup: {
selectedProducts: {
0: {
code: "1001",
description: "datapoint 1"
},
1: {
code: "1049",
description: "datapoint 2"
},
2: {
code: "1032",
description: "datapoint 3"
},
3: {
code: "1013",
description: "datapoint 4"
}
},
]

I would like it to look like this

[
{
code: 1001,
description: "datapoint 1",
productUrl: "NO_URL"
},
{
code: 1089,
description: "datapoint2",
productUrl: "http://example.com/"
},
{
code: 2101,
description: "datapoint2",
productUrl: "NO_URL"
}
]

Please advise. Thanks!

0 likes
19 replies
Sinnbeck's avatar

So something like this (add url yourself)

$final = collect($array['clientSetup']['selectedProducts'])->map(function ($item) {
    $item['code'] = intval($item['code']);
    $item['description'] = str_replace(' ', '', $item['description'];
    $item['productUrl'] = 'NO_URL';
    return $item ;
})->values();
dk4210's avatar

Thanks for the response

I added the code in there like this

public function __construct($coid, $env, $app_id, $product) 
    {
        
        $url = APP_ROOT."dev/install/wsdl/my.wsdl";

        try{
        $soapclient = new SoapClient($url);
        //This overwrites the location at the bottom of the WSDL file
        $soapclient->__setLocation('http://exampleurl'); 
        $params = array('clientOid' => $coid);
        $response =$soapclient->getClientSetup($params);
        
        $final = collect($array['clientSetup']['selectedProducts'])->map(function ($item) {
        $item['code'] = intval($item['code']);
        $item['description'] = str_replace(' ', '', $item['description']);
        $item['productUrl'] = 'NO_URL';
        return $item ;
        })->values();
        
        //var_dump($response);
        //$array = json_decode(json_encode($response), true);
        print_r($response);
        echo "--------";
         
        //echo  $array['code']['5']['Description'];
        var_dump($array);
                
        }catch(Exception $e){
            echo $e->getMessage();
        }   

I received this error

Call to undefined function collect()
dk4210's avatar

Plain PHP. I wish this was Laravel!

ahmeddabak's avatar

Based on the answer of @sinnbeck

$final = array_values(array_map(function ($item) {
    $item['code'] = intval($item['code']);
    $item['description'] = str_replace(' ', '', $item['description'];
    $item['productUrl'] = 'NO_URL';
    return $item ;
},$array['clientSetup']['selectedProducts']));
dk4210's avatar

I don't get any kind of output

dk4210's avatar

Guess I will give up on this one. Thanks everyone for the help

Sinnbeck's avatar

Well did you remember to change your dump to output $final and not $response?

jlrdw's avatar

First you can use map without using collections.

Second, Jeffrey has a free video on some array manipulation, including array_map.

Sinnbeck's avatar

Any chance your $response is actually a json string, and you need to run json_decode on it?

jlrdw's avatar

You do realize when dealing with array's, it can take a little trial and error. Walk away, take a breath, then come back. Also have you did json_decode. Your array is a json array. w3schools has example of objects, array's in json and regular php array's.

dk4210's avatar

Thanks everyone. Just getting aggravated with it. Let me walk away and come back to it

dk4210's avatar

I'm back and I almost have it.

This may not be the best solution, but I almost have it working

Here's the code

public function __construct($coid, $env, $app_id, $product) 
    {
        
        $url = APP_ROOT."dev/install/wsdl/my.wsdl";

        try{
        $soapclient = new SoapClient($url);
        //This overwrites the location at the bottom of the WSDL file
        $soapclient->__setLocation('http://exampleurl'); 
        $params = array('clientOid' => $coid);
        $response =$soapclient->getClientSetup($params);
        
        // This returns an object
        $response =$soapclient->getClientSetup($params);
        
        // Convert to an array  
        $objtoarray = json_decode(json_encode($response), true);
        
        // Add sqare brackets to beginning and end
        $tojson = json_encode(array($objtoarray));
        
        $removed = array_shift($objtoarray); // remove clientSetup
        $removed2 = array_shift($removed); // remove selectedProducts
        
                        
        print_r($removed2);
        echo "--------";
         
        //echo  $array['code']['5']['Description'];
        var_dump($array);
                
        }catch(Exception $e){
            echo $e->getMessage();
        }   

This returns the following

[
{
0: {
code: "1032",
description: "datapoint 1"
},
1: {
code: "2201",
description: "datapoint 2"
},
2: {
code: "2200",
description: "datapoint 3"
},
3: {
code: "1049",
description: "datapoint 4"
},

]

I need to remove the Index numbers in this case 0 - 3 and add the productUrl: "NO_URL" to all. It should look like this

[
{
code: 1001,
description: "datapoint 1",
productUrl: "NO_URL"
},
{
code: 1089,
description: "datapoint 1",
productUrl: "NO_URL"
},
{
code: 2101,
description: "datapoint 1",
productUrl: "NO_URL"
}
]

If some one could help me get this. I will be happy to buy you a beer!

jlrdw's avatar

There is no productUrl in top array, so step one is figure out why it's not pulled in. WIth out all the data it's hard to help. But you've come a long way.

If you get that url, here is a routine from a previous answer that may help display, it's in controller, just convert to blade.

    public function testJson()
 {
           $data = '[{  
    "ClientID":24,
    "Name":"Client1",
    "Balance1":null,
    "Balance2":null
},
{  
    "ClientID":25,
    "Name":"Client2",
    "Balance1":24,
    "Balance2":0
}]';
       // dd($data);
        $decoded = json_decode($data, true);
        //dd($decoded);
        foreach ($decoded as $d) {
            foreach ($d as $k => $v) {
                echo "$k - $v\n";
            }
        }
    }

It was some previous code from cronix that helped someone.

Please or to participate in this conversation.