1

I have a string which i am spitting using explode functionality. Code Snippet is shown below

$string = "2017167637/ 43/ 42/ 38/ 46/ 41/ 40/ 39";
                                $tags = (explode("/",$string));
                                print_r($tags);
                                foreach ($tags as $key)
                                $invoicedata[] = (object)$key;

                                echo '<pre>'; print_r($invoicedata); exit;

The Output what i am getting right now is

 Array ( [0] => 2017167637 [1] => 43 [2] => 42 [3] => 38 [4] => 46 [5] => 41 [6] => 40 [7] => 39 )
 Array
(
[0] => stdClass Object
    (
        [scalar] => 2017167637
    )

[1] => stdClass Object
    (
        [scalar] =>  43
    )

[2] => stdClass Object
    (
        [scalar] =>  42
    )

[3] => stdClass Object
    (
        [scalar] =>  38
    )

[4] => stdClass Object
    (
        [scalar] =>  46
    )

[5] => stdClass Object
    (
        [scalar] =>  41
    )

[6] => stdClass Object
    (
        [scalar] =>  40
    )

[7] => stdClass Object
    (
        [scalar] =>  39
    )

But the actual output what i need is in place of [scalar] i need [invoice]. How to do it? Any help appreciated.

Actual output what i need is shown below

  Array ( [0] => 2017167637 [1] => 43 [2] => 42 [3] => 38 [4] => 46 [5] => 41 [6] => 40 [7] => 39 )
  Array
  (
[0] => stdClass Object
    (
        [invoice] => 2017167637
    )

[1] => stdClass Object
    (
        [invoice] =>  43
    )

[2] => stdClass Object
    (
        [invoice] =>  42
    )

[3] => stdClass Object
    (
        [invoice] =>  38
    )

[4] => stdClass Object
    (
        [invoice] =>  46
    )

[5] => stdClass Object
    (
        [invoice] =>  41
    )

[6] => stdClass Object
    (
        [invoice] =>  40
    )

[7] => stdClass Object
    (
        [invoice] =>  39
    )

)

I tried with adding $invoice['invoice'] but it did not worked so how to get the desired output any help appreciated.

1 Answer 1

1

Here is working code. Try this.

$string = "2017167637/ 43/ 42/ 38/ 46/ 41/ 40/ 39";
                            $tags = (explode("/",$string));
                            print_r($tags);

                            foreach ($tags as $key=>$value)
                            {
                            $invoicedata[$key] = new stdClass();    
                            $invoicedata[$key]->invoice = $value;
                            }
                            echo '<pre>'; print_r($invoicedata); exit;
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @ShreyasBandimat I have edited my code. Please check now. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.