R
R
Rustam Gatin2016-11-07 15:44:45
PHP
Rustam Gatin, 2016-11-07 15:44:45

How to assign array values ​​to variables in PHP?

Please tell me, in the service API I get a multidimensional array that contains an object in the form of an array. foreachI got the values ​​of these arrays as a string using the function .
It looks like this
. However, the array values ​​are in a row in the form of one variable, how can they be divided into separate variables in order to use them later?
For example, like this:
16c79f5c46904743b703c1010bb7e664.jpg
Here is the code:

<?php
error_reporting(-1);
ini_set('display_errors', 1);

require_once '../config.php';
require_once '../../autoloader.php';

use ArmtekRestClient\Http\Exception\ArmtekException as ArmtekException; 
use ArmtekRestClient\Http\Config\Config as ArmtekRestClientConfig;
use ArmtekRestClient\Http\ArmtekRestClient as ArmtekRestClient; 

try {

    // init configuration 
    $armtek_client_config = new ArmtekRestClientConfig($user_settings);  

    // init client
    $armtek_client = new ArmtekRestClient($armtek_client_config);

  $pinnum = $_GET["pin"];
if (!empty($pin)) echo "Вы не указали номер детали";
  
    $params = [
        'VKORG'         => '4170'       
        ,'KUNNR_RG'     => '43069963'
        ,'PIN'          => $pinnum
        ,'BRAND'        => ''
        ,'QUERY_TYPE'   => ''
        ,'KUNNR_ZA'     => ''
        ,'INCOTERMS'    => ''
        ,'VBELN'        => ''
    ];

    // requeest params for send
    $request_params = [

        'url' => 'search/search',
        'params' => [
            'VKORG'         => !empty($params['VKORG'])?$params['VKORG']:(isset($ws_default_settings['VKORG'])?$ws_default_settings['VKORG']:'')       
            ,'KUNNR_RG'     => isset($params['KUNNR_RG'])?$params['KUNNR_RG']:(isset($ws_default_settings['KUNNR_RG'])?$ws_default_settings['KUNNR_RG']:'')
            ,'PIN'          => isset($params['PIN'])?$params['PIN']:''
            ,'BRAND'        => isset($params['BRAND'])?$params['BRAND']:''
            ,'QUERY_TYPE'   => isset($params['QUERY_TYPE'])?$params['QUERY_TYPE']:''
            ,'KUNNR_ZA'     => isset($params['KUNNR_ZA'])?$params['KUNNR_ZA']:(isset($ws_default_settings['KUNNR_ZA'])?$ws_default_settings['KUNNR_ZA']:'')
            ,'INCOTERMS'    => isset($params['INCOTERMS'])?$params['INCOTERMS']:(isset($ws_default_settings['INCOTERMS'])?$ws_default_settings['INCOTERMS']:'')
            ,'VBELN'        => isset($params['VBELN'])?$params['VBELN']:(isset($ws_default_settings['VBELN'])?$ws_default_settings['VBELN']:'')
            ,'format'       => 'json'
        ]

    ];

    // send data
    $response = $armtek_client->post($request_params);

    // in case of json
    $json_responce_data = $response->json();


} catch (ArmtekException $e) {

    $json_responce_data = $e -> getMessage(); 

}

// 
echo "<h1>Поиск по номеру: ".$pinnum."</h1>";

json_encode($json_responce_data); // Выводит многомерный массив

$respBase = (array) $json_responce_data->RESP;


$resp = array_filter(array_map(function ($next) {
    $next = (array) $next;
    return [
        'PIN'  => $next['PIN'],
        'BRAND'  => $next['BRAND'],
        'NAME'  => $next['NAME'],
        'RVALUE'  => $next['RVALUE'],
        'VENSL'  => $next['VENSL'],
    'PRICE' => $next['PRICE'],
    'DLVDT' => $next['DLVDT'],
    ];
}, $respBase));


foreach ($resp as $next => $value ) { // Раскладываем массив на ключи и значения
  foreach ($value as $key => $data) { 
    echo $data . "</br>"; // Отображает все значения массива
}
}


echo '<h1>Массив </h1></br><pre>'; print_r($resp); echo '</pre>'; // Выводит массив, значением которого являются массивы
?>

PS I tried to use it, list()but since I get several arrays at the output, the function does not work correctly.
PPS Programming at a very low level, sorry :(
PPPS Already asked a question about this project , but now again a dead end

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pashted, 2016-11-07
@gatin

php.net/manual/en/function.extract.php
It also says that list only works on numeric arrays

A
ant1vit, 2016-11-07
@ant1vit

foreach($a as $k=>$v){
$$k = $v;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question