Home > Net >  Split array in php
Split array in php

Time:02-01

I am getting result in php. I want to split the array to get each value in variable or list. The result is like this:

Array
(
    [success] => 1
    [result] => Array
        (
            [id] => 12345678ABCDEXXX
            [userid] => 2950
            [system_user_id] => 76
            [coin] => TRX
            [amount] => 11.110000
            [feePercent] => 0
            [feeAmount] => 0
            [memoTag] => 
            [networkFee] => 0
            [address] => TESTADDRESSHERE
            [confirmations] => 0
            [callBackUrl] => www.xyz.com/test
            [transactionStatus] => WaitingForTransaction
            [transactionType] => Deposit
            [createdOn] => 1643692929
            [modifiedOn] => 1643692929
            [expiredon] => 1644988929
            [description] => This is description
        )

)

how to split and get every value?

CodePudding user response:

This may answer your question

https://www.php.net/manual/en/function.extract.php

extract($array)

CodePudding user response:

Php Multidimensional array to variable

<?php
$array = Array
(
    'success' => '1',
    'result' => array(
        'id' => '12345678ABCDEXXX',
        'userid' => '2950',
        'system_user_id' => '76',
        'coin' => 'TRX',
        'amount' => '11.110000',
        'feePercent' => '0',
        'feeAmount' => '0',
        'memoTag' => '',
        'networkFee' => '0',
        'address' => 'TESTADDRESSHERE',
        'confirmations' => '0',
        'callBackUrl' => 'www.xyz.com/test',
        'transactionStatus' => 'WaitingForTransaction',
        'transactionType' => 'Deposit',
        'createdOn' => '1643692929',
        'modifiedOn' => '1643692929',
        'expiredon' => '1644988929',
        'description' => 'This is description'
    )
);

function extractMultidimentional($array){
    foreach ($array as $key => $value) {
        if(is_array($value)){
            extractMultidimentional($value);
        }else{
            global ${$key};//Set as global 
            ${$key} = $value;//Assign with its value
        }
    }
}

extractMultidimentional($array);


// Now you can call each key as a variable
echo $success;//1
echo $system_user_id;//76
echo $expiredon;//1644988929

If you have multidimensional array and those have same key then the variable will store last array value

CodePudding user response:

You can use array_chunk like this

$array = array('value -1', 'value 2', 'value 3', 'value 4', 'value 5','value -6','value -7');

$newArrays = array_chunk($array,2); // apply array chunk

echo "";

// print the first segment (position) array after splitting that array.

print_r($newArrays[0]);

  •  Tags:  
  • Related